#include <bits/stdc++.h>
using namespace std;
#define fwd(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) fwd(i, 0, n)
#define all(X) X.begin(), X.end()
#define sz(X) int(size(X))
#define pb push_back
#define eb emplace_back
#define st first
#define nd second
using pii = pair<int, int>; using vi = vector<int>;
using ll = long long; using ld = long double;
#ifdef LOC
auto SS = signal(6, [](int) { *(int *)0 = 0; });
#define DTP(x, y) auto operator << (auto &o, auto a) -> decltype(y, o) { o << "("; x; return o << ")"; }
DTP(o << a.st << ", " << a.nd, a.nd);
DTP(for (auto i : a) o << i << ", ", all(a));
void dump(auto... x) { (( cerr << x << ", " ), ...) <<
'\n'; }
#define deb(x...) cerr << setw(4) << __LINE__ << ":[" #x "]: ", dump(x)
#else
#define deb(...) 0
#endif
const int INF = 1e4;
const int N = 512;
void solve(){
short n; cin >> n;
vector<vector<short>> dist(n, vector<short>(n, INF));
rep(i, n){
string str; cin >> str;
rep(j, n){
if(str[j] == '1')dist[i][j] = 1;
}
dist[i][i] = 0;
}
rep(k, n) rep(i, n) rep(j, n){
dist[i][j] = min<short>(dist[i][j], dist[i][k] + dist[k][j]);
}
deb(dist);
vector<vector<short>> d(n, vector<short>(n+1, -INF));
vector<vector<short>> v(n, vector<short>(n+1));
rep(i, n){
vector<pair<short, short>> t(n);
rep(j, n){
t[j] = {dist[i][j], j};
}
sort(all(t));
rep(j, n){
d[i][j+1] = t[j].st;
v[i][j+1] = t[j].nd;
}
}
deb(d);
deb(v);
vector<bitset<N>> ok(n);
vector<short> l(n, n);
auto check = [&](short D){
deb("check", D);
rep(i, n)ok[i].set();
rep(a, n){
vector<bitset<N>> ok2(n);
rep(i, n)l[i] = n;
fwd(b, a+1, n){
if(dist[a][b] <= D)continue;
short j = n;
rep(i, n){
while(d[a][j] + d[b][i+1] > D)j--;
int ind = v[b][i+1];
l[ind] = min<short>(l[ind], j);
}
deb(b);
deb(l);
}
deb(a, l);
rep(i, n){
for(int j = 0; j < l[i]; j++){
ok2[i][v[a][j + 1]] = true;
ok2[v[a][j + 1]][i] = true;
}
}
rep(i, n)ok[i] &= ok2[i];
deb(ok);
}
rep(i, n)rep(j, n)if(ok[i][j])return true;
return false;
};
int x = 1;
int y = 1;
rep(i, n)rep(j, n)y = max<short>(y, dist[i][j]);
while(x < y){
int m = (x + y) / 2;
if(check(m)){
y = m;
}else{
x = m + 1;
}
}
cout << x << "\n";
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int t; cin >> t;
while(t--)solve();
return 0;
}
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 101 102 103 104 105 106 107 108 109 | #include <bits/stdc++.h> using namespace std; #define fwd(i, a, n) for (int i = (a); i < (n); i++) #define rep(i, n) fwd(i, 0, n) #define all(X) X.begin(), X.end() #define sz(X) int(size(X)) #define pb push_back #define eb emplace_back #define st first #define nd second using pii = pair<int, int>; using vi = vector<int>; using ll = long long; using ld = long double; #ifdef LOC auto SS = signal(6, [](int) { *(int *)0 = 0; }); #define DTP(x, y) auto operator << (auto &o, auto a) -> decltype(y, o) { o << "("; x; return o << ")"; } DTP(o << a.st << ", " << a.nd, a.nd); DTP(for (auto i : a) o << i << ", ", all(a)); void dump(auto... x) { (( cerr << x << ", " ), ...) << '\n'; } #define deb(x...) cerr << setw(4) << __LINE__ << ":[" #x "]: ", dump(x) #else #define deb(...) 0 #endif const int INF = 1e4; const int N = 512; void solve(){ short n; cin >> n; vector<vector<short>> dist(n, vector<short>(n, INF)); rep(i, n){ string str; cin >> str; rep(j, n){ if(str[j] == '1')dist[i][j] = 1; } dist[i][i] = 0; } rep(k, n) rep(i, n) rep(j, n){ dist[i][j] = min<short>(dist[i][j], dist[i][k] + dist[k][j]); } deb(dist); vector<vector<short>> d(n, vector<short>(n+1, -INF)); vector<vector<short>> v(n, vector<short>(n+1)); rep(i, n){ vector<pair<short, short>> t(n); rep(j, n){ t[j] = {dist[i][j], j}; } sort(all(t)); rep(j, n){ d[i][j+1] = t[j].st; v[i][j+1] = t[j].nd; } } deb(d); deb(v); vector<bitset<N>> ok(n); vector<short> l(n, n); auto check = [&](short D){ deb("check", D); rep(i, n)ok[i].set(); rep(a, n){ vector<bitset<N>> ok2(n); rep(i, n)l[i] = n; fwd(b, a+1, n){ if(dist[a][b] <= D)continue; short j = n; rep(i, n){ while(d[a][j] + d[b][i+1] > D)j--; int ind = v[b][i+1]; l[ind] = min<short>(l[ind], j); } deb(b); deb(l); } deb(a, l); rep(i, n){ for(int j = 0; j < l[i]; j++){ ok2[i][v[a][j + 1]] = true; ok2[v[a][j + 1]][i] = true; } } rep(i, n)ok[i] &= ok2[i]; deb(ok); } rep(i, n)rep(j, n)if(ok[i][j])return true; return false; }; int x = 1; int y = 1; rep(i, n)rep(j, n)y = max<short>(y, dist[i][j]); while(x < y){ int m = (x + y) / 2; if(check(m)){ y = m; }else{ x = m + 1; } } cout << x << "\n"; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while(t--)solve(); return 0; } |
English