結果

問題 No.1293 2種類の道路
ユーザー titiatitia
提出日時 2020-11-21 01:28:18
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 771 ms / 2,000 ms
コード長 1,693 bytes
コンパイル時間 222 ms
コンパイル使用メモリ 10,924 KB
実行使用メモリ 49,876 KB
最終ジャッジ日時 2023-09-30 20:25:43
合計ジャッジ時間 9,961 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,036 KB
testcase_01 AC 15 ms
7,976 KB
testcase_02 AC 16 ms
8,056 KB
testcase_03 AC 16 ms
8,236 KB
testcase_04 AC 16 ms
8,092 KB
testcase_05 AC 16 ms
8,048 KB
testcase_06 AC 16 ms
8,156 KB
testcase_07 AC 16 ms
8,136 KB
testcase_08 AC 16 ms
8,240 KB
testcase_09 AC 771 ms
41,984 KB
testcase_10 AC 758 ms
41,984 KB
testcase_11 AC 766 ms
41,972 KB
testcase_12 AC 766 ms
42,068 KB
testcase_13 AC 744 ms
42,024 KB
testcase_14 AC 493 ms
41,608 KB
testcase_15 AC 503 ms
41,592 KB
testcase_16 AC 626 ms
43,396 KB
testcase_17 AC 601 ms
41,968 KB
testcase_18 AC 475 ms
44,040 KB
testcase_19 AC 553 ms
49,872 KB
testcase_20 AC 545 ms
49,876 KB
testcase_21 AC 290 ms
8,408 KB
testcase_22 AC 289 ms
8,484 KB
testcase_23 AC 274 ms
8,496 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

N,D,W=map(int,input().split())

# UnionFind

Group0 = [i for i in range(N+1)] # グループ分け
Nodes0 = [1]*(N+1) # 各グループのノードの数

def find(x):
    while Group0[x] != x:
        x=Group0[x]
    return x

def Union(x,y):
    if find(x) != find(y):
        if Nodes0[find(x)] < Nodes0[find(y)]:
            
            Nodes0[find(y)] += Nodes0[find(x)]
            Nodes0[find(x)] = 0
            Group0[find(x)] = find(y)
            
        else:
            Nodes0[find(x)] += Nodes0[find(y)]
            Nodes0[find(y)] = 0
            Group0[find(y)] = find(x)

# UnionFind

Group1 = [i for i in range(N+1)] # グループ分け
Nodes1 = [1]*(N+1) # 各グループのノードの数

def find1(x):
    while Group1[x] != x:
        x=Group1[x]
    return x

def Union1(x,y):
    if find1(x) != find1(y):
        if Nodes1[find1(x)] < Nodes1[find1(y)]:
            
            Nodes1[find1(y)] += Nodes1[find1(x)]
            Nodes1[find1(x)] = 0
            Group1[find1(x)] = find1(y)
            
        else:
            Nodes1[find1(x)] += Nodes1[find1(y)]
            Nodes1[find1(y)] = 0
            Group1[find1(y)] = find1(x)

for i in range (D):
    x,y=map(int,input().split())
    Union(x,y)

for i in range(W):
    x,y=map(int,input().split())
    Union1(x,y)

G1_to_G0=[set() for i in range(N+1)]

for i in range(N+1):
    G1_to_G0[find1(i)].add(find(i))

NGroup=[0]*(N+1)

for i in range(N+1):
    if find1(i)==i:
        for g0 in G1_to_G0[i]:
            NGroup[g0]+=Nodes1[i]
            


ANS=0
for i in range(1,N+1):
    if Nodes0[i]==0:
        continue

    ANS+=Nodes0[i]*(NGroup[i]-1)

print(ANS)
0