#pragma GCC optimize("O3")
#include "bits/stdc++.h"
using namespace std;
#define all(x) x.begin(),x.end()
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << p.first << " " << p.second; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { string sep; for (const T &x : v) os << sep << x, sep = " "; return os; }
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#define ASSERT(...) 42
#endif
typedef long long ll;
typedef vector<short> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pi;
const short oo = 1e4;
typedef bitset<400> bs;
void solve() {
int n; cin >> n;
vector<string> g(n);
for(auto& i : g) cin >> i;
// do BFS!
vvi ord;
vvi ds;
auto bfs = [&](int s) {
vi q = {s};
vi d(n,oo);
d[s]=0;
for(int i=0;i<q.size();++i) {
int at = q[i];
for(int to=0;to<n;++to) {
if(g[at][to]=='1') {
if(d[to]==oo) {
d[to]=d[at]+1;
q.push_back(to);
}
}
}
}
ord.push_back(q);
ds.push_back(d);
};
for(int i=0;i<n;++i) bfs(i);
vector<bs> full(n);
for(int i=0;i<n;++i) for(int j=0;j<n;++j) full[i][j]=1;
auto good = full;
int x=n;
auto isgood = [&](int i, int j) {
auto my = good;
for(int r=0;r<2;++r) {
bs match = full[0];
// everything guchi!
auto it = ord[j].end();
for(int at : ord[i]) {
while(it!=ord[j].begin()) {
int who = *prev(it);
if(ds[i][at] + ds[j][who]>x) {
match[who]=0;
--it;
} else break;
}
my[at]|=match;
}
swap(i,j);
}
return my==full;
};
for(int i=0;i<n;++i) for(int j=i+1;j<n;++j) {
while(x>=0 and isgood(i,j)) {
--x;
for(int i=0;i<n;++i) for(int j=0;j<n;++j) if(ds[i][j]==x+1) {
good[i][j]=0;
}
}
}
cout << x+1;
cout << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t; cin >> t;
for(int i=0;i<t;++i) {
// if(i)
solve();
}
}
/*
1
2
01
10
*/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | #pragma GCC optimize("O3") #include "bits/stdc++.h" using namespace std; #define all(x) x.begin(),x.end() template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << p.first << " " << p.second; } template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { string sep; for (const T &x : v) os << sep << x, sep = " "; return os; } #ifdef LOCAL #include "debug.h" #else #define debug(...) 42 #define ASSERT(...) 42 #endif typedef long long ll; typedef vector<short> vi; typedef vector<vi> vvi; typedef pair<int,int> pi; const short oo = 1e4; typedef bitset<400> bs; void solve() { int n; cin >> n; vector<string> g(n); for(auto& i : g) cin >> i; // do BFS! vvi ord; vvi ds; auto bfs = [&](int s) { vi q = {s}; vi d(n,oo); d[s]=0; for(int i=0;i<q.size();++i) { int at = q[i]; for(int to=0;to<n;++to) { if(g[at][to]=='1') { if(d[to]==oo) { d[to]=d[at]+1; q.push_back(to); } } } } ord.push_back(q); ds.push_back(d); }; for(int i=0;i<n;++i) bfs(i); vector<bs> full(n); for(int i=0;i<n;++i) for(int j=0;j<n;++j) full[i][j]=1; auto good = full; int x=n; auto isgood = [&](int i, int j) { auto my = good; for(int r=0;r<2;++r) { bs match = full[0]; // everything guchi! auto it = ord[j].end(); for(int at : ord[i]) { while(it!=ord[j].begin()) { int who = *prev(it); if(ds[i][at] + ds[j][who]>x) { match[who]=0; --it; } else break; } my[at]|=match; } swap(i,j); } return my==full; }; for(int i=0;i<n;++i) for(int j=i+1;j<n;++j) { while(x>=0 and isgood(i,j)) { --x; for(int i=0;i<n;++i) for(int j=0;j<n;++j) if(ds[i][j]==x+1) { good[i][j]=0; } } } cout << x+1; cout << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; for(int i=0;i<t;++i) { // if(i) solve(); } } /* 1 2 01 10 */ |
English