#include <bits/stdc++.h> using namespace std; const int MAX = 400; int N; bool G[MAX][MAX]; bool H[MAX][MAX]; int d[MAX][MAX]; int color[MAX]; void input(){ cin >> N; string s; for(int i = 0; i < N; i++){ cin >> s; for(int j = 0; j < N; j++){ if(s[j] == '0'){ G[i][j] = false; } else { G[i][j] = true; } d[i][j] = -1; } } } // here we use the assumption that the graph is connected! void find_distances(){ for(int i = 0; i < N; i++){ queue<pair<int, int> > Q; Q.push(make_pair(i, 0)); while(!Q.empty()){ int x = Q.front().first; int dix = Q.front().second; Q.pop(); if(d[i][x] != -1){ continue; } d[i][x] = dix; for(int j = 0; j < N; j++){ if(G[x][j]){ Q.push(make_pair(j, dix + 1)); } } } } } // this little function checks if the graph H is bipartite. // this is a necessary condition for things to work! bool is_bipartite(){ for(int i = 0; i < N; i++){ color[i] = -1; } queue<pair<int, int> > Q; for(int i = 0; i < N; i++){ if(color[i] == -1){ Q.push(make_pair(i, 0)); while(!Q.empty()){ int x = Q.front().first; int c = Q.front().second; Q.pop(); if(color[x] == -1){ color[x] = c; for(int j = 0; j < N; j++){ if(H[x][j]){ Q.push(make_pair(j, 1 - c)); } } } else if(color[x] != c){ return false; } } } } return true; } // this function checks if there exists a pair of vertices // (x, y) such that x != y and putting a teleport at x and y // diminishes the distance between any 2 vertices down to // at most K. bool is_valid(int K){ // all pairs of relevant vertices (a, b) s.t. d(a, b) > K. vector<pair<int, int> > V; for(int a = 0; a < N; a++){ H[a][a] = false; for(int b = a + 1; b < N; b++){ if(d[a][b] > K){ V.push_back(make_pair(a, b)); H[a][b] = true; H[b][a] = true; } else { H[a][b] = false; H[b][a] = false; } } } random_shuffle(V.begin(), V.end()); // if(!is_bipartite()) return false; // all pairs (x, y) vector<pair<int, int> > all_candidates; for(int i = 0; i < N; i++){ for(int j = i + 1; j < N; j++){ all_candidates.push_back(make_pair(i, j)); } } random_shuffle(all_candidates.begin(), all_candidates.end()); // the idea is relatively simple: // on average, we will reject all candidates rather "quickly" for(auto candidate = all_candidates.begin(); candidate != all_candidates.end(); candidate++){ int x = candidate->first; int y = candidate->second; bool WORKS = true; for(auto v = V.begin(); v != V.end(); v++){ if(d[v->first][x] + d[y][v->second] > K && d[v->first][y] + d[x][v->second] > K){ WORKS = false; break; } } if(WORKS){ // found a pair (x, y)!! return true; } } // did not find a pair (x, y) return false; } void find_answer(){ int max_d = 0; for(int i = 0; i < N; i++){ for(int j = 0; j < N; j++){ max_d = max(max_d, d[i][j]); } } int a = 1; int b = max_d; while(a < b){ int h = (a + b)/2; if(is_valid(h)){ b = h; } else { a = h + 1; } } cout << a << endl; } int main(){ srand(time(0)); cin.tie(NULL); ios_base::sync_with_stdio(0); int T; cin >> T; for(int v = 0; v < T; v++){ input(); find_distances(); find_answer(); } }
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 | #include <bits/stdc++.h> using namespace std; const int MAX = 400; int N; bool G[MAX][MAX]; bool H[MAX][MAX]; int d[MAX][MAX]; int color[MAX]; void input(){ cin >> N; string s; for(int i = 0; i < N; i++){ cin >> s; for(int j = 0; j < N; j++){ if(s[j] == '0'){ G[i][j] = false; } else { G[i][j] = true; } d[i][j] = -1; } } } // here we use the assumption that the graph is connected! void find_distances(){ for(int i = 0; i < N; i++){ queue<pair<int, int> > Q; Q.push(make_pair(i, 0)); while(!Q.empty()){ int x = Q.front().first; int dix = Q.front().second; Q.pop(); if(d[i][x] != -1){ continue; } d[i][x] = dix; for(int j = 0; j < N; j++){ if(G[x][j]){ Q.push(make_pair(j, dix + 1)); } } } } } // this little function checks if the graph H is bipartite. // this is a necessary condition for things to work! bool is_bipartite(){ for(int i = 0; i < N; i++){ color[i] = -1; } queue<pair<int, int> > Q; for(int i = 0; i < N; i++){ if(color[i] == -1){ Q.push(make_pair(i, 0)); while(!Q.empty()){ int x = Q.front().first; int c = Q.front().second; Q.pop(); if(color[x] == -1){ color[x] = c; for(int j = 0; j < N; j++){ if(H[x][j]){ Q.push(make_pair(j, 1 - c)); } } } else if(color[x] != c){ return false; } } } } return true; } // this function checks if there exists a pair of vertices // (x, y) such that x != y and putting a teleport at x and y // diminishes the distance between any 2 vertices down to // at most K. bool is_valid(int K){ // all pairs of relevant vertices (a, b) s.t. d(a, b) > K. vector<pair<int, int> > V; for(int a = 0; a < N; a++){ H[a][a] = false; for(int b = a + 1; b < N; b++){ if(d[a][b] > K){ V.push_back(make_pair(a, b)); H[a][b] = true; H[b][a] = true; } else { H[a][b] = false; H[b][a] = false; } } } random_shuffle(V.begin(), V.end()); // if(!is_bipartite()) return false; // all pairs (x, y) vector<pair<int, int> > all_candidates; for(int i = 0; i < N; i++){ for(int j = i + 1; j < N; j++){ all_candidates.push_back(make_pair(i, j)); } } random_shuffle(all_candidates.begin(), all_candidates.end()); // the idea is relatively simple: // on average, we will reject all candidates rather "quickly" for(auto candidate = all_candidates.begin(); candidate != all_candidates.end(); candidate++){ int x = candidate->first; int y = candidate->second; bool WORKS = true; for(auto v = V.begin(); v != V.end(); v++){ if(d[v->first][x] + d[y][v->second] > K && d[v->first][y] + d[x][v->second] > K){ WORKS = false; break; } } if(WORKS){ // found a pair (x, y)!! return true; } } // did not find a pair (x, y) return false; } void find_answer(){ int max_d = 0; for(int i = 0; i < N; i++){ for(int j = 0; j < N; j++){ max_d = max(max_d, d[i][j]); } } int a = 1; int b = max_d; while(a < b){ int h = (a + b)/2; if(is_valid(h)){ b = h; } else { a = h + 1; } } cout << a << endl; } int main(){ srand(time(0)); cin.tie(NULL); ios_base::sync_with_stdio(0); int T; cin >> T; for(int v = 0; v < T; v++){ input(); find_distances(); find_answer(); } } |