結果
問題 | No.2290 UnUnion Find |
ユーザー | buey_t |
提出日時 | 2023-05-08 13:09:42 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 659 ms / 2,000 ms |
コード長 | 2,671 bytes |
コンパイル時間 | 295 ms |
コンパイル使用メモリ | 82,432 KB |
実行使用メモリ | 102,912 KB |
最終ジャッジ日時 | 2024-05-04 00:35:20 |
合計ジャッジ時間 | 26,251 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 160 ms
89,184 KB |
testcase_01 | AC | 159 ms
89,216 KB |
testcase_02 | AC | 234 ms
90,112 KB |
testcase_03 | AC | 298 ms
95,488 KB |
testcase_04 | AC | 321 ms
95,772 KB |
testcase_05 | AC | 326 ms
95,872 KB |
testcase_06 | AC | 312 ms
98,304 KB |
testcase_07 | AC | 316 ms
95,616 KB |
testcase_08 | AC | 307 ms
95,616 KB |
testcase_09 | AC | 305 ms
98,304 KB |
testcase_10 | AC | 310 ms
98,176 KB |
testcase_11 | AC | 306 ms
95,616 KB |
testcase_12 | AC | 314 ms
95,872 KB |
testcase_13 | AC | 316 ms
98,048 KB |
testcase_14 | AC | 317 ms
95,744 KB |
testcase_15 | AC | 324 ms
95,744 KB |
testcase_16 | AC | 322 ms
95,488 KB |
testcase_17 | AC | 320 ms
95,488 KB |
testcase_18 | AC | 330 ms
95,872 KB |
testcase_19 | AC | 646 ms
98,176 KB |
testcase_20 | AC | 543 ms
95,360 KB |
testcase_21 | AC | 418 ms
91,976 KB |
testcase_22 | AC | 580 ms
96,004 KB |
testcase_23 | AC | 545 ms
95,200 KB |
testcase_24 | AC | 577 ms
102,912 KB |
testcase_25 | AC | 453 ms
92,956 KB |
testcase_26 | AC | 423 ms
93,696 KB |
testcase_27 | AC | 483 ms
93,952 KB |
testcase_28 | AC | 567 ms
96,840 KB |
testcase_29 | AC | 546 ms
100,864 KB |
testcase_30 | AC | 472 ms
92,804 KB |
testcase_31 | AC | 565 ms
102,208 KB |
testcase_32 | AC | 483 ms
93,028 KB |
testcase_33 | AC | 528 ms
96,128 KB |
testcase_34 | AC | 436 ms
94,080 KB |
testcase_35 | AC | 555 ms
94,916 KB |
testcase_36 | AC | 514 ms
95,048 KB |
testcase_37 | AC | 602 ms
98,604 KB |
testcase_38 | AC | 515 ms
93,324 KB |
testcase_39 | AC | 530 ms
94,888 KB |
testcase_40 | AC | 508 ms
94,364 KB |
testcase_41 | AC | 506 ms
94,492 KB |
testcase_42 | AC | 654 ms
100,540 KB |
testcase_43 | AC | 659 ms
100,828 KB |
testcase_44 | AC | 321 ms
95,488 KB |
testcase_45 | AC | 316 ms
94,080 KB |
testcase_46 | AC | 351 ms
95,104 KB |
ソースコード
from math import sqrt,sin,cos,tan,ceil,radians,floor,gcd,exp,log,log10,log2,factorial,fsum from heapq import heapify, heappop, heappush from bisect import bisect_left, bisect_right from copy import deepcopy import copy import random from random import randrange from collections import deque,Counter,defaultdict from itertools import permutations,combinations from decimal import Decimal,ROUND_HALF_UP #tmp = Decimal(mid).quantize(Decimal('0'), rounding=ROUND_HALF_UP) from functools import lru_cache, reduce #@lru_cache(maxsize=None) from operator import add,sub,mul,xor,and_,or_,itemgetter import sys input = sys.stdin.readline # .rstrip() INF = 10**18 mod1 = 10**9+7 mod2 = 998244353 #DecimalならPython #再帰ならPython!!!!!!!!!!!!!!!!!!!!!!!!!! class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * n #親だけ self.edge = [0]*n self.count = n def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] def union(self, x, y): x = self.find(x) y = self.find(y) if self.parents[x] > self.parents[y]: x, y = y, x self.edge[x] += 1 if x == y: return self.count -= 1 self.parents[x] += self.parents[y] self.edge[x] += self.edge[y] self.parents[y] = x def size(self, x): return -self.parents[self.find(x)] def same(self, x, y): return self.find(x) == self.find(y) def members(self, x): root = self.find(x) return [i for i in range(self.n) if self.find(i) == root] def roots(self): return [i for i, x in enumerate(self.parents) if x < 0] def all_group_members(self): group_members = defaultdict(list) for member in range(self.n): group_members[self.find(member)].append(member) return group_members def __str__(self): return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items()) ''' ''' N,Q = map(int, input().split()) uf = UnionFind(N+1) l = 1 r = N for i in range(Q): tmp = list(map(int, input().split())) if tmp[0] == 1: t,u,v = tmp uf.union(u,v) while uf.find(l) != l: l += 1 while uf.find(r) != r: r -= 1 else: t,v = tmp if l == r: print(-1) else: if uf.same(r,v): print(l) else: print(r)