|
| 1 | +import java.io.*; |
| 2 | +import java.util.*; |
| 3 | + |
| 4 | +public class Main { |
| 5 | + public static int[] parent; |
| 6 | + |
| 7 | + public static void main(String[] args) throws IOException { |
| 8 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 9 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 10 | + int N = Integer.parseInt(st.nextToken()); |
| 11 | + int M = Integer.parseInt(st.nextToken()); |
| 12 | + parent = new int[N+1]; |
| 13 | + for(int i=1; i<=N; i++) { |
| 14 | + parent[i] = i; |
| 15 | + } |
| 16 | + st = new StringTokenizer(br.readLine()); |
| 17 | + int truthNum = Integer.parseInt(st.nextToken()); |
| 18 | + boolean[] truth = new boolean[N+1]; |
| 19 | + for(int i=0; i<truthNum; i++) { |
| 20 | + int truthIndex = Integer.parseInt(st.nextToken()); |
| 21 | + truth[truthIndex] = true; |
| 22 | + } |
| 23 | + List<List<Integer>> attendee = new ArrayList<>(); |
| 24 | + for(int i=0; i<=M; i++) { |
| 25 | + attendee.add(new ArrayList<>()); |
| 26 | + } |
| 27 | + for(int i=1; i<=M; i++) { |
| 28 | + st = new StringTokenizer(br.readLine()); |
| 29 | + int num = Integer.parseInt(st.nextToken()); |
| 30 | + for(int j=1; j<=num; j++) { |
| 31 | + int attendeeIndex = Integer.parseInt(st.nextToken()); |
| 32 | + attendee.get(i).add(attendeeIndex); |
| 33 | + union(attendee.get(i).get(0), attendeeIndex); |
| 34 | + } |
| 35 | + } |
| 36 | + // 진실을 아는 사람의 parent 도 진실을 알도록 업데이트 |
| 37 | + for(int i=1; i<=N; i++) { |
| 38 | + if(truth[i]) { |
| 39 | + truth[find(i)] = true; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + int answer = M; |
| 44 | + for(int i=1; i<=M; i++) { |
| 45 | + // 파티 참석자의 부모 노드가 진실을 아는 사람이면 거짓말 할 수 없음 |
| 46 | + for(int j=0; j<attendee.get(i).size(); j++) { |
| 47 | + int attendeeParent = find(attendee.get(i).get(j)); |
| 48 | + if(truth[attendeeParent]) { |
| 49 | + answer--; |
| 50 | + break; |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + System.out.println(answer); |
| 55 | + } |
| 56 | + |
| 57 | + public static int find(int x) { |
| 58 | + if(parent[x] == x) { |
| 59 | + return x; |
| 60 | + } |
| 61 | + parent[x] = find(parent[x]); |
| 62 | + return parent[x]; |
| 63 | + } |
| 64 | + public static void union(int x, int y) { |
| 65 | + x = find(x); |
| 66 | + y = find(y); |
| 67 | + if(x == y) { |
| 68 | + return; |
| 69 | + } |
| 70 | + parent[y] = x; |
| 71 | + } |
| 72 | +} |
0 commit comments