-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path1003. Parity.java
113 lines (89 loc) · 2.95 KB
/
1003. Parity.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
public class Parity {
public static class DSU {
Map<Integer, Integer> root = new HashMap<Integer, Integer>();
Map<Integer, Integer> size = new HashMap<Integer, Integer>();
private int getRoot(int i) {
while (root.get(i) != i) {
i = root.get(i);
}
return i;
}
public int union(int i, int j) {
if (root.get(i) == null) {
root.put(i, i);
size.put(i, 1);
}
if (root.get(j) == null) {
root.put(j, j);
size.put(j, 1);
}
int root_i = getRoot(i);
int root_j = getRoot(j);
int parent = 0;
/* same set then just return the parent */
if (root_i == root_j) {
parent = root_i;
}
/* Else different sets */
else if (size.get(root_i) > size.get(root_j)) {
parent = root_i;
root.put(root_j, parent);
} else {
parent = root_j;
root.put(root_i, parent);
}
return parent;
}
public boolean find(int i, int j) {
if (root.get(i) != null && root.get(j) != null) {
return getRoot(i) == getRoot(j);
} else {
return false;
}
}
public Set<Integer> keys() {
return root.keySet();
}
public void print() {
for (Integer key : root.keySet()) {
System.out.println(key + " = " + root.get(key));
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (true) {
int N = sc.nextInt();
if (N == -1) {
System.exit(0);
}
DSU dsu = new DSU();
int M = sc.nextInt();
int result = M;
for (int i = 0; i < M; i++) {
int k = sc.nextInt();
int m = sc.nextInt() + 1;
if ((k > N || m > N + 1) && result == M) {
result = i;
}
String s = sc.next();
/* odd */
if (s.charAt(0) == 'o') {
dsu.union(k, -m);
dsu.union(-k, m);
} else {
dsu.union(k, m);
dsu.union(-k, -m);
}
if ((dsu.find(m, -m) || dsu.find(k, -k)) && result == M) {
result = i;
}
}
System.out.println(result);
}
}
}