a53

ComponenteBiconexe_ANDU_PAD

a53
Feb 12th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. #include <fstream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <cstring>
  5. #include <stack>
  6. #include <ctime>
  7. #include <cstdlib>
  8.  
  9. using namespace std;
  10.  
  11. ifstream fin("componentebiconexe.in");
  12. ofstream fout("componentebiconexe.out");
  13.  
  14. const int dim = 100005;
  15.  
  16. vector<int> graph[dim];
  17. vector< pair<int, int> > critEdges;
  18. vector< vector<int> > biconex;
  19. int biconexCount;
  20.  
  21. bool critical[dim];
  22.  
  23. int level[dim], low[dim];
  24.  
  25. bool vis[dim];
  26.  
  27. stack<int> st;
  28.  
  29. void dfs(int node, int parent, int root) {
  30.  
  31. vis[node] = true;
  32. level[node] = low[node] = level[parent] + 1;
  33.  
  34. st.push(node);
  35.  
  36. int sonCount = 0;
  37.  
  38. for (vector<int>::iterator adj = graph[node].begin(); adj != graph[node].end(); ++adj) {
  39.  
  40. if (*adj == parent)
  41. continue;
  42.  
  43. if (vis[*adj]) {
  44. low[node] = min(low[node], level[*adj]);
  45. continue;
  46. }
  47.  
  48. ++sonCount;
  49.  
  50. dfs(*adj, node, root);
  51. low[node] = min(low[node], low[*adj]);
  52.  
  53. if (level[node] < low[*adj])
  54. critEdges.push_back((make_pair(node, *adj)));
  55.  
  56. if (level[node] <= low[*adj]) {
  57.  
  58. ++biconexCount;
  59. biconex.push_back(vector<int>(0));
  60.  
  61. int x;
  62. do {
  63.  
  64. x = st.top();
  65. st.pop();
  66. biconex[biconexCount - 1].push_back(x);
  67.  
  68.  
  69. } while (x != *adj);
  70.  
  71. biconex[biconexCount - 1].push_back(node);
  72.  
  73. if (node != root)
  74. critical[node] = true;
  75.  
  76. }
  77.  
  78. }
  79.  
  80. if (node == root && sonCount > 1)
  81. critical[node] = true;
  82.  
  83. }
  84.  
  85. int main() {
  86.  
  87. int p;
  88. fin >> p;
  89.  
  90. int n, m;
  91. fin >> n >> m;
  92.  
  93. for (int i = 1; i <= m; ++i) {
  94.  
  95. int x, y;
  96. fin >> x >> y;
  97.  
  98. graph[x].push_back(y);
  99. graph[y].push_back(x);
  100.  
  101. }
  102.  
  103. for (int i = 1; i <= n; ++i)
  104. if (!vis[i])
  105. dfs(i, 0, i);
  106.  
  107. if (p == 1) {
  108.  
  109. fout << biconexCount << '\n';
  110.  
  111. for (int i = 0; i < biconexCount; ++i) {
  112. fout << biconex[i].size() << ' ';
  113. for (unsigned int j = 0; j < biconex[i].size(); ++j) {
  114. fout << biconex[i][j] << ' ';
  115. }
  116. fout << '\n';
  117. }
  118.  
  119. }
  120. else if (p == 2) {
  121.  
  122. int cnt = 0;
  123. for (int i = 1; i <= n; ++i)
  124. if (critical[i])
  125. ++cnt;
  126.  
  127. fout << cnt << '\n';
  128. for (int i = 1; i <= n; ++i)
  129. if (critical[i])
  130. fout << i << ' ';
  131. fout << '\n';
  132.  
  133. }
  134. else {
  135.  
  136. fout << critEdges.size() << '\n';
  137. for (unsigned int i = 0; i < critEdges.size(); ++i)
  138. fout << critEdges[i].first << ' ' << critEdges[i].second << '\n';
  139.  
  140. }
  141.  
  142. return 0;
  143.  
  144. }
Add Comment
Please, Sign In to add comment