結果
| 問題 |
No.2290 UnUnion Find
|
| コンテスト | |
| ユーザー |
buey_t
|
| 提出日時 | 2023-05-08 13:09:42 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 552 ms / 2,000 ms |
| コード長 | 2,671 bytes |
| コンパイル時間 | 255 ms |
| コンパイル使用メモリ | 82,132 KB |
| 実行使用メモリ | 102,864 KB |
| 最終ジャッジ日時 | 2024-11-25 06:16:10 |
| 合計ジャッジ時間 | 21,183 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 46 |
ソースコード
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)
buey_t