結果

問題 No.2290 UnUnion Find
ユーザー FromBooskaFromBooska
提出日時 2023-05-06 08:11:33
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,479 bytes
コンパイル時間 1,657 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 87,168 KB
最終ジャッジ日時 2024-05-02 22:50:44
合計ジャッジ時間 10,499 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
57,856 KB
testcase_01 AC 42 ms
52,096 KB
testcase_02 AC 540 ms
76,288 KB
testcase_03 TLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * 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 unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        if self.parents[x] > self.parents[y]:
            x, y = y, x
        self.parents[x] += self.parents[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 group_count(self):
        return len(self.roots())
 
    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())


# 公式解説より
# ルートのうち、最小番号の頂点と最大番号の頂点を管理
# uniteするたびに、それぞれを更新
# 非連結頂点は、そこから出力すればいい

N, Q = map(int, input().split())
UF = UnionFind(N+1)
smallest, largest = 1, N
for i in range(Q):
    query = list(map(int, input().split()))
    if query[0] == 1:
        u, v = query[1], query[2] 
        UF.unite(u, v)
        if UF.same(u, smallest) == True:
            j = 1
            while smallest+j <= N:
                if UF.find(smallest+j) == j:
                    smallest += j
                    break
                else:
                    j += 1
        if UF.same(u, largest) == True:
            j = 1
            while largest - j >= 1:
                if UF.find(largest-j) == j:
                    largest -= j
                    break
                else:
                    j += 1
        
    elif query[0] == 2:
        v = query[1]
        if UF.size(v) == N:
            print(-1)
        else:
            if UF.same(v, smallest) == True:
                print(largest)
            else:
                print(smallest)
    #print('smallest', smallest, 'largest', largest)
0