标签:
input | output |
---|---|
4 4 1 2 2 3 1 3 3 4 3 2 4 3 |
1 2 3 |
3 1 1 2 1 1 |
3
|
分析:倒着写并查集;
代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <algorithm> #include <climits> #include <cstring> #include <string> #include <set> #include <map> #include <queue> #include <stack> #include <vector> #include <list> #define rep(i,m,n) for(i=m;i<=n;i++) #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++) #define mod 1000000007 #define inf 0x3f3f3f3f #define vi vector<int> #define pb push_back #define mp make_pair #define fi first #define se second #define ll long long #define pi acos(-1.0) const int maxn=1e5+10; const int dis[4][2]={{0,1},{-1,0},{0,-1},{1,0}}; using namespace std; ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);} ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;} int n,m,k,t,p[maxn],ans[maxn],query[maxn],coco[maxn]; struct node { int x,y; }a[maxn]; int find(int x) { return p[x]==x?x:p[x]=find(p[x]); } int main() { int i,j; scanf("%d%d",&n,&m); rep(i,1,n)p[i]=i; rep(i,1,m)scanf("%d%d",&a[i].x,&a[i].y); scanf("%d",&t); rep(i,1,t)scanf("%d",&query[i]),coco[query[i]]=1; ans[t]=n; rep(i,1,m) { if(!coco[i]) { int fa=find(a[i].x),fb=find(a[i].y); if(fa!=fb)p[fa]=fb,ans[t]--; } } for(i=t-1;i>=1;i--) { int fa=find(a[query[i+1]].x),fb=find(a[query[i+1]].y); if(fa!=fb)p[fa]=fb,ans[i]=ans[i+1]-1; else ans[i]=ans[i+1]; } printf("%d",ans[1]); rep(i,2,t)printf(" %d",ans[i]); printf("\n"); //system("pause"); return 0; }
标签:
原文地址:http://www.cnblogs.com/dyzll/p/5784932.html