malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const int maxN(2e5 + 10);   typedef int i_N[maxN];   int N, K;   namespace graph {   i_N A, P, is_us, Cus, deg;   pair<int, int> edge[maxN];   vector<int> adj[maxN], node[maxN];   void link(const int u, const int v) { adj[u].push_back(v); }   void dfs(const int u) {     for (int i = 0; i < (int)adj[u].size(); i++) {       const int v(adj[u][i]);       if (P[v]) continue;       P[v] = u;       dfs(v);       Cus[u] += Cus[v];     }     Cus[u] += is_us[u];   }   }  // namespace graph   int main() {     ios_base::sync_with_stdio(0);     {       using namespace graph;       cin >> N >> K;       for (int i = 0; i < K * 2; i++) cin >> A[i], is_us[A[i]] = 1;       for (int i = 0; i < N - 1; i++) {         int u, v;         cin >> u >> v;         edge[i] = pair<int, int>(u, v);         link(u, v);         link(v, u);       }     }     {       using namespace graph;       P[1] = -1;       dfs(1);     }     {       using namespace graph;       for (int i = 0; i < N - 1; i++) {         const int v(P[edge[i].first] == edge[i].second ? edge[i].first                                                        : edge[i].second);         const int u(P[v]);         if (Cus[v] && 2 * K - Cus[v]) deg[u]++, deg[v]++;       }       long long ans(0);       queue<int> bfs_node;       for (int u = 1; u <= N; u++)         if (deg[u] == 1) node[1].push_back(u);       for (int i = 1; i <= 2 * K; i++) {         for (int j = 0; j < (int)node[i].size(); j++) {           const int u(node[i][j]);           deg[u] = 0;           for (int k = 0; k < (int)adj[u].size(); k++) {             const int v(adj[u][k]);             if (!deg[v]) continue;             ans += is_us[u];             deg[v]--;             is_us[v] += is_us[u];             if (deg[v] == 1) node[is_us[v]].push_back(v);           }         }       }       cout << ans;     }   }
#include <bits/stdc++.h>   using namespace std;   int n, m;   int inf = (int)1e9;   int c[4];   string t[4];   vector<vector<long long> > dp;   const int hhh = 21;   string z[hhh] = { 000100010001 ,  100010001000 ,  000000010001 ,  000010001000 ,                     001100111111 ,  001000111111 ,  000100111111 ,  000000111111 ,                     100110011111 ,  000110011111 ,  100010011111 ,  000010011111 ,                     110011001111 ,  010011001111 ,  100011001111 ,  000011001111 ,                     000000000001 ,  000000001000 ,  000000000000 ,  000000001111 ,                     000011111111 };   int co[hhh][4] = {{0, 0, 1, 0}, {0, 0, 1, 0}, {1, 0, 1, 0}, {1, 0, 1, 0},                     {0, 1, 0, 0}, {1, 1, 0, 0}, {1, 1, 0, 0}, {2, 1, 0, 0},                     {0, 1, 0, 0}, {1, 1, 0, 0}, {1, 1, 0, 0}, {2, 1, 0, 0},                     {0, 1, 0, 0}, {1, 1, 0, 0}, {1, 1, 0, 0}, {2, 1, 0, 0},                     {0, 1, 1, 0}, {0, 1, 1, 0}, {0, 0, 2, 0}, {0, 2, 0, 0},                     {0, 0, 0, 0}};   int ff(string& s) {     int res = 0;     for (int i = 11; i >= 0; --i) res = res + res + (s[i] -  0 );     return res;   }   int f(int jj) {     vector<int> z;     for (int i = 0; i < 4; ++i) z.push_back(int(t[i][jj] ==  * ));     reverse(z.begin(), z.end());     int res = 0;     for (int i = 0; i < z.size(); ++i) res = res + res + z[i];     return res;   }   void add(int i, int j, int ii, int jj, int cost) {     dp[i][j] = min(dp[i][j], dp[ii][jj] + cost);   }   void addd(int ii, int mask, int ma, int cost) {     ma = (mask & ma);     for (int p = 0; p < 4; ++p)       if ((ma & (1 << p)) > 0) return;     ma = (ma >> 4) + (f(ii + 3) << 8);     add(ii + 1, ma, ii, mask, cost);   }   int main() {     ios_base::sync_with_stdio(false);     cin >> n;     m = (1 << 12);     for (int i = 0; i < 4; ++i) cin >> c[i];     for (int i = 2; i >= 0; --i) c[i] = min(c[i], c[i + 1]);     for (int i = 0; i < 4; ++i) {       cin >> t[i];       t[i] += string(3,  . );     }     dp.assign(n + 1, vector<long long>(m, inf));     dp[0][f(0) + (f(1) << 4) + (f(2) << 8)] = 0;     for (int i = 0; i < n; ++i) {       for (int mask = 0; mask < m; ++mask) {         if (dp[i][mask] == inf) continue;         add(i + 1, 0, i, mask, c[3]);         for (int s = 0; s < hhh - 1; ++s) {           int cost = 0;           for (int o = 0; o < 4; ++o) cost += co[s][o] * c[o];           addd(i, mask, ff(z[s]), cost);         }         int cost = 0;         for (int p = 0; p < 4; ++p)           if ((mask & (1 << p)) > 0) cost += c[0];         addd(i, mask, ff(z[hhh - 1]), cost);       }     }     cout << dp[n][0] << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   using ll = long long;   const ll mod = 1e18 + 7;   void solve() {     int n, k;     cin >> n >> k;     string s;     cin >> s;     for (int i = 0; i < k; i++) {       int z = 0, one = 0, j = i + k;       for (j; j < n; j += k) {         if (s[j] ==  1 )           one++;         else if (s[j] ==  0 )           z++;       }       if (one > 0 && z > 0) {         cout <<  NO n ;         return;       }       if ((one > 0 && s[i] ==  0 ) || (z > 0 && s[i] ==  1 )) {         cout <<  NO n ;         return;       }       if (one > 0) {         j = i;         for (j; j < n; j += k) {           s[j] =  1 ;         }       } else if (z > 0) {         j = i;         for (j; j < n; j += k) {           s[j] =  0 ;         }       }     }     int z = 0, one = 0;     for (int i = 0; i < k; i++) {       if (s[i] ==  0 )         z++;       else if (s[i] ==  1 )         one++;     }     if (one > k / 2 || z > k / 2) {       cout <<  NO n ;     } else       cout <<  YES n ;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int t;     t = 1;     cin >> t;     while (t--) {       solve();     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 520;   long long gi() {     long long x = 0, o = 1;     char ch = getchar();     while (!isdigit(ch) && ch !=  - ) ch = getchar();     if (ch ==  - ) o = -1, ch = getchar();     while (isdigit(ch)) x = x * 10 + ch -  0 , ch = getchar();     return x * o;   }   int k, n, t, x[N], y[N];   long long a[N][N], b[N][N];   int main() {     cin >> k;     n = 1 << k;     for (int i = 0; i < n; i++)       for (int j = 0; j < n; j++) a[i][j] = gi();     cin >> t;     for (int i = 1; i <= t; i++) x[i] = gi(), y[i] = gi();     for (int i = t; i; i--)       x[i] = (x[i] - x[1] + n) % n, y[i] = (y[i] - y[1] + n) % n;     for (int i = 1; i <= k; i++) {       for (int j = 0; j < n; j++)         for (int k = 0; k < n; k++)           for (int l = 1; l <= t; l++)             b[j][k] ^= a[(j - x[l] + n) % n][(k - y[l] + n) % n];       memcpy(a, b, sizeof(b));       memset(b, 0, sizeof(b));       for (int j = 1; j <= t; j++) x[j] = x[j] * 2 % n, y[j] = y[j] * 2 % n;     }     int ans = 0;     for (int i = 0; i < n; i++)       for (int j = 0; j < n; j++)         if (a[i][j]) ++ans;     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   inline long long read() {     long long x = 0;     bool f = false;     char c = getchar();     while (c <  0  || c >  9 ) {       if (c ==  - ) f = true;       c = getchar();     }     while (c >=  0  && c <=  9 ) {       x = (x << 3) + (x << 1) + (c ^  0 );       c = getchar();     }     return f ? -x : x;   }   inline void write(long long x) {     if (x < 0) putchar( - ), x = -x;     if (x > 9) write(x / 10);     putchar(x % 10 +  0 );   }   long long n, k, a[101], b[101][101], ans, cnt1, cnt2;   signed main() {     n = read();     k = read();     for (register long long i = 0; i < n; ++i)       a[i] = read(), b[i / k][i % k] = a[i];     for (register long long i = 0; i < k; ++i) {       cnt1 = cnt2 = 0;       for (register long long j = 0; j < n / k; ++j)         b[j][i] == 1 ? ++cnt1 : ++cnt2;       ans += min(cnt1, cnt2);     }     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int pointer;   vector<long long> M;   vector<long long> B;   bool bad(int l1, int l2, int l3) {     double x_12 = double(B[l2] - B[l1]) / double(M[l1] - M[l2]);     double x_13 = double(B[l3] - B[l1]) / double(M[l1] - M[l3]);     return x_13 < x_12;   }   void add(long long m, long long b) {     M.push_back(m);     B.push_back(b);     while (M.size() >= 3 && bad(M.size() - 3, M.size() - 2, M.size() - 1)) {       M.erase(M.end() - 2);       B.erase(B.end() - 2);     }   }   long long query(long long x) {     if (pointer > M.size()) pointer = M.size() - 1;     while (pointer < M.size() - 1 &&            M[pointer + 1] * x + B[pointer + 1] < M[pointer] * x + B[pointer])       pointer++;     return M[pointer] * x + B[pointer];   }   int main() {     long long int a[100123];     long long int b[100123];     int n;     scanf(  %d , &n);     for (int i = 0; i < n; i++) {       cin >> a[i];     }     for (int i = 0; i < n; i++) {       cin >> b[i];     }     long long int cost = 0;     add(b[0], 0);     pointer = 0;     for (int i = 0; i < n; i++) {       cost = query(a[i]);       if (i < n) add(b[i], cost);     }     cout << cost << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int i, c = 0, j, x = 0, n;     cin >> n;     string s;     vector<string> ar;     for (i = 0; i < n; i++) {       cin >> s;       ar.push_back(s);     }     for (i = 0; i < ar[0].length(); i++) {       for (j = 0; j < n - 1; j++) {         if (ar[j][i] != ar[j + 1][i]) {           x = 1;           break;         }       }       if (x == 0)         c++;       else         break;     }     cout << c;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = (int)((1e5) + 100);   int gcd(int a, int b) {     if (a == 0) return b;     return gcd(b % a, a);   }   int max(int a, int b) {     if (a > b)       return a;     else       return b;   }   int min(int a, int b) {     if (a < b)       return a;     else       return b;   }   void pr(int x) { cout << x; }   void prl(int x) { cout << x <<   n ; }   template <class T>   bool ckmin(T& a, const T& b) {     return b < a ? a = b, 1 : 0;   }   template <class T>   bool ckmax(T& a, const T& b) {     return a < b ? a = b, 1 : 0;   }   const int N = 200 * 1000 + 11;   void solve() {     int q;     cin >> q;     std::vector<int> v(N);     int left = 0, right = 0;     for (int i = 0; i < q; ++i) {       string x;       cin >> x;       int id;       cin >> id;       if (i == 0) {         v[id] = left;         left--;         right++;       } else {         if (x ==  L ) {           v[id] = left;           left--;         } else if (x ==  R ) {           v[id] = right;           right++;         } else {           cout << min(abs(v[id] - left), abs(v[id] - right)) - 1 <<   n ;         }       }     }   }   signed main() {     ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);     solve();   }
#include <bits/stdc++.h>   using namespace std;   map<pair<int, long long>, int> dp;   int n, s[1000010];   int calc(pair<int, long long> u) {     if (dp.count(u) > 0) return dp[u];     if (u.first == 0) return dp[u] = 0;     vector<bool> g(61);     for (int i = 1; i <= u.first; ++i) {       if (u.second & (1ll << i)) continue;       g[calc({u.first - i, u.second | (1ll << i)})] = true;     }     int x = 0;     while (g[x]) ++x;     return dp[u] = x;   }   int main() {     scanf( %d , &n);     int ans = 0;     for (int i = 0; i < n; ++i) {       scanf( %d , &s[i]);       ans = ans ^ calc({s[i], 0});     }     printf( %s n , ans != 0 ?  NO  :  YES );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int r[8192];   vector<int> g[8192];   int ans[8192] = {       0,   };   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int n;     cin >> n;     ans[0] = 1;     for (int i = 0; i < 8192; i++) {       g[i].push_back(0);     }     for (int i = 0; i < 8192; i++) {       r[i] = 8192;     }     for (int i = 0; i < n; i++) {       int x;       cin >> x;       for (const auto& key : g[x]) {         int to = (key ^ x);         ans[to] = 1;         while (r[to] > x) {           r[to]--;           if (r[to] != x) g[r[to]].push_back(to);         }       }       g[x].clear();     }     int cnt = 0;     for (int i = 0; i < 8192; i++) {       cnt += ans[i];     }     cout << cnt <<   n ;     for (int i = 0; i < 8192; i++) {       if (ans[i]) cout << i <<    ;     }     cout <<   n ;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     cin >> s;     int h = 0;     int start = 0;     map<int, int> m;     set<int> hash;     for (int i = 0; i < s.size(); ++i) {       int index = i;       if (s[i] ==  > ) {         for (int i = 0; i < 2 * h; ++i) {           cout <<    ;         }         cout << s.substr(start, index - start + 1) << endl;         hash.insert(index - 1);         if (s[index - 2] !=  / ) {           ++h;         }         start = index + 1;       } else if (s[i] ==  / ) {         --h;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int a, b, c, d;     while (scanf( %d%d%d%d , &a, &b, &c, &d) != EOF) {       if (d > 2 * c || d >= b || c > 2 * d) {         printf( -1 n );       } else {         printf( %d n%d n%d n , 2 * a, 2 * b, max(c, d));       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     long long int T;     cin >> T;     while (T--) {       long long int x, y, z;       cin >> x >> y >> z;       long long int ans = 0;       if (x == y || x == z || y == z) {         if (x == y && z == y) {           cout <<  YES                 <<   n                 << x <<     << x <<     << x <<   n ;           ans = 1;         } else if (x == z && y < x) {           cout <<  YES                 <<   n                 << x <<     << y <<     << 1 <<   n ;           ans = 1;         } else if (y == z && x < y) {           cout <<  YES                 <<   n                 << x <<     << 1 <<     << y <<   n ;           ans = 1;         } else if (x == y && z < x) {           cout <<  YES                 <<   n                 << x <<     << z <<     << 1 <<   n ;           ans = 1;         } else if (ans == 0)           cout <<  NO                 <<   n ;       } else         cout <<  NO               <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long oo = 1e18;   struct Data {     long long v;     int x, y;     Data() {}     Data(long long tv, int tx, int ty) {       v = tv;       x = tx;       y = ty;     }   };   int a[2010], b[2010];   Data mp1[4000010], mp2[4000010];   long long gabs(long long x) { return x < 0 ? -x : x; }   bool operator<(Data a, Data b) { return a.v < b.v; }   int main() {     int n;     scanf( %d , &n);     long long sa = 0;     for (int i = 1; i <= n; i++) {       scanf( %d , &a[i]);       sa += a[i];     }     int m;     scanf( %d , &m);     long long sb = 0;     for (int i = 1; i <= m; i++) {       scanf( %d , &b[i]);       sb += b[i];     }     long long t0 = gabs(sa - sb);     long long t1 = oo;     int tx, ty;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++)         if (gabs(sa - sb - 2ll * (a[i] - b[j])) < t1) {           t1 = gabs(sa - sb - 2ll * (a[i] - b[j]));           tx = i;           ty = j;         }     if (n == 1 || m == 1) {       if (t0 <= t1)         printf( %lld n0 n , t0);       else         printf( %lld n1 n%d %d n , t1, tx, ty);       return 0;     }     long long t2 = oo;     int tx1, ty1, tx2, ty2;     int tot;     for (int i = 1; i < m; i++)       for (int j = i + 1; j <= m; j++) {         mp1[++tot] = Data(2ll * (b[i] + b[j]), i, j);         mp2[tot] = Data(-2ll * (b[i] + b[j]), i, j);       }     sort(mp1 + 1, mp1 + tot + 1);     sort(mp2 + 1, mp2 + tot + 1);     for (int i = 1; i < n; i++)       for (int j = i + 1; j <= n; j++) {         Data tmp1 =             *lower_bound(mp1 + 1, mp1 + tot + 1,                          Data(-(sa - sb - 2ll * a[i] - 2ll * a[j]), 0, 0));         int pl1 = lower_bound(mp1 + 1, mp1 + tot + 1,                               Data(-(sa - sb - 2ll * a[i] - 2ll * a[j]), 0, 0)) -                   mp1;         Data tmp2 = *lower_bound(mp2 + 1, mp2 + tot + 1,                                  Data(sa - sb - 2ll * a[i] - 2ll * a[j], 0, 0));         int pl2 = lower_bound(mp2 + 1, mp2 + tot + 1,                               Data(sa - sb - 2ll * a[i] - 2ll * a[j], 0, 0)) -                   mp2;         tmp2.v = -tmp2.v;         Data tmp;         if (pl1 <= tot && gabs(sa - sb - 2ll * a[i] - 2ll * a[j] + tmp1.v) < t2) {           t2 = gabs(sa - sb - 2ll * a[i] - 2ll * a[j] + tmp1.v);           tx1 = i;           tx2 = j;           ty1 = tmp1.x;           ty2 = tmp1.y;         }         if (pl2 <= tot && gabs(sa - sb - 2ll * a[i] - 2ll * a[j] + tmp2.v) < t2) {           t2 = gabs(sa - sb - 2ll * a[i] - 2ll * a[j] + tmp2.v);           tx1 = i;           tx2 = j;           ty1 = tmp2.x;           ty2 = tmp2.y;         }       }     if (t0 <= t1 && t0 <= t2)       printf( %lld n0 n , t0);     else if (t1 <= t2)       printf( %lld n1 n%d %d n , t1, tx, ty);     else       printf( %lld n2 n%d %d n%d %d n , t2, tx1, ty1, tx2, ty2);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 2e3 + 5;   long long a[N], dp[N];   long long n, k;   long long check(long long m) {     for (int i = 0; i < n; i++) {       dp[i] = i - 1;       for (int j = 0; j < i; j++) {         if (abs(a[i] - a[j]) <= 1ll * (i - j) * m) {           dp[i] = min(dp[i], dp[j] + i - j - 1);         }       }       if (dp[i] + n - i <= k) {         return true;       }     }     return false;   }   int main() {     cin >> n >> k;     for (int i = 0; i < n; i++) {       cin >> a[i];     }     long long l = -1, r = 2e9;     while (r - l > 1) {       long long mid = (l + r) / 2;       if (check(mid)) {         r = mid;       } else {         l = mid;       }     }     cout << r;   }
#include <bits/stdc++.h>   using namespace std;   const int c = 100002, k = 6;   long long s[7], n, ans = 1e9, mini = 1e9, t[c];   priority_queue<pair<int, pair<int, int> > > q;   void pb(int a, int b) {     q.push({t[a] - s[b], {a, b}});     mini = min(mini, t[a] - s[b]);   }   int main() {     ios_base::sync_with_stdio(false);     for (int i = 1; i <= k; i++) cin >> s[i];     sort(s + 1, s + 7);     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> t[i];       pb(i, 1);     }     while (true) {       long long f = q.top().first, id = q.top().second.first,                 pos = q.top().second.second;       q.pop();       ans = min(ans, f - mini);       if (pos == k) {         break;       }       pb(id, pos + 1);     }     cout << ans <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int a[100006];   int main() {     int n;     while (scanf( %d , &n) != EOF) {       if (n == 1)         puts( 1 );       else if (n == 2)         puts( -1 );       else {         int delta = n;         for (int i = 1; i <= n && delta > 0; i++) {           if (i == 1)             a[1] = 2;           else if (i & 1) {             a[i] = a[i - 1] - delta;           } else             a[i] = a[i - 1] + delta;           if (delta == 3)             delta = 2;           else             delta -= 2;         }         delta = n;         int flag = 0;         for (int i = n; i >= 1 && delta > 0; i--) {           if (i == n)             a[i] = n - 1;           else if (flag == 0) {             a[i] = a[i + 1] + delta;           } else             a[i] = a[i + 1] - delta;           if (delta == 3)             delta = 2;           else             delta -= 2;           flag ^= 1;         }         flag = 1;         for (int i = 1; i <= n; i++) {           if (a[a[i]] != n - i + 1) {             flag = 0;             break;           }         }         if (flag == 0) {           puts( -1 );           continue;         }         for (int i = 1; i <= n; i++) printf(i == 1 ?  %d  :   %d , a[i]);         puts(  );       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     cin >> n >> m;     char a[53][53] = {5};     int ans = 0;     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++) {         char x;         cin >> x;         if (x ==  a  || x ==  f  || x ==  c  || x ==  e ) a[i][j] = x;       }     for (int i = 0; i < n; i++)       for (int j = 0; j < m; j++) {         string c =   ;         c += a[i][j];         c += a[i][j + 1];         c += a[i + 1][j];         c += a[i + 1][j + 1];         sort(c.begin(), c.end());         if (c ==  acef ) ans += 1;       }     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxN = (int)5e3 + 3;   vector<int> g[maxN], p[maxN];   int u, v, cur_h[maxN], cnt, n;   bool used[maxN], dp[maxN], ans[maxN];   void dfs(int v, int* cur_h) {     used[v] = true;     cur_h[v] = 1;     for (int i = 0; i < (int)g[v].size(); ++i) {       int to = g[v][i];       if (!used[to]) {         p[v].push_back(to);         dfs(to, cur_h);         cur_h[v] += cur_h[to];       }     }   }   void init_arrays() {     memset(dp, 0, sizeof(bool) * maxN);     memset(used, 0, sizeof(bool) * maxN);     memset(cur_h, 0, sizeof(int) * maxN);   }   int main() {     ios_base::sync_with_stdio(0);     cin >> n;     for (int i = 0; i < n - 1; ++i) {       cin >> u >> v;       g[u].push_back(v);       g[v].push_back(u);     }     init_arrays();     dfs(1, cur_h);     dp[0] = 1;     for (int i = 1; i <= n; ++i) {       for (int k = 1; k <= n; ++k) dp[k] = 0;       for (int j = 0; j < (int)p[i].size(); ++j)         for (int k = n; k >= 0; --k) dp[k + cur_h[p[i][j]]] |= dp[k];       for (int k = n; k >= 0; --k) dp[k + n - cur_h[i]] |= dp[k];       for (int k = n; k >= 0; --k) ans[k] |= dp[k];     }     --n;     for (int i = 1; i < n; ++i)       if (ans[i] == true) ++cnt;     cout << cnt << endl;     for (int i = 1; i < n; ++i)       if (ans[i] == true) cout << i <<     << n - i << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int mo = 998244353;   const int M = (1 << 23) | 5;   const int N = 50;   int n, m, fa[N], e[N][N];   int sz[N], id[N];   int GG[M], cnt[M];   char s[N][N];   int get(int x) { return x == fa[x] ? x : fa[x] = get(fa[x]); }   int main() {     scanf( %d , &n);     for (int i = (int)(1); i <= (int)(n); i++) fa[i] = i;     for (int i = (int)(1); i <= (int)(n); i++) scanf( %s , s[i] + 1);     for (int i = (int)(1); i <= (int)(n); i++)       for (int j = (int)(1); j <= (int)(n); j++)         if (s[i][j] ==  A ) fa[get(i)] = get(j);     for (int i = (int)(1); i <= (int)(n); i++)       for (int j = (int)(1); j <= (int)(n); j++)         if (s[i][j] ==  X  && get(i) == get(j)) return puts( -1 ), 0;     for (int i = (int)(1); i <= (int)(n); i++) ++sz[get(i)];     for (int i = (int)(1); i <= (int)(n); i++)       if (get(i) == i && sz[i] != 1) id[i] = ++m;     if (!m) return printf( %d n , n - 1), 0;     for (int i = (int)(1); i <= (int)(n); i++)       for (int j = (int)(1); j <= (int)(n); j++)         if (s[i][j] ==  X ) e[id[get(i)]][id[get(j)]] = 1;     for (int i = (int)(1); i <= (int)(m); i++)       for (int j = (int)(1); j <= (int)(m); j++)         if (e[i][j]) GG[((1 << i) | (1 << j)) >> 1] = 1;     for (int d = 1; d < 1 << m; d <<= 1)       for (int i = 0; i < 1 << m; i += d << 1)         for (int j = 0; j < d; j++) GG[i | j | d] |= GG[i | j];     for (int i = (int)(0); i <= (int)((1 << m) - 1); i++)       cnt[i] = cnt[i / 2] ^ (i & 1);     for (int i = (int)(0); i <= (int)((1 << m) - 1); i++) GG[i] = 1 - GG[i];     for (int d = 1; d < 1 << m; d <<= 1)       for (int i = 0; i < 1 << m; i += d << 1)         for (int j = 0; j < d; j++) GG[i | j | d] += GG[i | j];     for (int i = (int)(0); i <= (int)((1 << m) - 1); i++)       cnt[i] = ((cnt[i] + m) & 1 ? mo - 1 : 1);     for (int ans = 1;; ++ans) {       int sum = 0;       for (int i = (int)(0); i <= (int)((1 << m) - 1); i++)         cnt[i] = 1ll * cnt[i] * GG[i] % mo;       for (int i = (int)(0); i <= (int)((1 << m) - 1); i++)         sum = (sum + cnt[i]) % mo;       if (sum) return printf( %d n , n - 1 + ans), 0;     }   }