zoukankan      html  css  js  c++  java
  • Luogu P2024 [NOI2001]食物链 POJ 1182

    Description:

    动物王国中有三类动物 A,B,C,这三类动物的食物链构成了有趣的环形。A 吃 B,B吃 C,C 吃 A。
    现有 N 个动物,以 1 - N 编号。每个动物都是 A,B,C 中的一种,但是我们并不知道它到底是哪一种。
    有人用两种说法对这 N 个动物所构成的食物链关系进行描述:
    第一种说法是“1 X Y”,表示 X 和 Y 是同类。
    第二种说法是“2 X Y”,表示 X 吃 Y 。
    此人对 N 个动物,用上述两种说法,一句接一句地说出 K 句话,这 K 句话有的是真的,有的是假的。当一句话满足下列三条之一时,这句话就是假话,否则就是真话。
    • 当前的话与前面的某些真的话冲突,就是假话
    • 当前的话中 X 或 Y 比 N 大,就是假话
    • 当前的话表示 X 吃 X,就是假话
    你的任务是根据给定的 N 和 K 句话,输出假话的总数。

    Analysis:

    首先,第一句话一定是真话!
    开三倍空间的并查集,x, x + n , x + 2n 分别表示 A,B,C三种情况
    然后判断合并时是否矛盾即可。
    来自《挑战程序设计第二版》。

    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #include<queue>
    #define N 50001
    using namespace std;
    int fa[3*N],n,k,ans;
    int find(int x){
    	if(fa[x] == x) return x;
    	return fa[x] = find(fa[x]);
    }
    void unite(int x,int y){
    	fa[find(x)] = find(y);
    }
    int main(){
    	scanf("%d %d",&n,&k);
    	for(int i = 0;i <= 3*n;++i) fa[i] = i;
    	for(int i = 1;i <= k;++i){
    		int t,x,y;
    		scanf("%d %d %d",&t,&x,&y);
    		--x;
    		--y;
    		if(x < 0 || n <= x || y < 0 || y >= n){
    			++ans;
    			continue;
    		}
    		if(t == 1){
    			if(find(x) == find(y + n) || find(x) == find(y + (n << 1)) ){
    				++ans;
    			}
    			else{
    				unite(x,y);
    				unite(x + n,y + n);
    				unite(x + (n << 1),y + (n << 1));
    			}
    		}else{
    			if(find(x) == find(y) || find(x) == find(y + (n << 1))){
    				++ans;
    			}
    			else{
    				unite(x,y + n);
    				unite(x + n,y + (n << 1));
    				unite(x + (n << 1),y);
    			}
    		}
    	}
    	printf("%d",ans);
    	return 0;
    }
    
    岂能尽如人意,但求无愧我心
  • 相关阅读:
    centos6:一个网卡上显示多个ip地址的错误
    博客搬家到CSDN:http://blog.csdn.net/yeweiouyang
    Codeforces Round #430 (Div. 2)
    Codeforces Round #430 (Div. 2)
    Codeforces Round #430 (Div. 2)
    Codeforces Round #426 (Div. 2)
    Codeforces Round #427 (Div. 2)
    Codeforces Round #427 (Div. 2)
    Codeforces Round #427 (Div. 2)
    Codeforces Round #427 (Div. 2)
  • 原文地址:https://www.cnblogs.com/Zforw/p/11250560.html
Copyright © 2011-2022 走看看