Naxocist

chaos

Apr 29th, 2023
842
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.87 KB | Source Code | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. using pi = pair<int, int>;
  5. const int N = 2e5 + 3, M = 1e5 + 3;
  6. int dsu[M];
  7. pi edge[N];
  8. int group;
  9. stack<int> st;
  10.  
  11. int par(int u) {
  12.     return (dsu[u] == u ? u : dsu[u] = par(dsu[u]));
  13. }
  14.  
  15. void un(int u, int v) {
  16.     u = par(u), v = par(v);
  17.    
  18.     if(u == v) return ;
  19.    
  20.     dsu[u] = v;
  21.     group--;
  22. }
  23.  
  24. int main() {
  25.     int n, m; scanf("%d%d", &n, &m);
  26.     group = n;
  27.     for(int i=1; i<=n; ++i) dsu[i] = i;
  28.    
  29.     for(int i=1; i<=m; ++i) {
  30.         scanf("%d%d", &edge[i].first, &edge[i].second);
  31.     }
  32.    
  33.     vector<int> v(m);
  34.     for(int i=0; i<m; ++i) scanf("%d", &v[i]);
  35.    
  36.     for(int i=m-1; i>=0; --i) {
  37.         st.push(group);
  38.         un(edge[v[i]].first, edge[v[i]].second);
  39.     }
  40.    
  41.     while(st.size()) {
  42.         printf("%d\n", st.top());
  43.         st.pop();
  44.     }
  45.     return 0;
  46. }
  47.  
  48. /*
  49. 4 6
  50. 1 2
  51. 1 3
  52. 1 4
  53. 2 3
  54. 2 4
  55. 3 4
  56. 6
  57. 5
  58. 4
  59. 3
  60. 2
  61. 1
  62. */
Advertisement
Add Comment
Please, Sign In to add comment