|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +public class BOJ22197 { |
| 6 | + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 7 | + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 8 | + static StringTokenizer st; |
| 9 | + |
| 10 | + static int N, M, K; |
| 11 | + static List<int[]>[] tree; |
| 12 | + static TreeMap<Integer, Integer>[] map; |
| 13 | + static int[] size; |
| 14 | + static int answerCount = 0; |
| 15 | + static boolean[] isAnswer; |
| 16 | + |
| 17 | + public static void main(String[] args) throws Exception { |
| 18 | + st = new StringTokenizer(br.readLine()); |
| 19 | + N = Integer.parseInt(st.nextToken()); |
| 20 | + M = Integer.parseInt(st.nextToken()); |
| 21 | + K = Integer.parseInt(st.nextToken()); |
| 22 | + |
| 23 | + tree = new List[N+1]; |
| 24 | + for(int i=1;i<=N;i++) { |
| 25 | + tree[i] = new ArrayList<>(); |
| 26 | + } |
| 27 | + |
| 28 | + for(int i=1;i<N;i++) { |
| 29 | + st = new StringTokenizer(br.readLine()); |
| 30 | + int a = Integer.parseInt(st.nextToken()), b = Integer.parseInt(st.nextToken()); |
| 31 | + tree[a].add(new int[]{b,i}); |
| 32 | + tree[b].add(new int[]{a,i}); |
| 33 | + } |
| 34 | + |
| 35 | + map = new TreeMap[N+1]; |
| 36 | + for(int i=1;i<=N;i++) { |
| 37 | + map[i] = new TreeMap<>(); |
| 38 | + } |
| 39 | + size = new int[M]; |
| 40 | + for(int i=0;i<M;i++) { |
| 41 | + st = new StringTokenizer(br.readLine()); |
| 42 | + size[i] = Integer.parseInt(st.nextToken()); |
| 43 | + for(int j=0;j<size[i];j++) { |
| 44 | + map[Integer.parseInt(st.nextToken())].put(i, 1); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + isAnswer = new boolean[N]; |
| 49 | + dfs(1, 0); |
| 50 | + |
| 51 | + bw.write(answerCount + "\n"); |
| 52 | + for(int i=1;i<N;i++) if(isAnswer[i]) { |
| 53 | + bw.write(i + " "); |
| 54 | + } |
| 55 | + |
| 56 | + bw.close(); |
| 57 | + } |
| 58 | + |
| 59 | + public static void dfs(int cur, int par) { |
| 60 | + for(int[] edge : tree[cur]) { |
| 61 | + int nxt = edge[0], edgeNum = edge[1]; |
| 62 | + if(nxt == par) { |
| 63 | + continue; |
| 64 | + } |
| 65 | + |
| 66 | + dfs(nxt, cur); |
| 67 | + if(map[nxt].size() >= K) { |
| 68 | + answerCount++; |
| 69 | + isAnswer[edgeNum] = true; |
| 70 | + } |
| 71 | + |
| 72 | + if(map[cur].size() < map[nxt].size()) { |
| 73 | + TreeMap<Integer, Integer> temp = map[cur]; |
| 74 | + map[cur] = map[nxt]; |
| 75 | + map[nxt] = temp; |
| 76 | + } |
| 77 | + |
| 78 | + for(int key : map[nxt].keySet()) { |
| 79 | + int newVal = map[cur].getOrDefault(key, 0) + map[nxt].get(key); |
| 80 | + if(newVal != size[key]) { |
| 81 | + map[cur].put(key, newVal); |
| 82 | + } |
| 83 | + else if(map[cur].containsKey(key)) { |
| 84 | + map[cur].remove(key); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | +} |
| 90 | +``` |
0 commit comments