結果
| 問題 |
No.2403 "Eight" Bridges of Königsberg
|
| コンテスト | |
| ユーザー |
titia
|
| 提出日時 | 2023-08-05 00:00:25 |
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
| 結果 |
AC
|
| 実行時間 | 515 ms / 2,000 ms |
| コード長 | 1,129 bytes |
| コンパイル時間 | 262 ms |
| コンパイル使用メモリ | 12,672 KB |
| 実行使用メモリ | 48,640 KB |
| 最終ジャッジ日時 | 2024-11-26 18:06:35 |
| 合計ジャッジ時間 | 7,064 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 31 |
ソースコード
import sys
input = sys.stdin.readline
N,M=map(int,input().split())
E=[list(map(int,input().split())) for i in range(M)]
IN=[0]*(N+1)
OUT=[0]*(N+1)
# UnionFind
Group = [i for i in range(N+1)] # グループ分け
Nodes = [1]*(N+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 a,b in E:
Union(a,b)
LEN=0
for i in range(1,N+1):
if find(i)==i:
LEN+=1
for a,b in E:
IN[a]+=1
OUT[b]+=1
INP=[0]*(N+1)
for i in range(1,N+1):
if IN[i]>OUT[i]:
INP[find(i)]+=IN[i]-OUT[i]
LIST=[]
for i in range(1,N+1):
if find(i)==i and (IN[i]>0 or OUT[i]>0):
LIST.append(INP[i])
LIST.sort(reverse=True)
ANS=0
for l in LIST:
ANS+=max(l-1,0)
ANS+=len(LIST)-1
print(ANS)
titia