|
| 1 | +```java |
| 2 | +import java.util.*; |
| 3 | +import java.io.*; |
| 4 | + |
| 5 | +class DisjointSet { |
| 6 | + int[] root, cnt; |
| 7 | + Stack<int[]> works; |
| 8 | + DisjointSet(int size) { |
| 9 | + root = new int[size]; |
| 10 | + cnt = new int[size]; |
| 11 | + for(int i=0;i<size;i++) { |
| 12 | + root[i] = i; |
| 13 | + cnt[i] = 1; |
| 14 | + } |
| 15 | + works = new Stack<>(); |
| 16 | + } |
| 17 | + |
| 18 | + int find(int x) { return x == root[x] ? x : find(root[x]); } |
| 19 | + |
| 20 | + void union(int a, int b) { |
| 21 | + int x = find(a), y = find(b); |
| 22 | + if(x == y) { |
| 23 | + works.push(new int[]{-1,-1,-1}); |
| 24 | + return; |
| 25 | + } |
| 26 | + if(cnt[x] > cnt[y]) { |
| 27 | + int temp = x; |
| 28 | + x = y; |
| 29 | + y = temp; |
| 30 | + } |
| 31 | + works.push(new int[]{x, y, cnt[x]}); |
| 32 | + cnt[y] += cnt[x]; |
| 33 | + root[x] = y; |
| 34 | + } |
| 35 | + |
| 36 | + void rollback() { |
| 37 | + if(!works.empty()) { |
| 38 | + int[] info = works.pop(); |
| 39 | + int x = info[0], y = info[1], c = info[2]; |
| 40 | + if(x != -1) { |
| 41 | + root[x] = x; |
| 42 | + cnt[y] -= c; |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +public class Main { |
| 49 | + |
| 50 | + static BufferedReader br; |
| 51 | + static BufferedWriter bw; |
| 52 | + static StringTokenizer st; |
| 53 | + |
| 54 | + static int N, K; |
| 55 | + static List<int[]>[] edges; |
| 56 | + static int[][] finds; |
| 57 | + static Map<String, Integer> edgeTime; |
| 58 | + static DisjointSet ds; |
| 59 | + |
| 60 | + public static void main(String[] args) throws Exception { |
| 61 | + bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 62 | + |
| 63 | + input(); |
| 64 | + solve(); |
| 65 | + |
| 66 | + bw.close(); |
| 67 | + } |
| 68 | + |
| 69 | + public static void input() throws Exception { |
| 70 | + br = new BufferedReader(new InputStreamReader(System.in)); |
| 71 | + |
| 72 | + st = new StringTokenizer(br.readLine()); |
| 73 | + N = Integer.parseInt(st.nextToken()); |
| 74 | + K = Integer.parseInt(st.nextToken()); |
| 75 | + edges = new List[4*K]; |
| 76 | + finds = new int[K+1][2]; |
| 77 | + for(int i=1;i<=K;i++) finds[i] = new int[]{-1,-1}; |
| 78 | + for(int i=1;i<4*K;i++) edges[i] = new ArrayList<>(); |
| 79 | + edgeTime = new TreeMap<>(); |
| 80 | + for(int i=1;i<=K;i++) { |
| 81 | + st = new StringTokenizer(br.readLine()); |
| 82 | + int op = Integer.parseInt(st.nextToken()); |
| 83 | + int a = Integer.parseInt(st.nextToken()); |
| 84 | + int b = Integer.parseInt(st.nextToken()); |
| 85 | + if(op == 3) finds[i] = new int[]{a,b}; |
| 86 | + else { |
| 87 | + if(op == 1) edgeTime.put(a + " " + b, i); |
| 88 | + else { |
| 89 | + int start = edgeTime.get(a + " " + b); |
| 90 | + edgeTime.remove(a + " " + b); |
| 91 | + update(1,K,start,i,a,b,1); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + for(String key : edgeTime.keySet()) { |
| 96 | + int start = edgeTime.get(key); |
| 97 | + String[] sp = key.split(" "); |
| 98 | + int a = Integer.parseInt(sp[0]); |
| 99 | + int b = Integer.parseInt(sp[1]); |
| 100 | + update(1,K,start,K,a,b,1); |
| 101 | + } |
| 102 | + |
| 103 | + br.close(); |
| 104 | + } |
| 105 | + |
| 106 | + public static void update(int s, int e, int l, int r, int a, int b, int n) { |
| 107 | + if(l>r || l>e || r<s) return; |
| 108 | + if(l<=s && e<=r) { |
| 109 | + edges[n].add(new int[]{a, b}); |
| 110 | + return; |
| 111 | + } |
| 112 | + int m = (s+e)>>1; |
| 113 | + update(s,m,l,r,a,b,n*2); |
| 114 | + update(m+1,e,l,r,a,b,n*2+1); |
| 115 | + } |
| 116 | + |
| 117 | + public static void solve() throws Exception { |
| 118 | + ds = new DisjointSet(N); |
| 119 | + dnc(1,K,1); |
| 120 | + } |
| 121 | + |
| 122 | + public static void dnc(int s, int e, int n) throws Exception { |
| 123 | + for(int[] edge : edges[n]) { |
| 124 | + int a = edge[0], b = edge[1]; |
| 125 | + ds.union(a,b); |
| 126 | + } |
| 127 | + |
| 128 | + if(s == e) { |
| 129 | + if(finds[s][0] != -1) { |
| 130 | + int a = finds[s][0], b = finds[s][1]; |
| 131 | + boolean result = ds.find(a) == ds.find(b); |
| 132 | + bw.write(result ? "YES\n" : "NO\n"); |
| 133 | + } |
| 134 | + } |
| 135 | + else { |
| 136 | + int m = (s+e)>>1; |
| 137 | + dnc(s,m,n*2); |
| 138 | + dnc(m+1,e,n*2+1); |
| 139 | + } |
| 140 | + |
| 141 | + for(int[] edge : edges[n]) { |
| 142 | + ds.rollback(); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | +} |
| 147 | +``` |
0 commit comments