#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define reunique(v) v.resize(std::unique(v.begin(), v.end()) - v.begin())
#define sz(v) ((int)(v).size())
#define vec1d(x) vector<x>
#define vec2d(x) vector<vec1d(x)>
#define vec3d(x) vector<vec2d(x)>
#define vec4d(x) vector<vec3d(x)>
#define ivec1d(x, n, v) vec1d(x)(n, v)
#define ivec2d(x, n, m, v) vec2d(x)(n, ivec1d(x, m, v))
#define ivec3d(x, n, m, k, v) vec3d(x)(n, ivec2d(x, m, k, v))
#define ivec4d(x, n, m, k, l, v) vec4d(x)(n, ivec3d(x, m, k, l, v))
#define nl "\n"
#ifdef LOCAL
#include "pretty_print.hpp"
#define dbg(...) debug_out(__LINE__, #__VA_ARGS__, __VA_ARGS__)
#else
#define dbg(...) 42
#endif
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
template <typename T> T sqr(T x) { return x * x; }
template <typename T> T abs(T x) { return x < 0? -x : x; }
template <typename T> T gcd(T a, T b) { return b? gcd(b, a % b) : a; }
template <typename T> bool chmin(T &x, const T& y) { if (x > y) { x = y; return true; } return false; }
template <typename T> bool chmax(T &x, const T& y) { if (x < y) { x = y; return true; } return false; }
auto random_address = [] { char *p = new char; return (uint64_t) p; };
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count() * (random_address() | 1));
mt19937_64 rngll(chrono::steady_clock::now().time_since_epoch().count() * (random_address() | 1));
int main(int /* argc */, const char** /* argv */)
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
auto a = ivec2d(int, n, n, 0);
auto b = ivec1d(int, n, 0);
const int inf = 1e9 + 42;
for (int i = 0; i < n; ++i) {
string s;
cin >> s;
for (int j = 0; j < n; ++j) {
if (i == j) {
a[i][j] = 0;
} else if (s[j] == '1') {
a[i][j] = 1;
} else {
a[i][j] = inf;
}
}
}
// a = ivec2d(int, n, n, inf);
// for (int i = 0; i < n; ++i) {
// a[i][i] = 0;
// }
// for (int i = 0; i + 1 < 100; ++i) {
// a[i][i + 1] = 1;
// a[i + 1][i] = 1;
// }
// for (int i = 0; i < 150; ++i) {
// a[0][i + 100] = 1;
// a[i + 100][0] = 1;
// a[99][i + 250] = 1;
// a[i + 250][99] = 1;
// }
for (int k = 0; k < n; ++k) {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
chmin(a[i][j], a[i][k] + a[k][j]);
}
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
chmax(b[i], a[i][j]);
}
}
int l = 0;
int r = 0;
for (int i = 0; i < n; ++i) {
chmax(r, b[i]);
}
vector<int> c1(n);
iota(all(c1), 0);
shuffle(all(c1), rng);
vector<int> c2(n);
iota(all(c2), 0);
shuffle(all(c2), rng);
vector<pii> b12;
for (int b1 : c1) {
for (int b2 : c2) {
if (b1 < b2) {
b12.emplace_back(b1, b2);
}
}
}
while (l < r) {
int m = (l + r) / 2;
auto f = ivec1d(int, n, 0);
bool ok = true;
queue<int> q;
auto add = [&](int x, int c) {
if (f[x]) {
if (f[x] != c) {
ok = false;
}
return;
}
f[x] = c;
q.push(x);
};
vector<int> w(n, 0);
for (int i = 0; i < n; ++i) {
if (f[i] || b[i] <= m) {
continue;
}
add(i, 1);
while (!q.empty()) {
int x = q.front();
q.pop();
for (int j = 0; j < n; ++j) {
if (a[x][j] > m) {
++w[x];
add(j, f[x] ^ 3);
}
}
}
}
if (!ok) {
l = m + 1;
continue;
}
vector<int> a1;
vector<int> a2;
for (int i = 0; i < n; ++i) {
if (f[i] == 1) {
a1.push_back(i);
} else if (f[i] == 2) {
a2.push_back(i);
}
}
shuffle(all(a1), rng);
shuffle(all(a2), rng);
auto check_bases = [&](int b1, int b2) {
for (int x1 : a1) {
for (int x2 : a2) {
if (a[x1][x2] <= m) {
continue;
}
if (a[b1][x1] + a[b2][x2] <= m) {
continue;
}
if (a[b1][x2] + a[b2][x1] <= m) {
continue;
}
return false;
}
}
return true;
};
auto found_bases = [&]() {
for (auto [b1, b2] : b12) {
if (check_bases(b1, b2)) {
return true;
}
}
return false;
};
ok = found_bases();
if (ok) {
r = m;
} else {
l = m + 1;
}
}
cout << r << nl;
}
#ifdef LOCAL
cerr << "Time execute: " << clock() / (double)CLOCKS_PER_SEC << " sec" << endl;
#endif
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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | #include <bits/stdc++.h> using namespace std; #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define reunique(v) v.resize(std::unique(v.begin(), v.end()) - v.begin()) #define sz(v) ((int)(v).size()) #define vec1d(x) vector<x> #define vec2d(x) vector<vec1d(x)> #define vec3d(x) vector<vec2d(x)> #define vec4d(x) vector<vec3d(x)> #define ivec1d(x, n, v) vec1d(x)(n, v) #define ivec2d(x, n, m, v) vec2d(x)(n, ivec1d(x, m, v)) #define ivec3d(x, n, m, k, v) vec3d(x)(n, ivec2d(x, m, k, v)) #define ivec4d(x, n, m, k, l, v) vec4d(x)(n, ivec3d(x, m, k, l, v)) #define nl "\n" #ifdef LOCAL #include "pretty_print.hpp" #define dbg(...) debug_out(__LINE__, #__VA_ARGS__, __VA_ARGS__) #else #define dbg(...) 42 #endif typedef long double ld; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii; template <typename T> T sqr(T x) { return x * x; } template <typename T> T abs(T x) { return x < 0? -x : x; } template <typename T> T gcd(T a, T b) { return b? gcd(b, a % b) : a; } template <typename T> bool chmin(T &x, const T& y) { if (x > y) { x = y; return true; } return false; } template <typename T> bool chmax(T &x, const T& y) { if (x < y) { x = y; return true; } return false; } auto random_address = [] { char *p = new char; return (uint64_t) p; }; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count() * (random_address() | 1)); mt19937_64 rngll(chrono::steady_clock::now().time_since_epoch().count() * (random_address() | 1)); int main(int /* argc */, const char** /* argv */) { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) { int n; cin >> n; auto a = ivec2d(int, n, n, 0); auto b = ivec1d(int, n, 0); const int inf = 1e9 + 42; for (int i = 0; i < n; ++i) { string s; cin >> s; for (int j = 0; j < n; ++j) { if (i == j) { a[i][j] = 0; } else if (s[j] == '1') { a[i][j] = 1; } else { a[i][j] = inf; } } } // a = ivec2d(int, n, n, inf); // for (int i = 0; i < n; ++i) { // a[i][i] = 0; // } // for (int i = 0; i + 1 < 100; ++i) { // a[i][i + 1] = 1; // a[i + 1][i] = 1; // } // for (int i = 0; i < 150; ++i) { // a[0][i + 100] = 1; // a[i + 100][0] = 1; // a[99][i + 250] = 1; // a[i + 250][99] = 1; // } for (int k = 0; k < n; ++k) { for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { chmin(a[i][j], a[i][k] + a[k][j]); } } } for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { chmax(b[i], a[i][j]); } } int l = 0; int r = 0; for (int i = 0; i < n; ++i) { chmax(r, b[i]); } vector<int> c1(n); iota(all(c1), 0); shuffle(all(c1), rng); vector<int> c2(n); iota(all(c2), 0); shuffle(all(c2), rng); vector<pii> b12; for (int b1 : c1) { for (int b2 : c2) { if (b1 < b2) { b12.emplace_back(b1, b2); } } } while (l < r) { int m = (l + r) / 2; auto f = ivec1d(int, n, 0); bool ok = true; queue<int> q; auto add = [&](int x, int c) { if (f[x]) { if (f[x] != c) { ok = false; } return; } f[x] = c; q.push(x); }; vector<int> w(n, 0); for (int i = 0; i < n; ++i) { if (f[i] || b[i] <= m) { continue; } add(i, 1); while (!q.empty()) { int x = q.front(); q.pop(); for (int j = 0; j < n; ++j) { if (a[x][j] > m) { ++w[x]; add(j, f[x] ^ 3); } } } } if (!ok) { l = m + 1; continue; } vector<int> a1; vector<int> a2; for (int i = 0; i < n; ++i) { if (f[i] == 1) { a1.push_back(i); } else if (f[i] == 2) { a2.push_back(i); } } shuffle(all(a1), rng); shuffle(all(a2), rng); auto check_bases = [&](int b1, int b2) { for (int x1 : a1) { for (int x2 : a2) { if (a[x1][x2] <= m) { continue; } if (a[b1][x1] + a[b2][x2] <= m) { continue; } if (a[b1][x2] + a[b2][x1] <= m) { continue; } return false; } } return true; }; auto found_bases = [&]() { for (auto [b1, b2] : b12) { if (check_bases(b1, b2)) { return true; } } return false; }; ok = found_bases(); if (ok) { r = m; } else { l = m + 1; } } cout << r << nl; } #ifdef LOCAL cerr << "Time execute: " << clock() / (double)CLOCKS_PER_SEC << " sec" << endl; #endif return 0; } |
English