捞
查看原帖
捞
1192413
jimmy9_666楼主2024/4/1 21:30
#include <iostream>

using namespace std;

const int N = 400010;

int n, m, k;
struct edge {
	int from, to, next;
} e[N];
int head[N], cnt;
int anc[N], tot, a[N];
bool b[N];
int ans[N];

int add(int u, int v) {
	e[cnt].from = u;
	e[cnt].next = head[u];
	e[cnt].to = v;
	head[u] = cnt;
	cnt ++ ;
}

int find(int x) {
	return anc[x] == x? x : anc[x] = find(anc[x]);
}

int main() {
	cin >> n >> m;
	for (int i = 0; i < n; i ++ )
		anc[i] = i, head[i] = -1;
	for (int i = 0; i < m; i ++ ) {
		int x, y;
		cin >> x >> y;
		add(x, y);
		add(y, x);
	}
	cin >> k;
	tot = n - k;
	for (int i = 1; i <= k; i ++ ) {
		cin >> a[i];
		b[a[i]] = true;
	}
	for (int i = 0; i < 2 * m; i ++ ) {
		int fx = find(e[i].from), fy = find(e[i].to);
		if (b[e[i].from] == false && b[e[i].to] == false && fx != fy)
			tot -- , anc[fx] = fy;
	}
	ans[k + 1] = tot;
	for (int t = k; t >= 1; t -- ) {
		int u = a[t], fx = find(u);
		tot ++ , b[u] = false;
		for (int i = head[u]; ~i; i = e[i].next) {
			int fy = find(e[i].to);
			if (b[e[i].to] == false && fx != fy)
				tot -- , anc[fy] = fx;
		}
		ans[t] = tot;
	}
	for (int i = 1; i <= k + 1; i ++ )
		cout << ans[i] << endl;
	return 0;
}
2024/4/1 21:30
加载中...