結果

問題 No.416 旅行会社
ユーザー lloyzlloyz
提出日時 2023-12-21 00:01:02
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,097 ms / 4,000 ms
コード長 2,985 bytes
コンパイル時間 203 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 161,060 KB
最終ジャッジ日時 2023-12-21 00:01:17
合計ジャッジ時間 14,367 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 377 ms
128,600 KB
testcase_01 AC 44 ms
55,608 KB
testcase_02 AC 42 ms
55,608 KB
testcase_03 AC 43 ms
55,608 KB
testcase_04 AC 44 ms
55,608 KB
testcase_05 AC 44 ms
55,608 KB
testcase_06 AC 46 ms
57,676 KB
testcase_07 AC 69 ms
69,800 KB
testcase_08 AC 141 ms
77,960 KB
testcase_09 AC 294 ms
82,932 KB
testcase_10 AC 365 ms
128,624 KB
testcase_11 AC 359 ms
130,484 KB
testcase_12 AC 362 ms
124,528 KB
testcase_13 AC 358 ms
128,856 KB
testcase_14 AC 929 ms
161,060 KB
testcase_15 AC 1,022 ms
158,884 KB
testcase_16 AC 946 ms
154,016 KB
testcase_17 AC 1,097 ms
159,352 KB
testcase_18 AC 999 ms
158,116 KB
testcase_19 AC 982 ms
145,468 KB
testcase_20 AC 876 ms
146,236 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict

class UnionFind():
    def __init__(self, n):
        '''
        UnionFindクラス。nは要素数を表す。
        '''
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        '''
        要素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を含む集合と要素yを含む集合を合体する関数。
        基本的には、要素数が多い集合に統合される。
        要素数が同じときは要素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):
        '''
        要素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())

n, m, q = map(int, input().split())
L = set()
for _ in range(m):
    u, v = map(int, input().split())
    L.add((u - 1, v - 1))
Q = []
for _ in range(q):
    u, v = map(int, input().split())
    L.remove((u - 1, v - 1))
    Q.append((u - 1, v - 1))
UF = UnionFind(n)
M = [set([i]) for i in range(n)]
root_1 = 0
for u, v in L:
    if UF.same(u, v):
        continue
    uroot = UF.find(u)
    vroot = UF.find(v)
    UF.union(u, v)
    root = UF.find(u)
    if root == uroot:
        M[root] |= M[vroot]
        M[vroot] = set()
    else:
        M[root] |= M[uroot]
        M[uroot] = set()
    if 0 in M[root]:
        root_1 = root
ANS = [0 for _ in range(n)]
for u in M[root_1]:
    ANS[u] = -1
for i in range(q - 1, -1, -1):
    u, v = Q[i]
    if UF.same(u, v):
        continue
    uroot = UF.find(u)
    vroot = UF.find(v)
    if 0 in M[uroot]:
        for j in M[vroot]:
            ANS[j] = i + 1
    elif 0 in M[vroot]:
        for j in M[uroot]:
            ANS[j] = i + 1
    UF.union(u, v)
    root = UF.find(u)
    if root == uroot:
        M[root] |= M[vroot]
        M[vroot] = set()
    else:
        M[root] |= M[uroot]
        M[uroot] = set()
print(*ANS[1:], sep='\n')
0