zoukankan      html  css  js  c++  java
  • _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;
    }
    

      

  • 相关阅读:
    wait waitpid
    达梦备份还原
    sigprocmask阻塞信号
    sigaction信号处理
    dd命令
    linux系统启动过程
    cpio建立、还原备份档
    configure详解
    Git入门基础教程
    一篇文章了解Github和Git教程
  • 原文地址:https://www.cnblogs.com/ciao-sora/p/6169539.html
Copyright © 2011-2022 走看看