Advertisement
RaFiN_

cf -402E

Jul 7th, 2020
986
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.45 KB | None | 0 0
  1. /*
  2.  
  3.    In graph theoretic terms, b_ij > 0 in the matrix A^k means that there is a
  4.    path of k steps from i to j.  Now, because there is at least one non-zero
  5.    on the diagonal, it means that node represents a cycle of length 1.  So if
  6.    the graph has one strong component then eventually with a high enough power
  7.    you will e able to get from anywhere to anywhere in that number of steps.
  8.    Also, if the graph is not strongly connected, then there will be some b_ij
  9.    which always remain 0.
  10.  
  11.    So the property holds if and only if the graph has one big strong component.
  12. */
  13.  
  14. // 1-based
  15. namespace SCC {
  16.     vector<vector<int>> g, rg, scc, sccg;
  17.     vector<int> nodes, vis, who;
  18.     int comp, n;
  19.     void init(int n_) {
  20.         n = n_;
  21.         g.assign(n+1,{});
  22.         rg.assign(n+1,{});
  23.         vis.assign(n+1,0);
  24.         nodes.clear();
  25.         who.assign(n+1,0);
  26.         comp = 0;
  27.     }
  28.     void addEdge(int u, int v) {
  29.         g[u].push_back(v);
  30.         rg[v].push_back(u);
  31.     }
  32.     void dfs1(int u) {
  33.         vis[u] = 1;
  34.         for(int v : g[u]) {
  35.             if(!vis[v]) dfs1(v);
  36.         }
  37.         nodes.push_back(u);
  38.     }
  39.     void dfs2(int u) {
  40.         who[u] = comp;
  41.         for(int v: rg[u]) {
  42.             if (!who[v]) dfs2(v);
  43.         }
  44.     }
  45.     void SCC() {
  46.         for(int i = 1; i <= n; i++) {
  47.             if(!vis[i]) dfs1(i);
  48.         }
  49.         reverse(nodes.begin(), nodes.end());
  50.         for(int u: nodes) {
  51.             if (!who[u]) {
  52.                 ++comp;
  53.                 dfs2(u);
  54.             }
  55.         }
  56.         scc.assign(comp+1,{});
  57.         for(int i = 1; i <= n; i++) {
  58.             scc[who[i]].push_back(i);
  59.         }
  60.         sccg.assign(comp + 1,{});
  61.         for(int u = 1; u <= n; u++) {
  62.             for(int v : g[u]) {
  63.                 if (who[u] != who[v]) {
  64.                     sccg[who[u]].push_back(who[v]);
  65.                 }
  66.             }
  67.         }
  68.         for(int i = 1; i <= comp; i++) {
  69.             sort(sccg[i].begin(), sccg[i].end());
  70.             sccg[i].erase(unique(sccg[i].begin(), sccg[i].end()), sccg[i].end());
  71.         }
  72.     }
  73. }
  74.  
  75. int main() {
  76.     booster()
  77.     int n;
  78.     cin >> n;
  79.     SCC::init(n);
  80.  
  81.     for(int u = 1; u <= n; u++) {
  82.         for(int v = 1; v <= n; v++) {
  83.             int ej; cin >> ej;
  84.             if(ej) SCC::addEdge(u, v);
  85.         }
  86.     }
  87.     SCC::SCC();
  88.     if(SCC::comp == 1) {
  89.         cout << "YES";
  90.     }
  91.     else {
  92.         cout << "NO";
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement