• _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
    ", ans[i]);
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    /bin/bash^M:损坏的解释器: 没有那个文件或目录
    QT槽函数处理线程
    Strategy策略模式
    Proxy代理模式
    Ubuntu系统修改BIOS时间问题
    Ubuntu下安装Goldendict(翻译软件)
    自定义QMenu
    C connect实现Timeout效果(Windows)
    059 Python计算生态概览
    058 程序设计方法学小结
  • 原文地址:https://www.cnblogs.com/ciao-sora/p/6169539.html
Copyright © 2020-2023  润新知