結果
問題 |
No.3024 全単射的
|
ユーザー |
![]() |
提出日時 | 2025-02-17 03:19:30 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 279 ms / 5,000 ms |
コード長 | 1,065 bytes |
コンパイル時間 | 408 ms |
コンパイル使用メモリ | 81,936 KB |
実行使用メモリ | 173,544 KB |
最終ジャッジ日時 | 2025-02-17 03:19:34 |
合計ジャッジ時間 | 4,591 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 22 |
ソースコード
import sys input = sys.stdin.readline N,M=map(int,input().split()) XY=[list(map(int,input().split())) for i in range(N)] LIST=[] for x,y in XY: LIST.append(x) LIST.append(y) LIST=sorted(set(LIST)) D={LIST[i]:i for i in range(len(LIST))} for i in range(N): XY[i][0]=D[XY[i][0]] XY[i][1]=D[XY[i][1]] # UnionFind Group = [i for i in range(len(LIST)+1)] # グループ分け Nodes = [1]*(len(LIST)+1) # 各グループのノードの数 def find(x): while Group[x] != x: x=Group[x] return x def Union(x,y): if find(x) != find(y): if Nodes[find(x)] < Nodes[find(y)]: Nodes[find(y)] += Nodes[find(x)] Nodes[find(x)] = 0 Group[find(x)] = find(y) else: Nodes[find(x)] += Nodes[find(y)] Nodes[find(y)] = 0 Group[find(y)] = find(x) for x,y in XY: Union(x,y) A=[0]*len(LIST) for x,y in XY: A[find(x)]+=1 ANS=0 for i in range(len(LIST)): if find(i)==i: ANS+=min(A[i],Nodes[i]) print(ANS)