noj火烧赤壁2
这是我的一个学长出的题就是并查集但是开始没想到还能这样倒着来总之很有想法的一道题
#include<cstdio> #include<cstring> #include<algorithm> #include<vector> using namespace std; vector<int>tt[200005]; int b[200005]; int cc[200005][2]; int dd[200005]; int xx[200005]; int yy[200005]; int find(int x) { if(b[x] != x) b[x] = find(b[x]); return (b[x]); } void un(int x, int y) { if(find(x) != find(y)) b[x] = y; } int main() { int n, m; while(~scanf("%d%d", &n, &m)) { for(int i = 0; i < n; i ++) { tt[i].clear(); } memset(xx, 0, sizeof(xx)); for(int i = 1; i <= n; i++) { b[i] = i; } for(int i = 0; i < m; i++) { scanf("%d%d", &cc[i][0], &cc[i][1]); tt[cc[i][1]].push_back(cc[i][0]); tt[cc[i][0]].push_back(cc[i][1]); } int t; scanf("%d", &t); int num = n - t; for(int i = 0; i < t ; i++) { scanf("%d", &dd[i]); xx[dd[i]] = 1; } for(int i = 0; i < m; i++) { if(!(xx[cc[i][0]] || xx[cc[i][1]])) { if(find(cc[i][0]) != find(cc[i][1])) { un(find(cc[i][0]), find(cc[i][1])); num--; } } } yy[t] = num; for(int i = t-1; i >= 0; i--) { xx[dd[i]] = 0; num++; for(int j = 0; j < tt[dd[i]].size(); j++) { if(!xx[tt[dd[i]][j]]) { if(find(dd[i]) != find(tt[dd[i]][j])) { un(find(tt[dd[i]][j]), find(dd[i])); num--; } } } yy[i] = num; }//printf("erwsrwer\n"); for(int i = 0; i <= t; i++) { printf("%d\n", yy[i]); } } return 0; }