結果

問題 No.416 旅行会社
ユーザー mkawa2mkawa2
提出日時 2020-04-21 12:41:36
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,925 bytes
コンパイル時間 211 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 378,188 KB
最終ジャッジ日時 2024-04-16 21:08:40
合計ジャッジ時間 7,762 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 253 ms
130,944 KB
testcase_01 AC 49 ms
55,040 KB
testcase_02 AC 48 ms
54,912 KB
testcase_03 AC 48 ms
54,912 KB
testcase_04 AC 49 ms
54,784 KB
testcase_05 AC 50 ms
54,996 KB
testcase_06 AC 48 ms
55,424 KB
testcase_07 AC 65 ms
68,224 KB
testcase_08 AC 101 ms
77,520 KB
testcase_09 AC 182 ms
82,168 KB
testcase_10 AC 269 ms
125,184 KB
testcase_11 TLE -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

from itertools import permutations
import sys

sys.setrecursionlimit(10 ** 6)
from bisect import *
from collections import *
from heapq import *

def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def SI(): return sys.stdin.readline()[:-1]
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
int1 = lambda x: int(x) - 1
def MI1(): return map(int1, sys.stdin.readline().split())
def LI1(): return list(map(int1, sys.stdin.readline().split()))
p2D = lambda x: print(*x, sep="\n")
dij = [(1, 0), (0, 1), (-1, 0), (0, -1)]

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

    def root(self, u):
        v = self.state[u]
        if v < 0: return u
        self.state[u] = res = self.root(v)
        return res

    def merge(self, u, v):
        res=[]
        ru = self.root(u)
        rv = self.root(v)
        if ru == rv: return res
        if ru > rv: ru, rv = rv, ru
        if ru==0:res=self.member[rv]
        else:
            self.member[ru]+=self.member[rv]
            self.member[rv]=[]
        self.state[rv] = ru
        return res

def main():
    # 橋を壊していくのではなく、クエリを逆順にして、橋をつないでいく
    n,m,q=MI()
    ab=LLI1(m)
    cd=LLI1(q)
    uf=UnionFind(n)
    # 破壊される橋をチェック
    des=set()
    for c,d in cd:des.add((c,d))
    # 破壊されない橋を結ぶ
    ans=[0]*n
    for a,b in ab:
        if (a,b) in des:continue
        uu=uf.merge(a,b)
        for u in uu:ans[u]=-1
    # クエリを逆から見て橋をつないでいく
    for i,(c,d) in enumerate(cd[::-1]):
        uu=uf.merge(c,d)
        for u in uu:ans[u]=q-i
    for i in range(1,n):print(ans[i])

main()
0