|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +public class Main { |
| 6 | + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 7 | + static StringTokenizer st; |
| 8 | + static void nextLine() throws Exception { st = new StringTokenizer(br.readLine()); } |
| 9 | + static int nextInt() { return Integer.parseInt(st.nextToken()); } |
| 10 | + |
| 11 | + static int N, M; |
| 12 | + static int[] parent; |
| 13 | + static List<int[]> parties = new ArrayList<>(); |
| 14 | + |
| 15 | + public static void main(String[] args) throws Exception { |
| 16 | + nextLine(); |
| 17 | + N = nextInt(); |
| 18 | + M = nextInt(); |
| 19 | + |
| 20 | + parent = new int[N + 1]; |
| 21 | + for (int i = 1; i <= N; i++) parent[i] = i; |
| 22 | + |
| 23 | + nextLine(); |
| 24 | + int T = nextInt(); |
| 25 | + int[] truth = new int[T]; |
| 26 | + for (int i = 0; i < T; i++) truth[i] = nextInt(); |
| 27 | + |
| 28 | + for (int i = 0; i < M; i++) { |
| 29 | + nextLine(); |
| 30 | + int cnt = nextInt(); |
| 31 | + int[] attendees = new int[cnt]; |
| 32 | + for (int j = 0; j < cnt; j++) attendees[j] = nextInt(); |
| 33 | + parties.add(attendees); |
| 34 | + |
| 35 | + for (int j = 1; j < cnt; j++) { |
| 36 | + union(attendees[0], attendees[j]); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + boolean[] truthRoot = new boolean[N + 1]; |
| 41 | + for (int t : truth) { |
| 42 | + truthRoot[find(t)] = true; |
| 43 | + } |
| 44 | + |
| 45 | + int answer = 0; |
| 46 | + for (int[] party : parties) { |
| 47 | + boolean canLie = true; |
| 48 | + for (int p : party) { |
| 49 | + if (truthRoot[find(p)]) { |
| 50 | + canLie = false; |
| 51 | + break; |
| 52 | + } |
| 53 | + } |
| 54 | + if (canLie) answer++; |
| 55 | + } |
| 56 | + |
| 57 | + System.out.println(answer); |
| 58 | + } |
| 59 | + |
| 60 | + static int find(int x) { |
| 61 | + if (x == parent[x]) return x; |
| 62 | + return parent[x] = find(parent[x]); |
| 63 | + } |
| 64 | + |
| 65 | + static void union(int x, int y) { |
| 66 | + x = find(x); |
| 67 | + y = find(y); |
| 68 | + if (x == y) return; |
| 69 | + parent[y] = x; |
| 70 | + } |
| 71 | +} |
| 72 | +``` |
0 commit comments