結果
| 問題 |
No.1390 Get together
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-03-09 10:14:55 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 397 ms / 2,000 ms |
| コード長 | 689 bytes |
| コンパイル時間 | 187 ms |
| コンパイル使用メモリ | 82,408 KB |
| 実行使用メモリ | 102,556 KB |
| 最終ジャッジ日時 | 2024-10-11 01:02:14 |
| 合計ジャッジ時間 | 8,205 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 29 |
ソースコード
N,M=map(int,input().split())
l=[[] for i in range(N)]
for i in range(N):
b,c=map(int,input().split())
b-=1
c-=1
l[c].append(b)
root=[i for i in range(M)]
height=[0]*M
def find(a):
f=a
if a==root[a]:
return a
while a!=root[a]:
a=root[a]
root[f]=a
return a
def union(a,b):
A=find(a)
B=find(b)
if A==B:
return 0
if height[A]>height[B]:
root[B]=root[A]
return 1
else:
root[A]=root[B]
if height[A]==height[B]:
height[B]+=1
return 1
ans=0
for i in l:
if len(i)<2:
continue
S=i[0]
for j in range(1,len(i)):
ans+=union(i[j],S)
print(ans)