18.9.17 poj1182 食物链

描述

动物王国中有三类动物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句话有的是真的,有的是假的。当一句话满足下列三条之一时,这句话就是假话,否则就是真话。
1) 当前的话与前面的某些真的话冲突,就是假话;
2) 当前的话中X或Y比N大,就是假话;
3) 当前的话表示X吃X,就是假话。
你的任务是根据给定的N(1 <= N <= 50,000)和K句话(0 <= K <= 100,000),输出假话的总数。
输入第一行是两个整数N和K,以一个空格分隔。
以下K行每行是三个正整数 D,X,Y,两数之间用一个空格隔开,其中D表示说法的种类。
若D=1,则表示X和Y是同类。
若D=2,则表示X吃Y。输出只有一个整数,表示假话的数目。

样例输入

100 7
1 101 1 
2 1 2
2 2 3 
2 3 3 
1 1 3 
2 3 1 
1 5 5

样例输出

3

来源

Noi 01

 1 #include <iostream>
 2 #include <string>
 3 
 4 using namespace std;
 5 const int maxn = 50005;
 6 int parent[maxn];//根节点
 7 int relation[maxn];//与根节点关系 0同一类 1被根节点吃 2吃根节点
 8 
 9 int findparent(int x){
10     if(parent[x]!=x){
11         int root=findparent(parent[x]);
12         relation[x]=(relation[x]+relation[parent[x]])%3;
13         parent[x]=root;
14     }
15     return parent[x];
16 }
17 
18 bool merge(int x,int y,int type){
19     int px=findparent(x),py=findparent(y);
20     if(px==py){
21         if((type+relation[x]-1+3)%3==relation[y])return true;
22         return false;
23     }
24     relation[py]=(type+relation[x]-relation[y]-1+3)%3;
25     parent[py]=px;
26     return true;
27 }
28 
29 int main()
30 {
31     int n, k,sum=0;
32     scanf("%d%d",&n,&k);
33     for(int i=1;i<=n;i++)
34     {
35         relation[i]=0;
36         parent[i]=i;
37     }
38     while(k--){
39         int type,x,y;
40         scanf("%d%d%d",&type,&x,&y);
41         if(x>n||y>n||x==y&&type==2){sum++;continue;}
42         bool flag=merge(x,y,type);
43         if(!flag)sum++;
44     }
45     printf("%d\n",sum);
46     return 0;
47 }
View Code

同一树中包含两两已知关系的动物

 relation[x] 存放与根节点动物的关系

 1 #include <iostream>
 2 #include <string.h>
 3 #include <algorithm>
 4 #include <stack>
 5 #include <string>
 6 #include <math.h>
 7 #include <queue>
 8 #include <stdio.h>
 9 #include <string.h>
10 #include <set>
11 #include <vector>
12 #define maxn 50005
13 #define inf 999999
14 #define EPS 1e-10
15 #define lowbit(x) (int)(x&(-x))
16 using namespace std;
17 
18 int parent[maxn], relate[maxn];
19 int n, k;
20 
21 int getroot(int x) {
22     int px = parent[x];
23     if (px == x)return x;
24     parent[x] = getroot(px);
25     relate[x] = (relate[px] + relate[x]) % 3;
26     return parent[x];
27 }
28 
29 
30 bool Union(int x, int y, int relation) {
31     int px = getroot(x), py = getroot(y);
32     if (px == py && (relate[x]-relate[y]+3)%3 == relation)
33         return true;
34     if (px == py && (relate[x] - relate[y] + 3) % 3 != relation)
35         return false;
36     parent[px] = py;
37     relate[px] = (relate[y] - relate[x] + relation) % 3;
38     return true;
39 }
40 
41 void init() {
42     scanf("%d%d", &n, &k);
43     int ans = 0;
44     for (int i = 1; i <= n; i++)
45         parent[i] = i;
46     while (k--) {
47         int cmd, x, y;
48         scanf("%d%d%d", &cmd, &x, &y);
49         if (x > n || y > n) {
50             ans++;
51             continue;
52         }
53         if (cmd == 1) {
54             if (!Union(x, y, 0))
55                 ans++;
56         }
57         if (cmd == 2) {
58             if (!Union(x, y, 1))
59                 ans++;
60         }
61     }
62     printf("%d\n", ans);
63 }
64 
65 int main() {
66     init();
67     return 0;
68 }
View Code

 

更新!之前的参数过于麻烦

 

posted @ 2018-09-17 16:28  TobicYAL  阅读(265)  评论(0编辑  收藏  举报