总算是没看题解自己A了一道题,呜呜呜。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | #include<iostream> #include<vector> #include<cstring> #include<stack> #include<queue> using namespace std; int father[400005]; int _find(int node){ if(father[node]==-1)return node; return father[node]=_find(father[node]); } bool _union(int x,int y){ if(_find(x)==_find(y))return false; father[_find(x)]=_find(y); return true; } int n,m,k; bool alive[400005]; vector<int> edges[400005]; stack<int> ans; stack<int> q; int cnt,cntn; int main(){ ios::sync_with_stdio(false); memset(father,-1,sizeof(father)); memset(alive,true,sizeof(alive)); cin>>n>>m; cnt=0;cntn=n; for(int i=1;i<=m;i++){ int x,y; cin>>x>>y; edges[x].push_back(y); edges[y].push_back(x); } cin>>k; for(int i=1;i<=k;i++){ int v; cin>>v; alive[v]=false; q.push(v); cntn--; } for(int i=0;i<n;i++){ if(!alive[i])continue; for(int j=0;j<edges[i].size();j++){ if(!alive[edges[i][j]])continue; if(_union(i,edges[i][j]))cnt++; } } ans.push(cntn-cnt); while(!q.empty()){ int cur=q.top();q.pop(); cntn++; alive[cur]=true; for(int i=0;i<edges[cur].size();i++){ if(!alive[edges[cur][i]])continue; if(_union(cur,edges[cur][i]))cnt++; } ans.push(cntn-cnt); } while(!ans.empty()){ cout<<ans.top()<<endl; ans.pop(); } } |