|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +public class BOJ29851 { |
| 6 | + |
| 7 | + static class Query { |
| 8 | + int type, a, b; |
| 9 | + Query(int a) { |
| 10 | + this.type = 0; |
| 11 | + this.a = a; |
| 12 | + } |
| 13 | + Query(int a, int b) { |
| 14 | + this.type = 1; |
| 15 | + this.a = a; |
| 16 | + this.b = b; |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 21 | + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 22 | + static StringTokenizer st; |
| 23 | + |
| 24 | + static int N, Q; |
| 25 | + static Query[] queries; |
| 26 | + static BitSet cantUnion; |
| 27 | + static List<Integer>[] tree; |
| 28 | + static int[] root, parent, depth; |
| 29 | + |
| 30 | + public static int find(int x) { |
| 31 | + return x == root[x] ? x : (root[x] = find(root[x])); |
| 32 | + } |
| 33 | + |
| 34 | + public static boolean union(int a, int b) { |
| 35 | + int x = find(a), y = find(b); |
| 36 | + if(x == y) return false; |
| 37 | + root[x] = y; |
| 38 | + return true; |
| 39 | + } |
| 40 | + |
| 41 | + public static void main(String[] args) throws Exception { |
| 42 | + // 0. Input + Construct Tree |
| 43 | + |
| 44 | + st = new StringTokenizer(br.readLine()); |
| 45 | + N = Integer.parseInt(st.nextToken()); |
| 46 | + Q = Integer.parseInt(st.nextToken()); |
| 47 | + |
| 48 | + queries = new Query[Q]; |
| 49 | + cantUnion = new BitSet(Q); |
| 50 | + tree = new List[N+1]; |
| 51 | + root = new int[N+1]; |
| 52 | + for(int i=1;i<=N;i++) { |
| 53 | + root[i] = i; |
| 54 | + tree[i] = new ArrayList<>(); |
| 55 | + } |
| 56 | + for(int i=0;i<Q;i++) { |
| 57 | + st = new StringTokenizer(br.readLine()); |
| 58 | + if ("+".equals(st.nextToken())) { |
| 59 | + int a = Integer.parseInt(st.nextToken()); |
| 60 | + int b = Integer.parseInt(st.nextToken()); |
| 61 | + if(union(a,b)) { |
| 62 | + tree[a].add(b); |
| 63 | + tree[b].add(a); |
| 64 | + } |
| 65 | + else { |
| 66 | + cantUnion.set(i); |
| 67 | + } |
| 68 | + queries[i] = new Query(a, b); |
| 69 | + } |
| 70 | + else { |
| 71 | + queries[i] = new Query(Integer.parseInt(st.nextToken())); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + // 1. DFS |
| 76 | + parent = new int[N+1]; |
| 77 | + depth = new int[N+1]; |
| 78 | + dfs(1, 0, 0); |
| 79 | + |
| 80 | + // 2. Count Non-bridge |
| 81 | + |
| 82 | + root = new int[N+1]; |
| 83 | + for(int i=1;i<=N;i++) { |
| 84 | + root[i] = i; |
| 85 | + } |
| 86 | + int nonBridges = 0; |
| 87 | + for(int i=0;i<Q;i++) { |
| 88 | + Query query = queries[i]; |
| 89 | + int type = query.type, a = query.a, b = query.b; |
| 90 | + if(type == 1) { |
| 91 | + if(!cantUnion.get(i)) continue; |
| 92 | + a = find(a); |
| 93 | + b = find(b); |
| 94 | + while(a != b) { |
| 95 | + nonBridges++; |
| 96 | + if(depth[a] > depth[b]) { |
| 97 | + union(a, parent[a]); |
| 98 | + a = find(parent[a]); |
| 99 | + } |
| 100 | + else { |
| 101 | + union(b, parent[b]); |
| 102 | + b = find(parent[b]); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + else { |
| 107 | + if(nonBridges <= a && a <= N-1) { |
| 108 | + bw.write("JAH\n"); |
| 109 | + } |
| 110 | + else { |
| 111 | + bw.write("EI\n"); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + bw.close(); |
| 116 | + } |
| 117 | + |
| 118 | + public static void dfs(int cur, int par, int dep) { |
| 119 | + parent[cur] = par; |
| 120 | + depth[cur] = dep; |
| 121 | + for(int nxt : tree[cur]) if(nxt != par) { |
| 122 | + dfs(nxt, cur, dep+1); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | +} |
| 127 | +``` |
0 commit comments