結果

問題 No.416 旅行会社
ユーザー AEnAEn
提出日時 2023-05-30 01:48:30
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,770 ms / 4,000 ms
コード長 3,270 bytes
コンパイル時間 286 ms
コンパイル使用メモリ 87,124 KB
実行使用メモリ 185,024 KB
最終ジャッジ日時 2023-08-28 00:18:30
合計ジャッジ時間 17,790 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 418 ms
133,220 KB
testcase_01 AC 178 ms
79,928 KB
testcase_02 AC 167 ms
79,928 KB
testcase_03 AC 168 ms
80,056 KB
testcase_04 AC 168 ms
80,116 KB
testcase_05 AC 166 ms
80,112 KB
testcase_06 AC 168 ms
79,916 KB
testcase_07 AC 178 ms
81,112 KB
testcase_08 AC 246 ms
84,920 KB
testcase_09 AC 415 ms
89,916 KB
testcase_10 AC 405 ms
133,228 KB
testcase_11 AC 427 ms
132,008 KB
testcase_12 AC 435 ms
132,496 KB
testcase_13 AC 385 ms
131,804 KB
testcase_14 AC 1,279 ms
176,232 KB
testcase_15 AC 1,523 ms
183,044 KB
testcase_16 AC 1,180 ms
175,992 KB
testcase_17 AC 1,770 ms
185,024 KB
testcase_18 AC 1,460 ms
180,024 KB
testcase_19 AC 1,193 ms
142,324 KB
testcase_20 AC 1,038 ms
140,684 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from typing import List

class UnionFind:
    """0-indexed"""
    def __init__(self, n):
        self.n = n
        self.parent = [-1] * n
        self.__group_count = n

    def unite(self, x, y) -> bool:
        """xとyをマージ"""
        x = self.root(x)
        y = self.root(y)
        if x == y:
            return False

        self.__group_count -= 1

        if self.parent[x] > self.parent[y]:
            x, y = y, x

        self.parent[x] += self.parent[y]
        self.parent[y] = x
        return True

    def is_same(self, x, y) -> bool:
        """xとyが同じ連結成分か判定"""
        return self.root(x) == self.root(y)

    def root(self, x) -> int:
        """xの根を取得"""
        if self.parent[x] < 0:
            return x
        else:
            # 経路圧縮あり
            # self.parent[x] = self.root(self.parent[x])
            # return self.parent[x]
            # 経路圧縮なし
            return self.root(self.parent[x])

    def size(self, x) -> int:
        """xが属する連結成分のサイズを取得"""
        return -self.parent[self.root(x)]

    def all_sizes(self) -> List[int]:
        """全連結成分のサイズのリストを取得 O(N)"""
        sizes = []
        for i in range(self.n):
            size = self.parent[i]
            if size < 0:
                sizes.append(-size)
        return sizes
    
    def members(self, x) -> List[int]:
        """xが属するグループのリストを返す O(N)"""
        mem = []
        r = self.root(x)
        for i in range(self.n):
            if self.root(i) == r:
                mem.append(i)
        return mem

    def groups(self) -> List[List[int]]:
        """全連結成分の内容のリストを取得 O(N・α(N))"""
        groups = dict()
        for i in range(self.n):
            p = self.root(i)
            if not groups.get(p):
                groups[p] = []
            groups[p].append(i)
        return list(groups.values())

    @property
    def group_count(self) -> int:
        """連結成分の数を取得 O(1)"""
        return self.__group_count

import sys
input = sys.stdin.readline

N,M,Q = map(int, input().split())
edge = [list(map(int, input().split())) for _ in range(M)]
query = []
s = set()
for i in range(Q):
    c,d = map(int, input().split())
    query.append([c,d])
    s.add((c,d))

ans = [0]*N
uf = UnionFind(N)
for a,b in edge:
    if (a,b) not in s:
        uf.unite(a-1,b-1)

g = [list() for _ in range(N)]
for i in range(N):
    g[uf.root(i)].append(i)

for num in g[uf.root(0)]:
    ans[num] = -1

for id,(c,d) in enumerate(query[::-1]):
    if uf.is_same(c-1,d-1):continue
    x,y = uf.root(c-1),uf.root(d-1)
    r0 = uf.root(0)
    uf.unite(c-1,d-1)
    r = uf.root(c-1)
    if r==x or (r==y and len(g[x])>len(g[y])):
        if y==r0:
            for v in g[x]:
                ans[v] = Q-id
        while g[y]:
            v = g[y].pop()
            g[x].append(v)
            if x==r0:
                ans[v] = Q-id
    else:
        if x==r0:
            for v in g[y]:
                ans[v] = Q-id
        while g[x]:
            v = g[x].pop()
            g[y].append(v)
            if y==r0:
                ans[v] = Q-id
print(*ans[1:],sep='\n')              
0