#include<bits/stdc++.h>
#define st first
#define nd second
#define all(x) x.begin(), x.end()
#define BOOST cin.tie(NULL); ios_base::sync_with_stdio(false);
// #define int ll
typedef long long ll;
using namespace std;
template <typename T> struct tag:reference_wrapper <T>{ using reference_wrapper <T>::reference_wrapper; };
template <typename T1, typename T2> static inline tag <ostream> operator<<(tag <ostream> os, pair<T1, T2> const& p){ return os.get()<<"{"<<p.first<<", "<<p.second<<"}", os;}
template <typename Other> static inline tag <ostream> operator<<(tag <ostream> os, Other const& o){ os.get()<<o; return os; }
template <typename T> static inline tag <ostream> operator <<(tag <ostream> os, vector <T> const& v){ os.get()<<"["; for (int i=0; i<v.size(); i++) if (i!=v.size()-1) os.get()<<v[i]<<", "; else os.get()<<v[i]; return os.get()<<"]", os; }
template <typename T> static inline tag <ostream> operator <<(tag <ostream> os, set <T> const& s){ vector <T> v; for (auto i: s) v.push_back(i); os.get()<<"["; for (int i=0; i<v.size(); i++) if (i!=v.size()-1) os.get()<<v[i]<<", "; else os.get()<<v[i]; return os.get()<<"]", os; }
int infi = 1000;
int dist[500][500];
pair<int,int> closest[500][500];
int acc[500][500];
int tmp_acc[500][500];
vector<pair<int,int>> good_edges;
void floyd_warshall(int n){
for(int k = 0; k < n; k++){
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(dist[i][j] > dist[i][k] + dist[k][j]){
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
}
}
void calc_closest(int n){
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
closest[i][j] = {dist[i][j], j};
}
sort(closest[i], closest[i] + n);
}
}
void find_good_edges(int idx, int tank, int n){
vector<int> ltrip;
for(int i = 0; i < n; i++){
if(dist[idx][i] > tank){
ltrip.push_back(i);
}
}
int dlim = infi;
int tts,tte;
for(int te = 0; te < n; te++){
dlim = infi;
for(int i : ltrip){
dlim = min(dlim, tank - dist[te][i]);
}
for(int i = 0; i < n; i++){
if(closest[idx][i].first <= dlim){
tts = closest[idx][i].second;
tte = te;
if(tts > tte) swap(tts,tte);
tmp_acc[tts][tte] = 1;
} else {
break;
}
}
}
for(int ts = 0; ts < n; ts++){
for(int te = ts; te < n; te++){
acc[ts][te] += tmp_acc[ts][te];
tmp_acc[ts][te] = 0;
}
}
}
bool check(int tank, int n){
for(int i = 0; i < n; i++){
for(int j = i; j < n; j++){
acc[i][j] = 0;
tmp_acc[i][j] = 0;
}
}
for(int i = 0; i < n; i++){
find_good_edges(i, tank, n);
}
for(int i = 0; i < n; i++){
for(int j = i; j < n; j++){
assert(acc[i][j] <= n);
if(acc[i][j] == n){
return true;
}
}
}
return false;
}
void solve(){
int n; cin >> n;
good_edges.resize(n * n);
string s;
for(int i = 0; i < n; i++){
cin >> s;
for(int j = 0; j < n; j++){
if(s[j] == '0'){
dist[i][j] = infi;
} else {
dist[i][j] = 1;
}
}
dist[i][i] = 0;
}
floyd_warshall(n);
calc_closest(n);
int p = -1, k = n, sr;
while(p + 1 < k){
sr = (p + k)/2;
if(check(sr, n)){
k = sr;
} else {
p = sr;
}
}
cout << k << "\n";
}
int32_t main(){
BOOST;
int q; cin >> q;
while(q--){
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 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 | #include<bits/stdc++.h> #define st first #define nd second #define all(x) x.begin(), x.end() #define BOOST cin.tie(NULL); ios_base::sync_with_stdio(false); // #define int ll typedef long long ll; using namespace std; template <typename T> struct tag:reference_wrapper <T>{ using reference_wrapper <T>::reference_wrapper; }; template <typename T1, typename T2> static inline tag <ostream> operator<<(tag <ostream> os, pair<T1, T2> const& p){ return os.get()<<"{"<<p.first<<", "<<p.second<<"}", os;} template <typename Other> static inline tag <ostream> operator<<(tag <ostream> os, Other const& o){ os.get()<<o; return os; } template <typename T> static inline tag <ostream> operator <<(tag <ostream> os, vector <T> const& v){ os.get()<<"["; for (int i=0; i<v.size(); i++) if (i!=v.size()-1) os.get()<<v[i]<<", "; else os.get()<<v[i]; return os.get()<<"]", os; } template <typename T> static inline tag <ostream> operator <<(tag <ostream> os, set <T> const& s){ vector <T> v; for (auto i: s) v.push_back(i); os.get()<<"["; for (int i=0; i<v.size(); i++) if (i!=v.size()-1) os.get()<<v[i]<<", "; else os.get()<<v[i]; return os.get()<<"]", os; } int infi = 1000; int dist[500][500]; pair<int,int> closest[500][500]; int acc[500][500]; int tmp_acc[500][500]; vector<pair<int,int>> good_edges; void floyd_warshall(int n){ for(int k = 0; k < n; k++){ for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ if(dist[i][j] > dist[i][k] + dist[k][j]){ dist[i][j] = dist[i][k] + dist[k][j]; } } } } } void calc_closest(int n){ for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ closest[i][j] = {dist[i][j], j}; } sort(closest[i], closest[i] + n); } } void find_good_edges(int idx, int tank, int n){ vector<int> ltrip; for(int i = 0; i < n; i++){ if(dist[idx][i] > tank){ ltrip.push_back(i); } } int dlim = infi; int tts,tte; for(int te = 0; te < n; te++){ dlim = infi; for(int i : ltrip){ dlim = min(dlim, tank - dist[te][i]); } for(int i = 0; i < n; i++){ if(closest[idx][i].first <= dlim){ tts = closest[idx][i].second; tte = te; if(tts > tte) swap(tts,tte); tmp_acc[tts][tte] = 1; } else { break; } } } for(int ts = 0; ts < n; ts++){ for(int te = ts; te < n; te++){ acc[ts][te] += tmp_acc[ts][te]; tmp_acc[ts][te] = 0; } } } bool check(int tank, int n){ for(int i = 0; i < n; i++){ for(int j = i; j < n; j++){ acc[i][j] = 0; tmp_acc[i][j] = 0; } } for(int i = 0; i < n; i++){ find_good_edges(i, tank, n); } for(int i = 0; i < n; i++){ for(int j = i; j < n; j++){ assert(acc[i][j] <= n); if(acc[i][j] == n){ return true; } } } return false; } void solve(){ int n; cin >> n; good_edges.resize(n * n); string s; for(int i = 0; i < n; i++){ cin >> s; for(int j = 0; j < n; j++){ if(s[j] == '0'){ dist[i][j] = infi; } else { dist[i][j] = 1; } } dist[i][i] = 0; } floyd_warshall(n); calc_closest(n); int p = -1, k = n, sr; while(p + 1 < k){ sr = (p + k)/2; if(check(sr, n)){ k = sr; } else { p = sr; } } cout << k << "\n"; } int32_t main(){ BOOST; int q; cin >> q; while(q--){ solve(); } return 0; } |
English