結果

問題 No.416 旅行会社
ユーザー maspymaspy
提出日時 2020-03-22 12:22:59
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 982 ms / 4,000 ms
コード長 1,502 bytes
コンパイル時間 644 ms
コンパイル使用メモリ 10,788 KB
実行使用メモリ 107,352 KB
最終ジャッジ日時 2023-08-21 10:30:35
合計ジャッジ時間 11,578 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 444 ms
62,232 KB
testcase_01 AC 17 ms
7,980 KB
testcase_02 AC 16 ms
7,996 KB
testcase_03 AC 17 ms
7,992 KB
testcase_04 AC 16 ms
8,084 KB
testcase_05 AC 17 ms
8,024 KB
testcase_06 AC 17 ms
8,016 KB
testcase_07 AC 18 ms
8,672 KB
testcase_08 AC 25 ms
9,620 KB
testcase_09 AC 63 ms
14,168 KB
testcase_10 AC 450 ms
62,264 KB
testcase_11 AC 435 ms
59,000 KB
testcase_12 AC 456 ms
65,272 KB
testcase_13 AC 440 ms
58,956 KB
testcase_14 AC 981 ms
107,144 KB
testcase_15 AC 958 ms
107,124 KB
testcase_16 AC 962 ms
107,332 KB
testcase_17 AC 982 ms
107,092 KB
testcase_18 AC 960 ms
107,352 KB
testcase_19 AC 748 ms
73,092 KB
testcase_20 AC 756 ms
73,164 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/ python3.8
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


class UnionFind:
    def __init__(self, N):
        self.root = list(range(N))
        self.size = [1] * (N)
        self.component = [[i] for i in range(N)]

    def find_root(self, x):
        root = self.root
        while root[x] != x:
            root[x] = root[root[x]]
            x = root[x]
        return x

    def merge(self, x, y):
        x = self.find_root(x)
        y = self.find_root(y)
        if x == y:
            return False
        sx, sy = self.size[x], self.size[y]
        if sx < sy:
            self.root[x] = y
            self.size[y] += sx
            self.component[y] += self.component[x]
        else:
            self.root[y] = x
            self.size[x] += sy
            self.component[x] += self.component[y]
        return True


N, M, Q = map(int, readline().split())
m = map(int, read().split())
ABCD = tuple(zip(m, m))
AB = ABCD[:M]
CD = ABCD[M:]

init_edge = set(AB) - set(CD)
uf = UnionFind(N + 1)
find = uf.find_root
answer = [0] * (N + 1)


def merge(t, u, v):
    u = find(u)
    v = find(v)
    r = find(1)
    if u == v:
        return
    if r == v:
        u, v = v, u
    if r == u:
        for i in uf.component[v]:
            answer[i] = t
    uf.merge(u, v)


for a, b in init_edge:
    merge(-1, a, b)
for i, (a, b) in enumerate(CD[::-1]):
    merge(Q - i, a, b)

print('\n'.join(map(str, answer[2:])))
0