結果

問題 No.2403 "Eight" Bridges of Königsberg
ユーザー とりゐ
提出日時 2023-08-04 21:42:21
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 699 bytes
コンパイル時間 249 ms
コンパイル使用メモリ 81,884 KB
実行使用メモリ 108,592 KB
最終ジャッジ日時 2024-10-14 19:40:12
合計ジャッジ時間 4,984 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 12 WA * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

from sys import stdin
input=lambda :stdin.readline()[:-1]

n,m=map(int,input().split())
edge=[[] for i in range(n)]
IN=[0]*n
OUT=[0]*n
for i in range(m):
  a,b=map(lambda x:int(x)-1,input().split())
  IN[a]+=1
  OUT[b]+=1
  edge[a].append(b)
  edge[b].append(a)

G=[]
seen=[0]*n
for i in range(n):
  if seen[i]:
    continue
  todo=[i]
  seen[i]=1
  res=0
  flag=False
  while todo:
    v=todo.pop()
    if IN[v]>OUT[v]:
      flag=True
      res+=IN[v]-OUT[v]
    for u in edge[v]:
      if seen[u]==0:
        seen[u]=1
        todo.append(u)  
  G.append([res,flag])

  
ans=0
cnt1=0
cnt2=0
for x,y in G:
  ans+=x
  if y:
    ans-=1
  else:
    cnt2+=1

if cnt1:
  ans-=1
print(ans+max(0,cnt2-1))
0