Advertisement
Guest User

Untitled

a guest
Dec 14th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long LL;
  4. #define MAXN 500069
  5. bool isFine[MAXN];
  6. vector <vector <int> > SCC(MAXN);
  7. vector <int> whichSCC(MAXN, -1);
  8. int n, m, cnt, a, b;
  9.  
  10. void Answer()
  11. {
  12. int cnt = 0;
  13. for (int i=0; i!=n; ++i) if(isFine[i]) cnt++;
  14. cout << cnt << "\n";
  15. for (int i=0; i!=n; ++i) if (isFine[i]) cout << i+1 << " ";
  16. }
  17.  
  18. queue <pair<int,int>> Rebuild;
  19. stack <int> Order;
  20. void DFS_SCC1 (int node, vector <vector <int> >&G, vector <bool> &V)
  21. {
  22. V[node] = true;
  23. for (auto nei:G[node]) if (V[nei] == false) DFS_SCC1(nei, G, V);
  24. Order.push(node);
  25. }
  26.  
  27. int idx;
  28. void DFS_Create (int node, vector <vector <int>> &T, vector <bool> &V)
  29. {
  30. if (V[node]) return;
  31. V[node] = true;
  32. whichSCC[node] = idx;
  33. SCC[idx].push_back(node);
  34. for (auto nei:T[node]) DFS_Create(nei, T, V);
  35. }
  36.  
  37. inline void scan(int &number)
  38. {
  39. register int c;
  40. number = 0;
  41. c = getchar();
  42. for (; (c>47 && c<58); c=getchar()) number = number *10 + c - 48;
  43. }
  44.  
  45. void Proceed (vector <vector <int>> &Graph)
  46. {
  47. vector <int> degIn(idx, 0);
  48. for (auto node:Graph) for (auto nei:node) degIn[nei]++;
  49. queue <int> Q;
  50. for (int i=0; i!=n; i++) if (degIn[i] == 0) Q.push(i);
  51.  
  52. vector <int> TopOrd;
  53. while (Q.empty() == false)
  54. {
  55. int k = Q.size();
  56. while (k--)
  57. {
  58. int v = Q.front();
  59. Q.pop();
  60. TopOrd.push_back(v);
  61. for (auto nei:Graph[v])
  62. {
  63. degIn[nei]--;
  64. if (degIn[nei] == 0) Q.push(nei);
  65. }
  66. }
  67. }
  68. vector <int> Push(TopOrd.size());
  69. for (int i=0; i!=Push.size(); ++i) Push[TopOrd[i]] = i;
  70.  
  71. vector <vector <int> > topGraf(idx);
  72. for (int i=0; i!=Graph.size(); ++i)
  73. {
  74. for (auto nei:Graph[i])
  75. {
  76. //cout << Push[i]+1 << " -> " << Push[nei]+1 << endl;
  77. topGraf[Push[i]].push_back(Push[nei]);
  78. }
  79. }
  80.  
  81. vector <int> fR(idx, n+1);
  82. for (int i=0; i!=idx; ++i)
  83. {
  84. for (auto nei:topGraf[i]) fR[i] = min(fR[i], nei);
  85. }
  86.  
  87. stack <int> Res;
  88. for (int i=0; i!=idx; ++i)
  89. {
  90. cout << i << "-" << TopOrd[i] << "-" << fR[i] << endl;
  91. }
  92. }
  93.  
  94. int main()
  95. {
  96. ios_base::sync_with_stdio(0);
  97. cin.tie(0);
  98. cout.tie(0);
  99. for (int i=0; i!=MAXN; ++i) isFine[i] = true;
  100.  
  101. scan(n);
  102. scan(m);
  103. vector <vector <int> > Graph(n);
  104. vector <vector <int> > Trans(n);
  105.  
  106. while (m--)
  107. {
  108. scan(a);
  109. scan(b);
  110. a--,--b;
  111. Rebuild.push(make_pair(a,b));
  112. Graph[a].push_back(b);
  113. Trans[b].push_back(a);
  114. }
  115.  
  116. vector <bool> Vis(n,false);
  117. for (int i=0; i!=n; ++i) if (Vis[i] == false) DFS_SCC1(i, Graph, Vis);
  118. for (int i=0; i!=n; ++i) Vis[i] = false;
  119. idx = 0;
  120. while (Order.empty() == false)
  121. {
  122. int v = Order.top();
  123. Order.pop();
  124. if (Vis[v] == false)
  125. {
  126. DFS_Create(v, Trans, Vis);
  127. idx++;
  128. }
  129. }
  130.  
  131. for (int i=0; i!=n; ++i) Graph[i].clear(), Trans[i].clear();
  132. while (Rebuild.empty() == false)
  133. {
  134. a = Rebuild.front().first;
  135. b = Rebuild.front().second;
  136. Rebuild.pop();
  137. if (whichSCC[a] != whichSCC[b])
  138. {
  139. a = whichSCC[a];
  140. b = whichSCC[b];
  141. Graph[a].push_back(b);
  142. Trans[b].push_back(a);
  143. }
  144. }
  145. Proceed(Graph);
  146. Proceed(Trans);
  147.  
  148.  
  149.  
  150. // Answer();
  151. return 0;
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement