#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define deb(...)
#define DBP(...)
using namespace std;
using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;
#define pb push_back
#define mp make_pair
#define x first
#define y second
#define rep(i, b, e) for (int i = (b); i < (e); i++)
#define each(a, x) for (auto& a : (x))
#define all(x) (x).begin(), (x).end()
#define sz(x) int((x).size())
void run();
int main() {
cin.sync_with_stdio(0); cin.tie(0);
cout << fixed << setprecision(12);
run();
cout << flush; _Exit(0);
}
constexpr int MX = 405;
constexpr int INF = 1e8;
int n, lim;
int dist[MX][MX];
bitset<MX> masks[MX];
void updateMasks() {
rep(i, 0, n) {
masks[i].reset();
rep(j, 0, n) if (dist[i][j] > lim) masks[i].set(j);
}
}
void check(int t1, int t2) {
int tel[MX];
rep(i, 0, n) tel[i] = min(dist[t1][i], dist[t2][i]);
int cnt[MX];
fill(cnt, cnt+n+1, 0);
rep(i, 0, n) cnt[tel[i]]++;
rep(i, 1, n+1) cnt[i] += cnt[i-1];
int ord[MX];
rep(i, 0, n) ord[--cnt[tel[i]]] = i;
while (true) {
bitset<MX> bad;
int pos = n-1;
rep(i, 0, n) {
int v = ord[i];
while (pos >= 0 && tel[v]+tel[ord[pos]] > lim) {
bad.set(ord[pos--]);
}
if ((masks[v] & bad).any()) {
return;
}
}
lim--;
updateMasks();
}
}
void solve() {
string s;
cin >> n;
rep(i, 0, n) {
cin >> s;
rep(j, 0, n) {
if (i != j) {
dist[i][j] = (s[j] == '1' ? 1 : INF);
} else {
dist[i][j] = 0;
}
}
}
rep(k, 0, n) rep(i, 0, n) rep(j, 0, n) {
dist[i][j] = min(dist[i][j], dist[i][k]+dist[k][j]);
}
lim = n;
updateMasks();
rep(t1, 0, n) rep(t2, t1+1, n) check(t1, t2);
cout << lim+1 << '\n';
}
void run() {
int t; cin >> t;
while (t--) solve();
}
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 | #define _USE_MATH_DEFINES #include <bits/stdc++.h> #define deb(...) #define DBP(...) using namespace std; using ll = long long; using vi = vector<int>; using pii = pair<int, int>; #define pb push_back #define mp make_pair #define x first #define y second #define rep(i, b, e) for (int i = (b); i < (e); i++) #define each(a, x) for (auto& a : (x)) #define all(x) (x).begin(), (x).end() #define sz(x) int((x).size()) void run(); int main() { cin.sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(12); run(); cout << flush; _Exit(0); } constexpr int MX = 405; constexpr int INF = 1e8; int n, lim; int dist[MX][MX]; bitset<MX> masks[MX]; void updateMasks() { rep(i, 0, n) { masks[i].reset(); rep(j, 0, n) if (dist[i][j] > lim) masks[i].set(j); } } void check(int t1, int t2) { int tel[MX]; rep(i, 0, n) tel[i] = min(dist[t1][i], dist[t2][i]); int cnt[MX]; fill(cnt, cnt+n+1, 0); rep(i, 0, n) cnt[tel[i]]++; rep(i, 1, n+1) cnt[i] += cnt[i-1]; int ord[MX]; rep(i, 0, n) ord[--cnt[tel[i]]] = i; while (true) { bitset<MX> bad; int pos = n-1; rep(i, 0, n) { int v = ord[i]; while (pos >= 0 && tel[v]+tel[ord[pos]] > lim) { bad.set(ord[pos--]); } if ((masks[v] & bad).any()) { return; } } lim--; updateMasks(); } } void solve() { string s; cin >> n; rep(i, 0, n) { cin >> s; rep(j, 0, n) { if (i != j) { dist[i][j] = (s[j] == '1' ? 1 : INF); } else { dist[i][j] = 0; } } } rep(k, 0, n) rep(i, 0, n) rep(j, 0, n) { dist[i][j] = min(dist[i][j], dist[i][k]+dist[k][j]); } lim = n; updateMasks(); rep(t1, 0, n) rep(t2, t1+1, n) check(t1, t2); cout << lim+1 << '\n'; } void run() { int t; cin >> t; while (t--) solve(); } |
English