#include <bits/stdc++.h> using namespace std; #define FOR(i,n) for(int i=0; i < int(n); ++i) #define FO(i,a,b) for(int i=int(a); i<int(b); ++i) typedef long long ll; typedef pair<int,int> pii; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<pii> vpii; #define siz size()*1LL #define fi first #define se second #define ASS assert #define remin(a,b) a=min(a,b) #define remax(a,b) a=max(a,b) #define ALL(c) (c).begin(), (c).end() #define NL '\n' #define D if(0) #define cerr if(0)cerr const int N = 1009; bitset<N> range[N]; int n,m; vector<vector<pii>> nie; vector<bool> nie_deleted(10*N); vvi tos; namespace dfs1{ bool change = true; vector<int> vis; void dfs(int v, int parent){ if(vis[v]==1){ cerr << "cycle in dfs1" << NL; cout << "NIE" << NL; exit(0); } //D cerr << "visiting " << v << " from " << parent << NL; // visit if(!vis[v]){ vis[v]=1; for(auto dest : tos[v]){ dfs(dest,v); } FOR(idest, nie[v].size()){ if(nie_deleted[nie[v][idest].se]){ nie[v][idest] = nie[v].back(); nie[v].pop_back(); --idest; continue; } auto dest = nie[v][idest].fi; bool common_range = (range[v] & range[dest]).count() > 0; if(common_range){ change = true; nie_deleted[nie[v][idest].se] = 1; nie[v][idest] = nie[v].back(); nie[v].pop_back(); --idest; tos[v].push_back(dest); dfs(dest,v); } } vis[v]=2; } // return if(parent != -1){ range[parent] |= range[v]; nie[parent].insert(nie[parent].end(), ALL(nie[v])); //D{ // cerr << "returning from " << v << " -> " << parent << NL; //} } } void run(){ change = false; vis.clear(); vis.resize(n); // shuffle :) FOR(i,n) random_shuffle(ALL(tos[i])); FOR(i,n) random_shuffle(ALL(nie[i])); //vi indeg(n); //FOR(v,n) for(auto dest : tos[v]) indeg[dest]++; FOR(i,n) // if(indeg[i] == 0) dfs(i,-1); } } vvi tosi; vi topo_order; namespace topo_dfs{ int ctopo; vector<bool> vis; void dfs(int v){ if(vis[v]) return; vis[v] = 1; for(auto dest : tosi[v]) dfs(dest); topo_order[v] = ctopo--; } void run(){ ctopo = n-1; topo_order.resize(n); vis.clear(); vis.resize(n); FOR(i,n) dfs(i); } } namespace flatten{ int last_topo; vector<int> vis; int cvis = 1; int min_topo; int min_topo_arg; int ch; void dfs(int v){ if(vis[v] == cvis) return; if(vis[v]){ if(topo_order[v] < min_topo){ min_topo = topo_order[v]; min_topo_arg = v; } return; } vis[v] = cvis; for(auto dest : tosi[v]){ dfs(dest); } tosi[v].clear(); if(last_topo == -1){ ch = v; } else{ tosi[v].push_back(last_topo); } last_topo = v; } void run(int s){ if(vis[s]) return; cerr << "flatten from " << s << NL; cvis++; min_topo = INT_MAX; min_topo_arg = -1; last_topo = -1; dfs(s); tosi[ch].push_back(min_topo_arg); } } struct UF{ vector<int> uf; UF(int n) : uf(n){ FOR(i,n) uf[i] = i; } int find(int a){ if(a == uf[a]) return a; return uf[a] = find(uf[a]); } void unio(int a, int b){ uf[find(a)] = find(b); } }; int main(){ ios_base::sync_with_stdio(0); cin >> n >> m; tos.resize(n); nie.resize(n); FOR(i,m){ int a,b; string s; cin >> a >> b >> s; --a; --b; if(s=="T") tos[b].push_back(a); else{ nie[a].push_back({b,i}); } } FOR(i,n) range[i][i] = 1; int phases = 0; int no_change = 0; while(no_change < 2){ dfs1::run(); if(!dfs1::change) ++no_change; else no_change = 0; ++phases; } cerr << "TOTAL PHASES: " << phases << NL; // inverse graph tosi.resize(n); FOR(v,tos.size()) FOR(j,tos[v].size()){ auto dest = tos[v][j]; tosi[dest].push_back(v); } // policz skladowe UF uf(n); FOR(v,n) for(auto dest : tosi[v]) uf.unio(v,dest); { cerr << "union find:" << NL; FOR(i,n) cerr << " " << i << " -> " << uf.find(i) << NL; } // ile skladowych? int num_skladowe = 0; { vi v(n); FOR(i,n) v[i] = uf.find(i); sort(ALL(v)); auto last = unique(ALL(v)); num_skladowe = last-v.begin(); } cerr << "num_skladowe: " << num_skladowe << NL; int nie_tab[N][N]; FOR(i,N) FOR(j,N) nie_tab[i][j] = 0; FOR(v,n) for(auto p : nie[v]){ nie_tab[v][p.fi] = 1; cerr << " NIE " << v << " " << p.fi << NL; } // dla kazdego wierzcholka: lista skladowych, ktorych moze on byc ojcem int root = -1; FOR(i,n){ if(tosi[i].size() != 0) continue; bool ok = true; FOR(j,n){ if(uf.find(i) == uf.find(j)) continue; if(nie_tab[j][i] == 1){ ok = false; break; } } if(ok){ root = i; break; } } cerr << "root " << root << NL; if(root == -1){ cout << "NIE" << NL; exit(0); } FOR(i,n){ if(i == root) continue; if(tosi[i].size() == 0){ tosi[i].push_back(root); cerr << "adding edge to the root: " << i << " -> " << root << NL; } } // check if graph is now connected D{ FOR(v,n) for(auto dest : tosi[v]) uf.unio(v,dest); // ile skladowych? int ns = 0; { vi v(n); FOR(i,n) v[i] = uf.find(i); sort(ALL(v)); auto last = unique(ALL(v)); ns = last-v.begin(); } assert(ns == 1); } // global topo order topo_dfs::run(); // flatten flatten::vis.clear(); flatten::vis.resize(n); FOR(i,n){ flatten::run(i); } D FOR(i,n) assert(tosi[i].size() == 1); //cout << "TAK" << NL; //if(0) { FOR(i,n) cout << tosi[i][0]+1 << NL; cout << NL; } // check if root is root D assert(tosi[root][0] == -1); // check if exactly one 0 D{ int num0 = 0; FOR(i,n) if(tosi[i][0] == -1) ++num0; assert(num0 == 1); } 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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 | #include <bits/stdc++.h> using namespace std; #define FOR(i,n) for(int i=0; i < int(n); ++i) #define FO(i,a,b) for(int i=int(a); i<int(b); ++i) typedef long long ll; typedef pair<int,int> pii; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<pii> vpii; #define siz size()*1LL #define fi first #define se second #define ASS assert #define remin(a,b) a=min(a,b) #define remax(a,b) a=max(a,b) #define ALL(c) (c).begin(), (c).end() #define NL '\n' #define D if(0) #define cerr if(0)cerr const int N = 1009; bitset<N> range[N]; int n,m; vector<vector<pii>> nie; vector<bool> nie_deleted(10*N); vvi tos; namespace dfs1{ bool change = true; vector<int> vis; void dfs(int v, int parent){ if(vis[v]==1){ cerr << "cycle in dfs1" << NL; cout << "NIE" << NL; exit(0); } //D cerr << "visiting " << v << " from " << parent << NL; // visit if(!vis[v]){ vis[v]=1; for(auto dest : tos[v]){ dfs(dest,v); } FOR(idest, nie[v].size()){ if(nie_deleted[nie[v][idest].se]){ nie[v][idest] = nie[v].back(); nie[v].pop_back(); --idest; continue; } auto dest = nie[v][idest].fi; bool common_range = (range[v] & range[dest]).count() > 0; if(common_range){ change = true; nie_deleted[nie[v][idest].se] = 1; nie[v][idest] = nie[v].back(); nie[v].pop_back(); --idest; tos[v].push_back(dest); dfs(dest,v); } } vis[v]=2; } // return if(parent != -1){ range[parent] |= range[v]; nie[parent].insert(nie[parent].end(), ALL(nie[v])); //D{ // cerr << "returning from " << v << " -> " << parent << NL; //} } } void run(){ change = false; vis.clear(); vis.resize(n); // shuffle :) FOR(i,n) random_shuffle(ALL(tos[i])); FOR(i,n) random_shuffle(ALL(nie[i])); //vi indeg(n); //FOR(v,n) for(auto dest : tos[v]) indeg[dest]++; FOR(i,n) // if(indeg[i] == 0) dfs(i,-1); } } vvi tosi; vi topo_order; namespace topo_dfs{ int ctopo; vector<bool> vis; void dfs(int v){ if(vis[v]) return; vis[v] = 1; for(auto dest : tosi[v]) dfs(dest); topo_order[v] = ctopo--; } void run(){ ctopo = n-1; topo_order.resize(n); vis.clear(); vis.resize(n); FOR(i,n) dfs(i); } } namespace flatten{ int last_topo; vector<int> vis; int cvis = 1; int min_topo; int min_topo_arg; int ch; void dfs(int v){ if(vis[v] == cvis) return; if(vis[v]){ if(topo_order[v] < min_topo){ min_topo = topo_order[v]; min_topo_arg = v; } return; } vis[v] = cvis; for(auto dest : tosi[v]){ dfs(dest); } tosi[v].clear(); if(last_topo == -1){ ch = v; } else{ tosi[v].push_back(last_topo); } last_topo = v; } void run(int s){ if(vis[s]) return; cerr << "flatten from " << s << NL; cvis++; min_topo = INT_MAX; min_topo_arg = -1; last_topo = -1; dfs(s); tosi[ch].push_back(min_topo_arg); } } struct UF{ vector<int> uf; UF(int n) : uf(n){ FOR(i,n) uf[i] = i; } int find(int a){ if(a == uf[a]) return a; return uf[a] = find(uf[a]); } void unio(int a, int b){ uf[find(a)] = find(b); } }; int main(){ ios_base::sync_with_stdio(0); cin >> n >> m; tos.resize(n); nie.resize(n); FOR(i,m){ int a,b; string s; cin >> a >> b >> s; --a; --b; if(s=="T") tos[b].push_back(a); else{ nie[a].push_back({b,i}); } } FOR(i,n) range[i][i] = 1; int phases = 0; int no_change = 0; while(no_change < 2){ dfs1::run(); if(!dfs1::change) ++no_change; else no_change = 0; ++phases; } cerr << "TOTAL PHASES: " << phases << NL; // inverse graph tosi.resize(n); FOR(v,tos.size()) FOR(j,tos[v].size()){ auto dest = tos[v][j]; tosi[dest].push_back(v); } // policz skladowe UF uf(n); FOR(v,n) for(auto dest : tosi[v]) uf.unio(v,dest); { cerr << "union find:" << NL; FOR(i,n) cerr << " " << i << " -> " << uf.find(i) << NL; } // ile skladowych? int num_skladowe = 0; { vi v(n); FOR(i,n) v[i] = uf.find(i); sort(ALL(v)); auto last = unique(ALL(v)); num_skladowe = last-v.begin(); } cerr << "num_skladowe: " << num_skladowe << NL; int nie_tab[N][N]; FOR(i,N) FOR(j,N) nie_tab[i][j] = 0; FOR(v,n) for(auto p : nie[v]){ nie_tab[v][p.fi] = 1; cerr << " NIE " << v << " " << p.fi << NL; } // dla kazdego wierzcholka: lista skladowych, ktorych moze on byc ojcem int root = -1; FOR(i,n){ if(tosi[i].size() != 0) continue; bool ok = true; FOR(j,n){ if(uf.find(i) == uf.find(j)) continue; if(nie_tab[j][i] == 1){ ok = false; break; } } if(ok){ root = i; break; } } cerr << "root " << root << NL; if(root == -1){ cout << "NIE" << NL; exit(0); } FOR(i,n){ if(i == root) continue; if(tosi[i].size() == 0){ tosi[i].push_back(root); cerr << "adding edge to the root: " << i << " -> " << root << NL; } } // check if graph is now connected D{ FOR(v,n) for(auto dest : tosi[v]) uf.unio(v,dest); // ile skladowych? int ns = 0; { vi v(n); FOR(i,n) v[i] = uf.find(i); sort(ALL(v)); auto last = unique(ALL(v)); ns = last-v.begin(); } assert(ns == 1); } // global topo order topo_dfs::run(); // flatten flatten::vis.clear(); flatten::vis.resize(n); FOR(i,n){ flatten::run(i); } D FOR(i,n) assert(tosi[i].size() == 1); //cout << "TAK" << NL; //if(0) { FOR(i,n) cout << tosi[i][0]+1 << NL; cout << NL; } // check if root is root D assert(tosi[root][0] == -1); // check if exactly one 0 D{ int num0 = 0; FOR(i,n) if(tosi[i][0] == -1) ++num0; assert(num0 == 1); } return 0; } |