-
Notifications
You must be signed in to change notification settings - Fork 7
/
1182 食物链.java
80 lines (79 loc) · 2.59 KB
/
1182 食物链.java
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
import java.util.*;
import java.io.*;
public class Main {
private static int[] parent;
private static int[] relation; //每个节点对应的 r[]值记录他与根节点的关系:0:同类, 1:被父亲节点吃, 2: 吃父亲节点
public static void main(String[] args) throws IOException {
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
String[] strs = read.readLine().split("\\s+");
int num = Integer.parseInt(strs[0]);
int testcase = Integer.parseInt(strs[1]);
parent = new int[num + 1];
relation = new int[num + 1];
for (int i = 1; i < parent.length; ++i) {
parent[i] = i;
}
int rst = 0;
while (testcase-- > 0) {
strs = read.readLine().split("\\s+");
int type = Integer.parseInt(strs[0]);
int a = Integer.parseInt(strs[1]);
int b = Integer.parseInt(strs[2]);
if (a > num || b > num) {
rst++;
continue;
}
if (a == b && type == 2) {
rst++;
continue;
}
int fa = find(a);
int fb = find(b);
if (fa == fb) {
if (type == 1) {
if (relation[a] != relation[b]) {
rst++;
}
} else {
if ((relation[a] + 1) % 3 != relation[b] % 3) {
rst++;
}
}
} else
union(a, b, type);
}
read.close();
System.out.println(rst);
}
private static int find(int animal) {
if (animal == parent[animal])
return parent[animal];
else {
int p = parent[animal];
parent[animal] = find(parent[animal]);
relation[animal] = (relation[animal] + relation[p]) % 3;
}
return parent[animal];
}
private static void union(int x, int y, int type) {
int fx = find(x);
int fy = find(y);
if (fx != fy) {
if (relation[x] < relation[y]) {
parent[fx] = fy;
if (type == 2) {
relation[fx] = (relation[y] - relation[x] - 1 + 3) % 3;
} else {
relation[fx] = (relation[y] - relation[x]) % 3;
}
} else {
parent[fy] = fx;
if (type == 2) {
relation[fy] = (relation[x] - relation[y] + 1) % 3;
} else {
relation[fy] = (relation[x] - relation[y]) % 3;
}
}
}
}
}