結果

問題 No.416 旅行会社
ユーザー sjiki
提出日時 2022-11-03 23:55:28
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 2,118 bytes
コンパイル時間 169 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 66,816 KB
最終ジャッジ日時 2024-07-18 05:43:25
合計ジャッジ時間 3,455 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other RE * 21
権限があれば一括ダウンロードができます

ソースコード

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