結果

問題 No.416 旅行会社
ユーザー sjikisjiki
提出日時 2022-11-03 23:55:28
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 2,118 bytes
コンパイル時間 268 ms
コンパイル使用メモリ 87,088 KB
実行使用メモリ 80,980 KB
最終ジャッジ日時 2023-09-25 06:51:24
合計ジャッジ時間 6,566 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#yukicoder 旅行会社

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] *(n)
        #parents 各要素の親番号を格納するリストを返す
        
    #要素xが属するグループを返す
    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]
    #要素xとyが属するグループを併合する
    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
 
    #要素xが属するグループのサイズ
    def size(self, x):
        return -self.parents[self.find(x)]
 
    #要素xとyが同じグループに属するかどうか
    def same(self, x, y):
        return self.find(x) == self.find(y)
 
    #要素xが属するグループを返す
    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()}

from sys import stdin
input = stdin.readline

 
def main():

    N,M,Q = map(int,input().split())
    uf = UnionFind(N)
    m = map(int, input().split())
    ABCD = tuple(zip(m, m))
    AB = ABCD[:M]
    CD = ABCD[M:]
    init_set = set(AB)-set(CD)
    for a,b in init_set:
        uf.union(a,b)

    for i in range(len(CD[::-1])):
        c, d = CD[::-1][i]
        if uf.same(c,0) and uf.same(d,0):
            continue     
        elif uf.same(d,0) and not uf.same(c,0):
            res[c-1] = M -i
        elif uf.same(c,0) and not uf.same(d,0):
            res[d-1] = M-i
    
        uf.union(c,d)
    
    for i in res:
        print(i)

main()
0