-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSetOperation.java
More file actions
76 lines (72 loc) · 2.39 KB
/
SetOperation.java
File metadata and controls
76 lines (72 loc) · 2.39 KB
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
package company;
import java.util.*;
/**
* @Author: Wenhang Chen
* @Description:
* @Date: Created in 19:31 4/11/2020
* @Modified by:
*/
public class SetOperation {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
// 每组样例
while (T-- > 0) {
// 正整数个数
int N = sc.nextInt();
List<Set<Integer>> list = new ArrayList<>();
for (int i = 1; i <= N; i++) {
Set<Integer> set = new HashSet<>();
set.add(i);
list.add(set);
}
// 操作数
int M = sc.nextInt();
// 具体操作
for (int i = 0; i < M; i++) {
// 操作类型
int op = sc.nextInt();
int a, b;
// 操作数
if (op == 1) {
a = sc.nextInt();
b = sc.nextInt();
int p1 = 0, p2 = 0;
for (int k = 0; k < list.size(); k++) {
if (list.get(k).contains(a)) {
p1 = k;
}
if (list.get(k).contains(b)) {
p2 = k;
}
}
if (p1 != p2) {
for (Integer m : list.get(p2)) {
list.get(p1).add(m);
}
list.remove(p2);
}
} else if (op == 2) {
a = sc.nextInt();
for (int k = 0; k < list.size(); k++) {
if (list.get(k).contains(a) && list.get(k).size() > 1) {
list.get(k).remove(a);
Set<Integer> s = new HashSet<>();
s.add(a);
list.add(s);
break;
}
}
} else {
a = sc.nextInt();
for (Set<Integer> integers : list) {
if (integers.contains(a)) {
System.out.println(integers.size());
break;
}
}
}
}
}
}
}