_bzoj1015 [JSOI2008]星球大战starwar【并查集】

传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1015

倒过来做就ok了。

#include <cstdio>
#include <cstring>

const int maxn = 400005, maxm = 200005;

int n, m, t1, t2, ans[maxn], cnt;
int head[maxn], to[maxm << 1], next[maxm << 1], lb;
int fa[maxn], destroy[maxn], num_of_des, fu, fv;
char book[maxn];

inline void ist(int aa, int ss) {
	to[lb] = ss;
	next[lb] = head[aa];
	head[aa] = lb;
	++lb;
}
int getfa(int aa) {
	return fa[aa] == aa? aa: fa[aa] = getfa(fa[aa]);
}

int main(void) {
	//freopen("in.txt", "r", stdin);
	memset(head, -1, sizeof head);
	memset(next, -1, sizeof next);
	scanf("%d%d", &n, &m);
	for (int i = 1; i < n; ++i) {
		fa[i] = i;
	}
	while (m--) {
		scanf("%d%d", &t1, &t2);
		ist(t1, t2);
		ist(t2, t1);
	}
	scanf("%d", &num_of_des);
	for (int i = 0; i < num_of_des; ++i) {
		scanf("%d", destroy + i);
		book[destroy[i]] = 1;
	}
	
	cnt = n - num_of_des;
	for (int i = 0; i < n; ++i) {
		if (book[i]) {
			continue;
		}
		for (int j = head[i]; j != -1; j = next[j]) {
			if (book[to[j]]) {
				continue;
			}
			fu = getfa(i);
			fv = getfa(to[j]);
			if (fu != fv) {
				fa[fu] = fv;
				--cnt;
			}
		}
	}
	ans[num_of_des] = cnt;
	for (int i = num_of_des - 1; ~i; --i) {
		book[destroy[i]] = 0;
		++cnt;
		for (int j = head[destroy[i]]; j != -1; j = next[j]) {
			if (book[to[j]]) {
				continue;
			}
			fu = getfa(destroy[i]);
			fv = getfa(to[j]);
			if (fu != fv) {
				fa[fu] = fv;
				--cnt;
			}
		}
		ans[i] = cnt;
	}
	
	for (int i = 0; i <= num_of_des; ++i) {
		printf("%d\n", ans[i]);
	}
	return 0;
}

  

posted @ 2016-12-13 13:28  ciao_sora  阅读(139)  评论(0编辑  收藏  举报