publicstaticvoidmain(String[] args) { Scannersc=newScanner(System.in); Set<int[]> edge = newHashSet<>(); Set<int[]> delEdge = newHashSet<>(); intn= sc.nextInt(), m = sc.nextInt(), q = sc.nextInt(); for (inti=0; i < m; i++) { intu= sc.nextInt(), v = sc.nextInt(); edge.add(newint[] { u, v }); } List<int[]> ops = newArrayList<>(); for (inti=0; i < q; i++) { intop= sc.nextInt(), u = sc.nextInt(), v = sc.nextInt(); if (op == 1) { delEdge.add(newint[] { u, v }); delEdge.add(newint[] { v, u }); } ops.add(newint[] { op, u, v }); }
solve(n, edge, delEdge, ops);
sc.close(); }
staticvoidsolve(int n, Set<int[]> edge, Set<int[]> delEdge, List<int[]> ops) { UnionFinduf=newUnionFind(n + 1); // 并查集建边(最终的并查集) for (int[] e : edge) { booleanflag=false; for (int[] de : delEdge) { if (e[0] == de[0] && e[1] == de[1]) { flag = true; break; } } if (flag) continue; uf.join(e[0], e[1]); }
// 倒序遍历指令 intq= ops.size(); List<String> ans = newArrayList<>(); for (inti= q - 1; i >= 0; i--) { int[] op = ops.get(i); intu= op[1], v = op[2]; if (op[0] == 1) { // 加边 uf.join(u, v); } else { if (uf.isSame(u, v)) { ans.add("Yes"); } else { ans.add("No"); } } } for (inti= ans.size() - 1; i >= 0; i--) { System.out.println(ans.get(i)); } }
/** * 并查集初始化 */ publicvoidinit() { for (inti=0; i < n; i++) { father[i] = i; } }
/** * 寻找根节点(路径压缩) * * @param u 节点 * @return u 的根节点 */ publicintfind(int u) { if (u == father[u]) return u; else return father[u] = find(father[u]); }
/** * 判断u和v是否能找到同一个根节点 * * @param u u 节点 * @param v v 节点 * @return u v 是否同根 */ publicbooleanisSame(int u, int v) { u = find(u); v = find(v); return u == v; }
/** * 将 v -> u 这条边加入并查集 * * @param u u 节点 * @param v v 节点 */ publicvoidjoin(int u, int v) { // 寻根 u = find(u); v = find(v); // 若根相同,说明在同一个集合内,不用将两个节点相连,直接返回 if (u == v) return; father[v] = u; }
}
classUnionFind: def__init__(self, n=1005): self.n = n self.father = [i for i inrange(n)]
deffind(self, u: int): if u == self.father[u]: return u else: self.father[u] = self.find(self.father[u]) return self.father[u]
defisSame(self, u: int, v: int): u = self.find(u) v = self.find(v) return u == v
defjoin(self, u: int, v: int): u = self.find(u) v = self.find(v) if u == v: return self.father[v] = u
defsolve(n: int, edge: list, delEdge: list, ops: list) -> None: uf = UnionFind(n + 1) for e in edge: flag = False for de in delEdge: if e[0] == de[0] and e[1] == de[1]: flag = True break if flag: continue uf.join(e[0], e[1])
ans = [] for op inreversed(ops): u = op[1] v = op[2] if op[0] == 1: uf.join(u, v) else: if uf.isSame(u, v): ans.append("Yes") else: ans.append("No")
for a inreversed(ans): print(a)
n, m, q = map(int, input().split()) edge, delEdge, ops = [], [], [] for _ inrange(m): u, v = map(int, input().split()) edge.append((u, v)) for _ inrange(q): op, u, v = map(int, input().split()) if op == 1: delEdge.append((u, v)) delEdge.append((v, u)) ops.append((op, u, v))