結果

問題 No.1390 Get together
ユーザー 小野寺健
提出日時 2021-05-24 22:40:15
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
WA  
実行時間 -
コード長 773 bytes
コンパイル時間 173 ms
コンパイル使用メモリ 12,544 KB
実行使用メモリ 46,324 KB
最終ジャッジ日時 2024-10-13 06:55:42
合計ジャッジ時間 18,635 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 7 WA * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

N, M = map(int, input().split())

par = [0] * N
rank = [0] * N

def init(n):
    for i in range(n):
        par[i] = i
        rank[i] = 0
        
def find(x):
    if par[x] == x:
        return x
    else:
        par[x] = find(par[x])
        return par[x]
    
def unite(x, y):
    x = find(x)
    y = find(y)
    if x == y:
        return
    if rank[x] < rank[y]:
        par[x] = y
    else:
        par[y] = x
        if rank[x] == rank[y]:
            rank[x] += 1

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

init(N)
D = dict()
E = dict()

for _ in range(N):
    b, c = map(int, input().split())
    E[c-1] = 0
    if (b-1) not in D:
        D[b-1] = c-1
    else:
        unite(D[b-1], c-1)
        
F = [i for i in par if i in E]

print(len(D) - len(set(F)))
0