AtCoder Regular Contest 097

Submission #3966503

Source codeソースコード

N, M = [int(_) for _ in input().split()]
P = [0] + [int(_) for _ in input().split()]
XY = [[int(_) for _ in input().split()] for _ in range(M)]

UF = list(range(N + 1))


def find(x):
    if UF[x] != x:
        UF[x] = find(UF[x])
    return UF[x]


def unite(x, y):
    UF[find(x)] = find(y)


def same(x, y):
    return find(x) == find(y)


for x, y in XY:
    unite(x, y)

ans = 0
for i in range(1, N + 1):
    if same(i, P[i]):
        ans += 1
print(ans)

Submission

Task問題 D - Equals
User nameユーザ名 yydoco
Created time投稿日時
Language言語 Python3 (3.4.3)
Status状態 AC
Score得点 400
Source lengthソースコード長 489 Byte
File nameファイル名
Exec time実行時間 643 ms
Memory usageメモリ使用量 31384 KB

Test case

Set

Set name Score得点 / Max score Cases
Sample - 0_000.txt,0_001.txt,0_002.txt,0_003.txt
All 400 / 400 0_000.txt,0_001.txt,0_002.txt,0_003.txt,1_004.txt,1_005.txt,1_006.txt,1_007.txt,1_008.txt,1_009.txt,1_010.txt,1_011.txt,1_012.txt,1_013.txt,1_014.txt,1_015.txt,1_016.txt,1_017.txt,1_018.txt,1_019.txt,1_020.txt,1_021.txt,1_022.txt

Test case

Case name Status状態 Exec time実行時間 Memory usageメモリ使用量
0_000.txt AC 18 ms 3064 KB
0_001.txt AC 18 ms 3064 KB
0_002.txt AC 18 ms 3064 KB
0_003.txt AC 18 ms 3064 KB
1_004.txt AC 409 ms 17428 KB
1_005.txt AC 513 ms 29340 KB
1_006.txt AC 574 ms 29404 KB
1_007.txt AC 18 ms 3064 KB
1_008.txt AC 18 ms 3064 KB
1_009.txt AC 18 ms 3064 KB
1_010.txt AC 18 ms 3064 KB
1_011.txt AC 18 ms 3064 KB
1_012.txt AC 18 ms 3064 KB
1_013.txt AC 22 ms 3064 KB
1_014.txt AC 37 ms 3572 KB
1_015.txt AC 19 ms 3064 KB
1_016.txt AC 19 ms 3064 KB
1_017.txt AC 23 ms 3188 KB
1_018.txt AC 364 ms 18080 KB
1_019.txt AC 106 ms 13880 KB
1_020.txt AC 105 ms 13812 KB
1_021.txt AC 109 ms 13812 KB
1_022.txt AC 643 ms 31384 KB