Submission #16567503
Source Code Expand
Copy
#!/usr/bin/env python3import syssys.setrecursionlimit(10**6)INF = 10 ** 9 + 1 # sys.maxsize # float("inf")MOD = 10 ** 9 + 7# dsu / UnionFinddef init_unionfind(N):global parent, rankparent = [-1] * Nrank = [0] * Ndef find_root_1(x):p = parent[x]if p == -1:return xp2 = find_root(p)parent[x] = p2
#!/usr/bin/env python3 import sys sys.setrecursionlimit(10**6) INF = 10 ** 9 + 1 # sys.maxsize # float("inf") MOD = 10 ** 9 + 7 # dsu / UnionFind def init_unionfind(N): global parent, rank parent = [-1] * N rank = [0] * N def find_root_1(x): p = parent[x] if p == -1: return x p2 = find_root(p) parent[x] = p2 return p2 def find_root(x): p = parent[x] while p != -1: x = p p = parent[x] return x def unite(x, y): x = find_root(x) y = find_root(y) if x == y: return # already united if rank[x] < rank[y]: parent[x] = y else: parent[y] = x if rank[x] == rank[y]: rank[x] += 1 def is_connected(x, y): return (find_root(x) == find_root(y)) # acl/dsu init = init_unionfind merge = unite # FIXME: return value same = is_connected leader = find_root def size(a): raise NotImplementedError def group(a): raise NotImplementedError # --- def debug(*x): print(*x, file=sys.stderr) def main(): # parse input N, Q = map(int, input().split()) init(N) for _q in range(Q): q, u, v = map(int, input().split()) if q == 0: merge(u, v) else: print(int(same(u, v))) # tests T1 = """ 4 7 1 0 1 0 0 1 0 2 3 1 0 1 1 1 2 0 0 2 1 1 3 """ TEST_T1 = """ >>> as_input(T1) >>> main() 0 1 0 1 """ def _test(): import doctest doctest.testmod() g = globals() for k in sorted(g): if k.startswith("TEST_"): doctest.run_docstring_examples(g[k], g, name=k) def as_input(s): "use in test, use given string as input file" import io f = io.StringIO(s.strip()) g = globals() g["input"] = lambda: bytes(f.readline(), "ascii") g["read"] = lambda: bytes(f.read(), "ascii") input = sys.stdin.buffer.readline read = sys.stdin.buffer.read if sys.argv[-1] == "-t": print("testing") _test() sys.exit() main()
Submission Info
Submission Time | |
---|---|
Task | A - Disjoint Set Union |
User | nishiohirokazu |
Language | PyPy3 (7.3.0) |
Score | 100 |
Code Size | 2111 Byte |
Status | AC |
Exec Time | 277 ms |
Memory | 75028 KB |
Judge Result
Set Name | Sample | All | ||||
---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 100 / 100 | ||||
Status |
|
|
Set Name | Test Cases |
---|---|
Sample | example_00 |
All | example_00, random_00, random_01, random_02, random_03, random_04, random_05, random_06, random_07, random_08, random_09 |
Case Name | Status | Exec Time | Memory |
---|---|---|---|
example_00 | AC | 68 ms | 61896 KB |
random_00 | AC | 239 ms | 73672 KB |
random_01 | AC | 254 ms | 74052 KB |
random_02 | AC | 235 ms | 73528 KB |
random_03 | AC | 118 ms | 72264 KB |
random_04 | AC | 205 ms | 70980 KB |
random_05 | AC | 236 ms | 73620 KB |
random_06 | AC | 202 ms | 74280 KB |
random_07 | AC | 94 ms | 70188 KB |
random_08 | AC | 164 ms | 71504 KB |
random_09 | AC | 277 ms | 75028 KB |