|
| 1 | +# https://maang.in/problems/Easy-Graph-Queries-400?resourceUrl=cs214-cp873-pl3337-rs400&returnUrl=%5B%22%2Fcourses%2FGraph-Level-1-214%3Ftab%3Dchapters%22%5D |
| 2 | + |
| 3 | +""" |
| 4 | +Description |
| 5 | +You are given an undirected graph G with N nodes and M edges. You have to answer Q queries. Each query is one of the following two types. |
| 6 | +
|
| 7 | +Type 1: 1 X — print the size of the connected component containing node |
| 8 | +Type 2: 2 X Y — print YES if nodes X and Y are in the same connected component, otherwise print NO. |
| 9 | +
|
| 10 | +Input Format |
| 11 | +The first line contains three space-separated integers |
| 12 | +N, M, Q |
| 13 | +The next M lines each contain two integers u, v denoting an undirected edge between nodes u and v. |
| 14 | +
|
| 15 | +Each of the next Q lines contains a single query in one of the formats: 1X 2 X Y |
| 16 | +
|
| 17 | +Output Format |
| 18 | +Print |
| 19 | +Q lines as the answers to the |
| 20 | +Q |
| 21 | +Q queries, each on a new line. |
| 22 | +Constraints |
| 23 | +1≤N,M,Q≤105 |
| 24 | +
|
| 25 | +Sample Input 1 |
| 26 | +6 5 5 |
| 27 | +1 2 |
| 28 | +2 3 |
| 29 | +1 3 |
| 30 | +4 4 |
| 31 | +5 6 |
| 32 | +1 2 |
| 33 | +1 4 |
| 34 | +2 3 4 |
| 35 | +1 5 |
| 36 | +2 5 6 |
| 37 | +Sample Output 1 |
| 38 | +3 |
| 39 | +1 |
| 40 | +NO |
| 41 | +2 |
| 42 | +YES |
| 43 | +""" |
| 44 | + |
| 45 | +import sys |
| 46 | +from collections import defaultdict |
| 47 | + |
| 48 | +input = sys.stdin.readline |
| 49 | +print = sys.stdout.write |
| 50 | +sys.setrecursionlimit(1 << 25) |
| 51 | + |
| 52 | +MOD = 10**9 + 7 |
| 53 | +INF = 10**18 |
| 54 | + |
| 55 | + |
| 56 | +def read(): |
| 57 | + return sys.stdin.buffer.read() |
| 58 | + |
| 59 | + |
| 60 | +def ints(): |
| 61 | + return map(int, input().split()) |
| 62 | + |
| 63 | + |
| 64 | +def list_ints(): |
| 65 | + return list(map(int, input().split())) |
| 66 | + |
| 67 | + |
| 68 | +def write_line(value=""): |
| 69 | + sys.stdout.write(str(value) + "\n") |
| 70 | + |
| 71 | + |
| 72 | +def write_lines(values): |
| 73 | + sys.stdout.write("\n".join(map(str, values))) |
| 74 | + if values: |
| 75 | + sys.stdout.write("\n") |
| 76 | + |
| 77 | + |
| 78 | +def solve(): |
| 79 | + [n, m, q] = list_ints() |
| 80 | + adj_list = defaultdict(list) |
| 81 | + for i in range(m): |
| 82 | + [u, v] = list_ints() |
| 83 | + if u != v: |
| 84 | + adj_list[u].append(v) |
| 85 | + adj_list[v].append(u) |
| 86 | + |
| 87 | + comp_id = [-1] * (n + 1) |
| 88 | + cid = 0 |
| 89 | + comp_size = [] |
| 90 | + |
| 91 | + for node in range(1, n + 1): |
| 92 | + if comp_id[node] == -1: |
| 93 | + size = 0 |
| 94 | + stack = [node] |
| 95 | + comp_id[node] = cid |
| 96 | + |
| 97 | + while stack: |
| 98 | + child = stack.pop() |
| 99 | + size += 1 |
| 100 | + for nei in adj_list[child]: |
| 101 | + if comp_id[nei] == -1: |
| 102 | + comp_id[nei] = cid |
| 103 | + stack.append(nei) |
| 104 | + cid += 1 |
| 105 | + comp_size.append(size) |
| 106 | + |
| 107 | + for _ in range(q): |
| 108 | + query_list = list_ints() |
| 109 | + if len(query_list) == 2: |
| 110 | + write_line(comp_size[comp_id[query_list[1]]]) |
| 111 | + else: |
| 112 | + if comp_id[query_list[1]] == comp_id[query_list[2]]: |
| 113 | + write_line("YES") |
| 114 | + else: |
| 115 | + write_line("NO") |
| 116 | + |
| 117 | + |
| 118 | +if __name__ == "__main__": |
| 119 | + solve() |
| 120 | + |
| 121 | +""" |
| 122 | +No recursive DFS because of the stack overflow risk |
| 123 | +✅ Time Complexity |
| 124 | +Build graph: O(N + M) |
| 125 | +DFS traversal (all nodes): O(N + M) |
| 126 | +Each query: |
| 127 | +Type 1 (size): O(1) |
| 128 | +Type 2 (connected?): O(1) |
| 129 | +
|
| 130 | +👉 Total: |
| 131 | +
|
| 132 | +O(N+M+Q) |
| 133 | +✅ Space Complexity |
| 134 | +Adjacency list: O(N + M) |
| 135 | +Component array (comp_id): O(N) |
| 136 | +Component sizes: O(N) (worst case) |
| 137 | +
|
| 138 | +👉 Total: |
| 139 | +
|
| 140 | +O(N+M) |
| 141 | +""" |
| 142 | + |
| 143 | + |
| 144 | +import sys |
| 145 | +from collections import defaultdict |
| 146 | + |
| 147 | +input = sys.stdin.readline |
| 148 | +print = sys.stdout.write |
| 149 | +sys.setrecursionlimit(1 << 25) |
| 150 | + |
| 151 | +MOD = 10**9 + 7 |
| 152 | +INF = 10**18 |
| 153 | + |
| 154 | + |
| 155 | +def read(): |
| 156 | + return sys.stdin.buffer.read() |
| 157 | + |
| 158 | + |
| 159 | +def ints(): |
| 160 | + return map(int, input().split()) |
| 161 | + |
| 162 | + |
| 163 | +def list_ints(): |
| 164 | + return list(map(int, input().split())) |
| 165 | + |
| 166 | + |
| 167 | +def write_line(value=""): |
| 168 | + sys.stdout.write(str(value) + "\n") |
| 169 | + |
| 170 | + |
| 171 | +def write_lines(values): |
| 172 | + sys.stdout.write("\n".join(map(str, values))) |
| 173 | + if values: |
| 174 | + sys.stdout.write("\n") |
| 175 | + |
| 176 | + |
| 177 | +class DSU: |
| 178 | + def __init__(self, n): |
| 179 | + self.parent = list(range(n + 1)) |
| 180 | + self.size = [1] * (n + 1) |
| 181 | + |
| 182 | + def find(self, x): |
| 183 | + while self.parent[x] != x: |
| 184 | + self.parent[x] = self.parent[self.parent[x]] |
| 185 | + x = self.parent[x] |
| 186 | + return x |
| 187 | + |
| 188 | + def union(self, x, y): |
| 189 | + ra = self.find(x) |
| 190 | + rb = self.find(y) |
| 191 | + |
| 192 | + if ra == rb: |
| 193 | + return |
| 194 | + |
| 195 | + if self.size[ra] < self.size[rb]: |
| 196 | + ra, rb = rb, ra |
| 197 | + |
| 198 | + self.parent[rb] = ra |
| 199 | + self.size[ra] += self.size[rb] |
| 200 | + |
| 201 | + def component_size(self, x): |
| 202 | + return self.size[self.find(x)] |
| 203 | + |
| 204 | + |
| 205 | +def solve(): |
| 206 | + [n, m, q] = list_ints() |
| 207 | + dsu = DSU(n) |
| 208 | + for i in range(m): |
| 209 | + [u, v] = list_ints() |
| 210 | + dsu.union(u, v) |
| 211 | + |
| 212 | + for _ in range(q): |
| 213 | + query_list = list_ints() |
| 214 | + if len(query_list) == 2: |
| 215 | + write_line(dsu.component_size(query_list[1])) |
| 216 | + else: |
| 217 | + if dsu.find(query_list[1]) == dsu.find(query_list[2]): |
| 218 | + write_line("YES") |
| 219 | + else: |
| 220 | + write_line("NO") |
| 221 | + |
| 222 | + |
| 223 | +if __name__ == "__main__": |
| 224 | + solve() |
| 225 | + |
| 226 | +""" |
| 227 | +✅ Time Complexity |
| 228 | +Each union / find: O(α(N)) (inverse Ackermann, ~constant) |
| 229 | +Processing edges: O(M α(N)) |
| 230 | +Each query: O(α(N)) |
| 231 | +
|
| 232 | +👉 Total: |
| 233 | +
|
| 234 | +O((N+M+Q)α(N))≈O(N+M+Q) |
| 235 | +✅ Space Complexity |
| 236 | +Parent array: O(N) |
| 237 | +Size array: O(N) |
| 238 | +
|
| 239 | +👉 Total: |
| 240 | +
|
| 241 | +O(N) |
| 242 | +""" |
0 commit comments