結果

問題 No.416 旅行会社
ユーザー kawacchukawacchu
提出日時 2019-07-19 21:52:04
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 2,998 bytes
コンパイル時間 99 ms
コンパイル使用メモリ 11,256 KB
実行使用メモリ 87,064 KB
最終ジャッジ日時 2023-08-26 22:04:51
合計ジャッジ時間 12,442 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

class UnionFind:
    def __init__(self, num):
        self.rank = [0] * num
        self.par = [i for i in range(num)]
        self.n = num

    def find_root(self, node):
        if self.par[node] == node:
            return node
        else:
            self.par[node] = self.find_root(self.par[node])
            return self.par[node]

    def same_root(self, x, y):
        return self.find_root(x) == self.find_root(y)

    def union(self, x, y):
        x = self.find_root(x)
        y = self.find_root(y)
        if x == y:
            return
        if self.rank[x] > self.rank[y]:
            self.par[y] = x
        else:
            self.par[x] = y
            if self.rank[x] == self.rank[y]:
                self.rank[y] += 1


def main():
    N, M, Q = map(int,input().split())
    
    S1 = set()
    for i in range(M) :
        a, b = map(int,input().split())
        a -= 1
        b -= 1
        
        key = str(a) + " " + str(b)
        S1.add(key)
    
    S2 = set()
    A = []
    for i in range(Q) :
        c, d = map(int,input().split())
        c -= 1
        d -= 1
        
        key = str(c) + " " + str(d)
        S2.add(key)
        A.append([c,d])
    
    S = S1.difference(S2)
    
    UF = UnionFind(N)
    
    L = [set([i]) for i in range(N)]
    
    for s in S :
        u, v = s.split()
        u = int(u)
        v = int(v)
        
        u_root = UF.find_root(u)
        v_root = UF.find_root(v)
        
        if u_root != v_root :
            
            if len(L[u_root]) < len(L[v_root]) :
                L[u_root], L[v_root] = L[v_root], L[u_root]
            
            L[u_root] = L[u_root].union(L[v_root])
            
            UF.union(u_root, v_root)
            
            if UF.find_root(u_root) == v_root :
                L[u_root], L[v_root] = L[v_root], L[u_root]
        
    
    ans = [0]*N
    
    start_root = UF.find_root(0)
    for j in L[start_root] :
        ans[j] = Q+1
    
    
    for i in range(Q-1,-1,-1) :
        u,v = A[i]
        
        u_root = UF.find_root(u)
        v_root = UF.find_root(v)
        start_root = UF.find_root(0)
        
        if start_root != u_root and start_root == v_root :
            for j in L[u_root] :
                ans[j] = max(i+1, ans[j])
        
        if start_root != v_root and start_root == u_root :
            for j in L[v_root] :
                ans[j] = max(i+1, ans[j])
        
        if u_root != v_root :
            
            if len(L[u_root]) < len(L[v_root]) :
                L[u_root], L[v_root] = L[v_root], L[u_root]
            
            L[u_root] = L[u_root].union(L[v_root])
            
            UF.union(u_root, v_root)
            
            if UF.find_root(u_root) == v_root :
                L[u_root], L[v_root] = L[v_root], L[u_root]
                
    for res in ans[1:] :
        print(res if res != Q+1 else -1)
    
            
    
import sys
input = sys.stdin.readline

if __name__ == "__main__":
    main()
0