Madiyar

acm.mipt.ru 098s

Feb 11th, 2012
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.39 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <cstdlib>
  5. #include <vector>
  6. #include <set>
  7. #include <map>
  8. #include <cassert>
  9. #include <ctime>
  10. #include <cmath>
  11. #include <string>
  12. #include <cstring>
  13. #include <queue>
  14.  
  15. using namespace std;
  16.  
  17. #define f first
  18. #define s second
  19. #define mp make_pair
  20. #define pb push_back
  21. #define forit(it,v) for (typeof(v.begin()) it = v.begin(); it != v.end(); ++it)
  22. const int maxm = 200001;
  23. const int maxn = 20001;
  24. vector<int> g[maxn], gi[maxn], order;
  25. int comp[maxn], used[maxn], L[maxm], R[maxm], ver[maxn], in[maxn], out[maxn], cc, n, m;
  26.  
  27.  
  28. void Dfs1(int v) {
  29.  
  30.     comp[v] = true;
  31.  
  32.     forit(it, g[v]) {
  33.         int to = *it;
  34.         if (!comp[to]) Dfs1(to);
  35.     }
  36.  
  37.     order.pb(v);
  38. }
  39.  
  40. void Dfs2(int v) {
  41.    
  42.     comp[v] = cc;
  43.     ver[cc] = v;
  44.  
  45.     forit(it, gi[v]) {
  46.         int to = *it;
  47.         if (!comp[to]) Dfs2(to);
  48.     }
  49. }
  50.  
  51. void Dfs3(int v) {
  52.    
  53.     tin[v] = ++timer;
  54.  
  55.     int cnt = 0;       
  56.     forit(it, g2[v]) {
  57.         int to = *it;
  58.         if (!used[to]) {
  59.             Dfs3(to);
  60.             cnt++;
  61.         } else
  62.         if (used[to] == 2){
  63.             sl[tin[to]] = -1;
  64.             sl[tin[out]] = -2;
  65.         }
  66.     }
  67.  
  68.     if (cnt == 0) {
  69.         T.pb(v);
  70.  
  71.     }
  72.  
  73.     tout[v] = ++timer;
  74. }
  75.  
  76. int main() {
  77.     scanf("%d%d", &n, &m);
  78.    
  79.     for (int i = 0; i < m; ++i) {
  80.         int u, v;
  81.         scanf("%d%d", &u, &v);     
  82.         g[u].pb(v);
  83.         gi[v].pb(u);
  84.         L[i] = u; R[i] = v;
  85.     }
  86.  
  87.     for (int i = 0; i < n; ++i)
  88.         if (!comp[i]) Dfs1(i);
  89.  
  90.     memset(comp, 0, sizeof(comp));
  91.     for (int i = (int)order.size() - 1; i >= 0; --i)
  92.         if (!comp[order[i]]) {
  93.             cc++;
  94.             Dfs2(order[i]);
  95.         }
  96.  
  97.     if (cc == 1) {
  98.         puts("0");
  99.         return 0;
  100.     }
  101.    
  102.     for (int i = 0; i < m; ++i) {
  103.         int u = L[i], v = R[i];
  104.  
  105.         if (comp[u] != comp[v]) {
  106.             g2[comp[u]].pb(comp[v]);
  107.             in[comp[v]] = true;
  108.             out[comp[u]] = true;
  109.         }
  110.     }
  111.    
  112.  
  113.     vector<int>  S, T;
  114.  
  115.     for (int i = 1; i <= cc; ++i) {
  116.         if (!in[i]) S.pb(i);
  117.     }
  118.    
  119.     memset(used, 0, sizeof(used));
  120.  
  121.     for (int i = 0; i < S.size(); ++i) {       
  122.         memset(sl, 0, sizeof(sl));
  123.        
  124.         Dfs3(S[i]);
  125.        
  126.         sl[tin[S[i]]] = -1;
  127.         sl[tout[S[i]]] = -2;
  128.  
  129.         forit (it, T) {
  130.             if (!sl[tin[*it]])
  131.                 sl[tin[*it]] = *it;
  132.         }
  133.  
  134.         int op = 0;
  135.         for (int j = 1; j <= timer; ++j) {
  136.            
  137.             if (sl[j] == -1) op++;
  138.             if (sl[j] == -2) op--;
  139.  
  140.             if (sl[j] != 0 && op == 0) {
  141.                 ans.pb(mp(ver[sl[j]], ver[S[i]]));
  142.                
  143.                 in[S[i]] = true;
  144.                 out[sl[j]] = true;             
  145.                 break;
  146.             }
  147.         }
  148.                    
  149.  
  150.     }
  151.  
  152.  
  153.     return 0;
  154.    
  155.  
  156. }
Advertisement
Add Comment
Please, Sign In to add comment