-
Notifications
You must be signed in to change notification settings - Fork 0
/
poj1182.c
100 lines (80 loc) · 1.48 KB
/
poj1182.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
* Problem: POJ1182, food chain (Disjoint Sets)
* User: LoiteringLeo
* Memory: 776K Time: 266MS
*Language: GCC Result: Accepted
*/
#include <stdio.h>
#define MAX_ANIMAL 50000
#define UNDETERMINED_TYPE 3
int parent[MAX_ANIMAL + 1]; // Parent node id
int type[MAX_ANIMAL + 1]; // Node type
int lies = 0;
int n = 0;
int k = 0;
void make_set(int k)
{
parent[k] = k;
type[k] = 0;
}
int finds(int k)
{
if (parent[k] == k) {
return k;
}
else {
parent[k] = finds(parent[k]);
}
return parent[k];
}
int find_set(int k)
{
int p;
if (parent[k] == k) {
return k;
}
p = find_set(parent[k]);
type[k] = (type[k] + type[parent[k]]) % 3;
parent[k] = p;
return p;
}
void check(int d, int x, int y)
{
int p_x = find_set(x);
int p_y = find_set(y);
if (x > n || y > n) {
lies++;
return;
}
if (p_x == p_y) {
if (((type[y] - type[x] + 3) % 3) != d - 1) {
lies++;
}
return;
}
parent[p_y] = p_x;
if (d == 1) {
type[p_y] = (type[x] - type[y] + 3) % 3;
}
else if (d == 2) {
type[p_y] = (type[x] - type[y] + 7) % 3;
}
return;
}
int main()
{
int i;
int d;
int x;
int y;
scanf("%d %d", &n, &k);
for (i = 1; i <= n; i++) {
make_set(i);
}
for (i = 1; i <= k; i++) {
scanf("%d %d %d", &d, &x, &y);
check(d, x, y);
}
printf("%d\n", lies);
return 0;
}