結果

問題 No.416 旅行会社
ユーザー NoneNone
提出日時 2021-03-26 05:45:58
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 805 ms / 4,000 ms
コード長 3,134 bytes
コンパイル時間 1,336 ms
コンパイル使用メモリ 86,728 KB
実行使用メモリ 130,796 KB
最終ジャッジ日時 2023-08-21 10:45:45
合計ジャッジ時間 10,680 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 300 ms
107,684 KB
testcase_01 AC 75 ms
70,976 KB
testcase_02 AC 76 ms
70,812 KB
testcase_03 AC 76 ms
70,868 KB
testcase_04 AC 77 ms
70,896 KB
testcase_05 AC 78 ms
70,944 KB
testcase_06 AC 74 ms
70,856 KB
testcase_07 AC 85 ms
75,108 KB
testcase_08 AC 140 ms
77,812 KB
testcase_09 AC 235 ms
81,692 KB
testcase_10 AC 309 ms
108,228 KB
testcase_11 AC 276 ms
106,760 KB
testcase_12 AC 287 ms
106,644 KB
testcase_13 AC 267 ms
106,436 KB
testcase_14 AC 805 ms
130,796 KB
testcase_15 AC 725 ms
126,668 KB
testcase_16 AC 651 ms
126,948 KB
testcase_17 AC 752 ms
128,796 KB
testcase_18 AC 766 ms
129,076 KB
testcase_19 AC 619 ms
113,628 KB
testcase_20 AC 621 ms
117,296 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
        self.groups = [[i] for i in range(self.n)]

    def find(self, x):
        """ 根を見つける関数を定義(同時にxを直接根にくっつける操作も行う)"""
        tmp = []
        parents = self.parents
        while parents[x] >= 0:
            tmp.append(x)
            x = parents[x]
        for y in tmp:
            parents[y] = x
        return x

    def union(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
        while self.groups[y]:
            p = self.groups[y].pop()
            self.groups[x].append(p)
        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def same(self, x, y):
        """ xとyが同じ根の子かを判定 """
        return self.find(x) == self.find(y)

    def size(self, x):
        """ xの根のparent(= 要素数)を返す """
        return -self.parents[self.find(x)]

    def members(self, x):
        """ xが属するグループの要素をリストとして返す O(N)、償却計算量 O(1) """
        root = self.find(x)
        return self.groups[root]

    def roots(self):
        """ 全ての根の要素をリストとして返す O(N)"""
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        """ グループの数を返す O(N)"""
        return len(self.roots())

    def size_list(self):
        """ 各グループの要素数のリストを返す(根の番号は返さない) O(N)"""
        return [-x for x in self.parents if x < 0]

    def all_group_members(self):
        """ {根:[根の子(根を含む)のリスト],...}を辞書で返す O(N)"""
        res = [[] for _ in range(self.n)]
        for i in range(self.n):
            x = self.find(i)
            res[x].append(i)
        return {r: res[r] for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

##############################################################################################################
import sys
input = sys.stdin.readline


N, M, Q = map(int, input().split())
U = UnionFind(N)
data=set()
for _ in range(M):
    a, b = map(int, input().split())
    a, b = a-1, b-1
    if a<b: a,b=b,a
    data.add((a, b))

data2=[]
for _ in range(Q):
    a, b = map(int, input().split())
    a, b = a-1, b-1
    if a<b: a,b=b,a
    data2.append((a, b))
    data.remove((a,b))

data2.reverse()

for a,b in data:
    U.union(a,b)

res=[0]*N

for i in U.members(0):
    res[i]=-1

time=Q
for a,b in data2:
    a0,b0=U.same(a,0),U.same(b,0)
    if a0==True and b0==False:
        for i in U.members(b):
            res[i]=time
    elif b0==True and a0==False:
        for i in U.members(a):
            res[i]=time
    U.union(a,b)
    time-=1

print(*res[1:], sep="\n")
0