Skip to content

Commit 240ab49

Browse files
authored
Merge pull request #544 from AlgorithmWithGod/JHLEE325
[20250725] BOJ / G4 / 거짓말 / 이준희
2 parents ecbcbfd + 31cd8ca commit 240ab49

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class Main {
6+
static int[] parent;
7+
static boolean[] truth;
8+
9+
public static void main(String[] args) throws Exception {
10+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
11+
StringTokenizer st = new StringTokenizer(br.readLine());
12+
13+
int n = Integer.parseInt(st.nextToken());
14+
int m = Integer.parseInt(st.nextToken());
15+
16+
parent = new int[n + 1];
17+
truth = new boolean[n + 1];
18+
for (int i = 1; i <= n; i++) parent[i] = i;
19+
20+
st = new StringTokenizer(br.readLine());
21+
int t = Integer.parseInt(st.nextToken());
22+
List<Integer> truthList = new ArrayList<>();
23+
for (int i = 0; i < t; i++) {
24+
int person = Integer.parseInt(st.nextToken());
25+
truth[person] = true;
26+
truthList.add(person);
27+
}
28+
29+
List<List<Integer>> parties = new ArrayList<>();
30+
31+
for (int i = 0; i < m; i++) {
32+
st = new StringTokenizer(br.readLine());
33+
int cnt = Integer.parseInt(st.nextToken());
34+
List<Integer> party = new ArrayList<>();
35+
36+
if (cnt > 0) {
37+
int first = Integer.parseInt(st.nextToken());
38+
party.add(first);
39+
for (int j = 1; j < cnt; j++) {
40+
int next = Integer.parseInt(st.nextToken());
41+
union(first, next);
42+
party.add(next);
43+
}
44+
}
45+
parties.add(party);
46+
}
47+
48+
Set<Integer> truthRoots = new HashSet<>();
49+
for (int i = 1; i <= n; i++) {
50+
if (truth[i]) {
51+
truthRoots.add(find(i));
52+
}
53+
}
54+
55+
int answer = 0;
56+
for (List<Integer> party : parties) {
57+
boolean canLie = true;
58+
for (int person : party) {
59+
if (truthRoots.contains(find(person))) {
60+
canLie = false;
61+
break;
62+
}
63+
}
64+
if (canLie) answer++;
65+
}
66+
67+
System.out.println(answer);
68+
}
69+
70+
static int find(int x) {
71+
if (x != parent[x]) {
72+
parent[x] = find(parent[x]);
73+
}
74+
return parent[x];
75+
}
76+
77+
static void union(int a, int b) {
78+
int ra = find(a);
79+
int rb = find(b);
80+
if (ra != rb) {
81+
parent[rb] = ra;
82+
}
83+
}
84+
}
85+
86+
```

0 commit comments

Comments
 (0)