4eyes4u

Untitled

Apr 4th, 2016
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.46 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int N=2e5+10;
  5.  
  6. struct NodeDSU
  7. {
  8.     int rank;
  9.     int parent;
  10. } dsu[N];
  11.  
  12. bool mark[N];
  13. vector<int> g[N], sol;
  14. int n, m, p[N], k;
  15.  
  16. void MakeSet (int x)
  17. {
  18.     dsu[x].rank=0;
  19.     dsu[x].parent=x;
  20.     mark[x]=true;
  21.     k++;
  22. }
  23.  
  24. int FindSet(int x)
  25. {
  26.     if (dsu[x].parent==x) return x;
  27.     dsu[x].parent=FindSet(dsu[x].parent);
  28.     return dsu[x].parent;
  29. }
  30.  
  31. void UniteSet (int x, int y)
  32. {
  33.     x=FindSet(x);
  34.     y=FindSet(y);
  35.  
  36.     if (x==y) return;
  37.     k--;
  38.     if (dsu[x].rank<dsu[y].rank) dsu[x].parent=x;
  39.     else dsu[y].parent=x;
  40.     if (dsu[x].rank==dsu[y].rank) dsu[x].rank++;
  41. }
  42.  
  43. int main()
  44. {
  45.     freopen("closing.in", "r", stdin);
  46.     freopen("closing.out", "w", stdout);
  47.  
  48.     scanf ("%d%d", &n, &m);
  49.     for (int i=0;i<m;i++)
  50.     {
  51.         int a, b;
  52.         scanf ("%d%d", &a, &b);
  53.         g[a].push_back(b);
  54.         g[b].push_back(a);
  55.     }
  56.  
  57.     for (int i=n-1;i>=0;i--) scanf ("%d", &p[i]);
  58.     MakeSet(p[0]);
  59.     for (int i=1;i<n;i++)
  60.     {
  61.         if (k==1) sol.push_back(1);
  62.         else sol.push_back(0);
  63.  
  64.         MakeSet(p[i]);
  65.         for (int j=0;j<g[p[i]].size();j++)
  66.         {
  67.             int u=g[p[i]][j];
  68.             if (mark[u]) UniteSet(p[i], u);
  69.         }
  70.     }
  71.     if (k==1) sol.push_back(1);
  72.     else sol.push_back(0);
  73.  
  74.     for (int i=n-1;i>=0;i--)
  75.     {
  76.         if (sol[i]==1) puts ("YES");
  77.         else puts ("NO");
  78.     }
  79.  
  80.     return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment