Korotkodul

ЗОШ мосты А OK

Jan 9th, 2022 (edited)
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4. #include <queue>
  5. #include <algorithm>
  6. #include <string>
  7. #include <stack>
  8. #include <set>
  9. #include <map>
  10. #define pii pair <int,int>
  11. using namespace std;
  12. using ll = long long;
  13. using ld = long double;
  14. void cv(vector <int> &v){
  15. for (auto x: v) cout<<x<<' ';
  16. cout<<"\n";
  17. }
  18.  
  19. void cvl(vector <ll> &v){
  20. for (auto x: v) cout<<x<<' ';
  21. cout<<"\n";
  22. }
  23.  
  24.  
  25. void cvv(vector <vector <int> > &v){
  26. for (auto x: v) cv(x);
  27. cout<<"\n";
  28. }
  29.  
  30. struct edge{
  31. int fr, to, id;
  32. };
  33. int n,m;
  34. vector <vector <edge> > G;
  35. const int bl = 2, gr = 1, wh = 0, inf = 2e9;
  36. vector <int> clr;
  37.  
  38. void she(edge x){
  39. cout<<x.fr<<' '<<x.to<<' '<<x.id<<"\n";
  40. }
  41.  
  42. void shG(){
  43. for (int i=0;i<n;++i){
  44. cout<<"i = "<<i<<"\n";
  45. for (int j = 0; j <G[i].size();++j){she(G[i][j]);}
  46. cout<<"\n\n";
  47. }
  48. }
  49. int curT = 0;
  50. vector <edge> bridge; //id мостов
  51. stack <int> comp;
  52. vector <int> tIn, tUp;
  53.  
  54. void dfs(int v, int parId){//parId - id ребра по которомц пришли в v
  55. clr[v] = gr;
  56. tIn[v] = curT++;
  57. comp.push(v);
  58.  
  59. for (auto e: G[v]){
  60. if (e.id == parId) continue;
  61. else if (clr[e.to] == wh){
  62. dfs(e.to, e.id);
  63. tUp[v] = min(tUp[v], tUp[e.to]);
  64. if (tUp[e.to] >= tIn[e.to]){
  65. bridge.push_back(e);
  66. }
  67. }
  68. else if(clr[e.to] == gr){
  69. tUp[v] = min(tUp[v], tIn[e.to]);
  70. }
  71.  
  72. }
  73. clr[v] = bl;
  74. }
  75.  
  76. bool cmp(edge a, edge b){
  77. return a.id < b.id;
  78. }
  79.  
  80.  
  81. int main()
  82. {
  83. ios::sync_with_stdio(0);
  84. cin.tie(0);
  85. cout.tie(0);
  86. cin>>n>>m;
  87. G.resize(n);
  88. clr.assign(n, wh);
  89. tIn.assign(n, inf);
  90. tUp.assign(n, inf);
  91. int id = 0;
  92. for (int i=0;i<m;++i){
  93. int a,b;
  94. cin>>a>>b;
  95. if (a>b) swap(a,b);
  96. a--; b--;
  97. edge any;
  98. any = {a, b, id};
  99. G[a].push_back(any);
  100. any = {b, a, id};
  101. G[b].push_back(any);
  102. id++;
  103. }
  104. //shG();
  105. for (int i = 0; i < n;++i){
  106. if (clr[i] == wh){
  107. dfs(i, -1);
  108. }
  109. }
  110. sort(bridge.begin(), bridge.end(), cmp);
  111. cout<<bridge.size()<<"\n";
  112. for (auto i: bridge){
  113. cout<<i.id+1<<"\n";
  114. //she(i);
  115. }
  116. }
  117. /*
  118. 13 16
  119. 1 2
  120. 2 3
  121. 3 4
  122. 1 3
  123. 4 5
  124. 4 6
  125. 5 6
  126. 5 7
  127. 5 8
  128. 7 8
  129. 9 10
  130. 10 11
  131. 11 9
  132. 11 12
  133. 12 13
  134. 12 13
  135.  
  136. 13 15
  137. 1 2
  138. 2 3
  139. 3 4
  140. 1 3
  141. 4 5
  142. 4 6
  143. 5 6
  144. 5 7
  145. 5 8
  146. 7 8
  147. 9 10
  148. 10 11
  149. 11 9
  150. 11 12
  151. 12 13
  152.  
  153.  
  154. 13 15
  155. 1 2
  156. 2 3
  157. 3 4
  158. 1 3
  159. 4 5
  160. 4 6
  161. 5 6
  162. 5 7
  163. 5 8
  164. 7 8
  165. 9 10
  166. 10 11
  167. 11 9
  168. 11 12
  169. 12 13
  170.  
  171. 12 14
  172. 1 2
  173. 2 3
  174. 3 4
  175. 1 3
  176. 4 5
  177. 4 6
  178. 5 6
  179. 5 7
  180. 5 8
  181. 7 8
  182. 9 10
  183. 10 11
  184. 11 9
  185. 11 12
  186.  
  187.  
  188. 6 7
  189. 1 2
  190. 2 3
  191. 3 4
  192. 1 3
  193. 4 5
  194. 4 6
  195. 5 6
  196.  
  197.  
  198. 6 8
  199. 1 2
  200. 2 3
  201. 3 4
  202. 1 3
  203. 4 5
  204. 4 6
  205. 5 6
  206. 1 5
  207.  
  208. 8 10
  209. 1 2
  210. 2 3
  211. 3 4
  212. 1 3
  213. 4 5
  214. 4 6
  215. 5 6
  216. 5 7
  217. 5 8
  218. 7 8
  219.  
  220. 9 11
  221. 1 2
  222. 2 3
  223. 3 4
  224. 1 3
  225. 4 5
  226. 4 6
  227. 5 6
  228. 5 7
  229. 5 8
  230. 7 8
  231. 8 9
  232.  
  233. 10 11
  234. 1 2
  235. 2 3
  236. 3 4
  237. 1 3
  238. 4 5
  239. 4 6
  240. 5 6
  241. 5 7
  242. 5 8
  243. 7 8
  244. 9 10
  245.  
  246. 11 12
  247. 1 2
  248. 2 3
  249. 3 4
  250. 1 3
  251. 4 5
  252. 4 6
  253. 5 6
  254. 5 7
  255. 5 8
  256. 7 8
  257. 9 10
  258. 10 11
  259.  
  260. 11 13
  261. 1 2
  262. 2 3
  263. 3 4
  264. 1 3
  265. 4 5
  266. 4 6
  267. 5 6
  268. 5 7
  269. 5 8
  270. 7 8
  271. 9 10
  272. 10 11
  273. 11 9
  274. */
  275.  
Add Comment
Please, Sign In to add comment