結果

問題 No.416 旅行会社
ユーザー Akijin_007Akijin_007
提出日時 2023-12-09 17:09:45
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 965 ms / 4,000 ms
コード長 2,338 bytes
コンパイル時間 240 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 129,440 KB
最終ジャッジ日時 2023-12-09 17:09:58
合計ジャッジ時間 11,611 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 258 ms
109,700 KB
testcase_01 AC 36 ms
53,460 KB
testcase_02 AC 35 ms
53,460 KB
testcase_03 AC 36 ms
53,460 KB
testcase_04 AC 36 ms
53,460 KB
testcase_05 AC 38 ms
53,460 KB
testcase_06 AC 39 ms
55,600 KB
testcase_07 AC 58 ms
69,784 KB
testcase_08 AC 171 ms
78,060 KB
testcase_09 AC 249 ms
81,004 KB
testcase_10 AC 267 ms
109,700 KB
testcase_11 AC 268 ms
111,876 KB
testcase_12 AC 274 ms
111,108 KB
testcase_13 AC 265 ms
111,876 KB
testcase_14 AC 821 ms
129,176 KB
testcase_15 AC 944 ms
129,304 KB
testcase_16 AC 896 ms
129,184 KB
testcase_17 AC 965 ms
129,440 KB
testcase_18 AC 899 ms
128,288 KB
testcase_19 AC 744 ms
105,748 KB
testcase_20 AC 667 ms
106,260 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#int(input())
#map(int, input().split())
#list(map(int, input().split()))

import sys
sys.setrecursionlimit(200000)

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 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

        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):
        return {r: self.members(r) for r in self.roots()}

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

N, M, Q = map(int, input().split())
A = [0] * M
for i in range(M):
    a, b = map(int, input().split())
    A[i] = (a-1, b-1)

C = [0] * Q
for i in range(Q):
    a, b = map(int, input().split())
    C[i] = (a-1, b-1)

u = UnionFind(N)

to = [[] for i in range(N)]
# print(C)
sc = set(C)
# print(sc)
for i in range(M):
    if A[i] not in sc:
        a, b = A[i]
        to[a].append(b)
        to[b].append(a)
        u.union(a, b)

ans = [0] * N
r = u.members(0)
for x in r:
    ans[x] = -1
v = [0] * N

for i in range(Q-1, -1, -1):
    # print(i, u.members(0), to, v)
    a, b = C[i]
    f = 0
    if u.same(0, a):
        f += 1
    if u.same(0, b):
        f += 2

    if f == 1 or f == 2:
        if f == 1:
            a, b = b, a
        v[a] = 1
        ans[a] = i+1
        q = [a]
        while q:
            t = q.pop()
            for x in to[t]:
                if v[x] == 0:
                    q.append(x)
                    v[x] = 1
                    ans[x] = i+1

    u.union(a, b)
    to[a].append(b)
    to[b].append(a)

for x in ans[1:]:
    print(x)
0