-
Notifications
You must be signed in to change notification settings - Fork 0
/
793.java
134 lines (112 loc) · 2.51 KB
/
793.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import java.io.*;
import java.util.*;
class DisjointSet {
private int[] parent;
private int[] rank;
public DisjointSet(int N) {
parent = new int[N + 1];
rank = new int[N + 1];
for (int i = 0; i < N; i++) {
parent[i] = i;
}
for (int i = 0; i < N; i++) {
rank[i] = 1;
}
}
public void union(int node1, int node2) {
int root1, root2;
root1 = root(node1);
root2 = root(node2);
if (root1 != root2) {
if (rank[root1] > rank[root2]) {
parent[root2] = root1;
} else if (rank[root1] < rank[root2]) {
parent[root1] = root2;
} else {
parent[root2] = root1;
rank[root1]++;
}
}
}
public int root(int node) {
if (node == parent[node]) {
return node;
}
return parent[node] = root(parent[node]);
}
public boolean isConnected(int node1, int node2) {
return root(node1) == root(node2);
}
}
class Pair {
int first, second;
}
public class Main {
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(System.in);
int t;
t = in.nextInt();
for (int z = 0; z < t; z++) {
int n;
n = in.nextInt();
DisjointSet ds = new DisjointSet(n);
int correct = 0, wrong = 0;
String s = "";
int c = 0, q = 0;
while (true) {
s = in.nextLine();
if (s == null || s.length() == 0) {
break;
}
String[] arr = s.split(" ");
int x, y;
x = Integer.parseInt(arr[1]);
y = Integer.parseInt(arr[2]);
if (arr[0].compareTo("c") == 0) {
ds.union(x, y);
} else {
if (ds.isConnected(x, y)) {
correct++;
} else {
wrong++;
}
}
}
System.out.println(correct + "," + wrong);
if (z < t - 1) {
System.out.println();
}
}
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(String s) throws FileNotFoundException {
br = new BufferedReader(new FileReader(s));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public boolean ready() throws IOException {
return br.ready();
}
}
}