Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int N=2e5+10;
- struct NodeDSU
- {
- int rank;
- int parent;
- } dsu[N];
- bool mark[N];
- vector<int> g[N], sol;
- int n, m, p[N], k;
- void MakeSet (int x)
- {
- dsu[x].rank=0;
- dsu[x].parent=x;
- mark[x]=true;
- k++;
- }
- int FindSet(int x)
- {
- if (dsu[x].parent==x) return x;
- dsu[x].parent=FindSet(dsu[x].parent);
- return dsu[x].parent;
- }
- void UniteSet (int x, int y)
- {
- x=FindSet(x);
- y=FindSet(y);
- if (x==y) return;
- k--;
- if (dsu[x].rank<dsu[y].rank) dsu[x].parent=x;
- else dsu[y].parent=x;
- if (dsu[x].rank==dsu[y].rank) dsu[x].rank++;
- }
- int main()
- {
- freopen("closing.in", "r", stdin);
- freopen("closing.out", "w", stdout);
- scanf ("%d%d", &n, &m);
- for (int i=0;i<m;i++)
- {
- int a, b;
- scanf ("%d%d", &a, &b);
- g[a].push_back(b);
- g[b].push_back(a);
- }
- for (int i=n-1;i>=0;i--) scanf ("%d", &p[i]);
- MakeSet(p[0]);
- for (int i=1;i<n;i++)
- {
- if (k==1) sol.push_back(1);
- else sol.push_back(0);
- MakeSet(p[i]);
- for (int j=0;j<g[p[i]].size();j++)
- {
- int u=g[p[i]][j];
- if (mark[u]) UniteSet(p[i], u);
- }
- }
- if (k==1) sol.push_back(1);
- else sol.push_back(0);
- for (int i=n-1;i>=0;i--)
- {
- if (sol[i]==1) puts ("YES");
- else puts ("NO");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment