Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define fst first
- #define snd second
- #define mp make_pair
- #define rep(i,a,n) for(int i=(a);i<(n);++i)
- #define per(i,a,n) for(int i=(n)-1;i>=(a);--i)
- #define all(x) (x).begin(),(x).end()
- template <typename T1, typename T2>
- bool umin(T1& x, const T2& y){return x>y ? x=y,true : false;}
- template <typename T1, typename T2>
- bool umax(T1& x, const T2& y){return x<y ? x=y,true : false;}
- typedef long long ll;
- typedef long double ld;
- typedef pair<int,int> pii;
- const int N = (int)1e6+3;
- const int mod = (int)1e9+7;
- const int INF = (int)1e9+17;
- const ll LLINF = (ll)1e18+17;
- int n, m;
- int res;
- int a[N];
- int d[N+N];
- bool mark[N+N];
- void solve() {
- priority_queue<pii, vector<pii>, greater<pii> > q;
- fill(d, d+N+N, INF);
- d[a[0]] = 0;
- q.push(mp(0, a[0]));
- while (!q.empty()) {
- int v = q.top().snd;
- int dist = q.top().fst;
- // cerr << v << ' ' << dist << endl;
- q.pop();
- if (dist != d[v]) continue;
- // cerr << "here" << endl;
- if (mark[v]) {
- res += dist;
- d[v] = dist = 0;
- }
- rep(i, 0, n) {
- int to = v ^ (1<<i);
- // cerr << to << ' ' << d[to] << endl;
- if (umin(d[to], d[v]+1)) {
- q.push(mp(d[to], to));
- }
- }
- }
- }
- int main() {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr), cout.tie(nullptr);
- #ifdef LOCAL
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #endif
- //!slow
- cin >> n >> m;
- rep(i, 0, m) {
- string s;
- cin >> s;
- rep(j, 0, n) a[i] |= ((s[j]=='R')<<j);
- // cerr << a[i] << '\n';
- mark[a[i]] = true;
- }
- solve();
- cout << res << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement