Advertisement
rjlth

Untitled

Jan 8th, 2015
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.79 KB | None | 0 0
  1. //Zhandos Kapezov
  2. #include <iostream>
  3. #include <math.h>
  4. #include <cmath>
  5. #include <vector>
  6. #include <utility>
  7. #include <algorithm>
  8. #include <cstdio>
  9. #include <cstdlib>
  10. #include <fstream>
  11. #include <string>
  12. #include <string.h>
  13. #include <sstream>
  14. #include <map>
  15. #include <set>
  16. #include <stack>
  17. #include <queue>
  18. #include <deque>
  19. #include <limits>
  20. #include <list>
  21. #include <functional>
  22. #include <bitset>
  23. #include <numeric>
  24. #include <iomanip>
  25. #include <ctime>
  26. #include <ctype.h>
  27.  
  28. using namespace std;
  29. typedef long long ll;
  30.  
  31. #define F first
  32. #define S second
  33. #define pb push_back
  34. #define mp make_pair
  35. #define sz size()
  36. #define sqr(x) ((x)*(x))
  37. #define INF numeric_limits<int>::max()
  38.  
  39. const int maxn=100000;
  40. const int maxm=100000;
  41.  
  42. int p[maxn+10];
  43.  
  44. int get(int v) {
  45.     if (v==p[v]) return v;
  46.     p[v]=get(p[v]);
  47.     return p[v];
  48. }
  49.  
  50. void unite(int a, int b) {
  51.     if (rand()&1)
  52.         p[a]=b; else
  53.         p[b]=a;
  54. }
  55.  
  56. int x[maxm+10], y[maxm+10], u[maxm+10], q[maxm+10];
  57. int n, m, a, b, all, k;
  58. vector<int> res;
  59.  
  60. int main()
  61. {
  62.     #ifndef ONLINE_JUDGE
  63.         freopen("input.txt","rt",stdin);
  64.         freopen("output.txt","wt",stdout);
  65.     #endif
  66.     cin>>n>>m;
  67.     for (int i=0; i<m; i++) scanf("%d%d", x+i, y+i);
  68.     cin>>k;
  69.     memset(u, 0, sizeof(u));
  70.     for (int i=0; i<k; i++) scanf("%d", q+i), q[i]--, u[q[i]]=1;
  71.  
  72.     for (int i=1; i<=n; i++) p[i]=i;
  73.     for (int i=0; i<m; i++)
  74.         if (!u[i]) {
  75. //          cerr<<x[i]<<" "<<y[i]<<endl;
  76.             a=get(x[i]); b=get(y[i]);
  77.             if (a!=b) unite(a, b);
  78.         }
  79.     memset(u, 0, sizeof(u));
  80.     all=0;
  81.     for (int i=1; i<=n; i++) {
  82.         p[i]=get(p[i]);
  83.         if (!u[p[i]]) all++;
  84.         u[p[i]]=1;
  85.     }
  86. //  for (int i=1; i<=n; i++) cerr<<p[i]<<" ";
  87.  
  88.     res.pb(all);
  89.     for (int i=k-1; i>0; i--) {
  90.         a=get(x[q[i]]); b=get(y[q[i]]);
  91.         if (a!=b) {
  92.             all--;
  93.             unite(a, b);
  94.         }
  95.         res.pb(all);
  96.     }
  97.     reverse(res.begin(), res.end());
  98.     for (int i=0; i<res.sz; i++) printf("%d ", res[i]);
  99.     return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement