結果

問題 No.2403 "Eight" Bridges of Königsberg
ユーザー 👑 timitimi
提出日時 2024-02-28 13:27:42
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 321 ms / 2,000 ms
コード長 898 bytes
コンパイル時間 3,120 ms
コンパイル使用メモリ 81,444 KB
実行使用メモリ 122,464 KB
最終ジャッジ日時 2024-02-28 13:27:53
合計ジャッジ時間 8,632 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
53,460 KB
testcase_01 AC 36 ms
53,460 KB
testcase_02 AC 36 ms
53,460 KB
testcase_03 AC 36 ms
53,460 KB
testcase_04 AC 183 ms
81,172 KB
testcase_05 AC 281 ms
85,624 KB
testcase_06 AC 301 ms
87,276 KB
testcase_07 AC 219 ms
82,984 KB
testcase_08 AC 242 ms
84,188 KB
testcase_09 AC 310 ms
88,572 KB
testcase_10 AC 270 ms
87,564 KB
testcase_11 AC 256 ms
84,344 KB
testcase_12 AC 321 ms
88,792 KB
testcase_13 AC 250 ms
83,972 KB
testcase_14 AC 36 ms
53,460 KB
testcase_15 AC 36 ms
53,460 KB
testcase_16 AC 36 ms
53,460 KB
testcase_17 AC 36 ms
53,460 KB
testcase_18 AC 36 ms
53,460 KB
testcase_19 AC 191 ms
122,464 KB
testcase_20 AC 55 ms
71,440 KB
testcase_21 AC 69 ms
87,884 KB
testcase_22 AC 110 ms
121,528 KB
testcase_23 AC 105 ms
118,332 KB
testcase_24 AC 107 ms
112,276 KB
testcase_25 AC 73 ms
94,780 KB
testcase_26 AC 44 ms
61,152 KB
testcase_27 AC 134 ms
108,896 KB
testcase_28 AC 129 ms
102,056 KB
testcase_29 AC 100 ms
81,212 KB
testcase_30 AC 98 ms
96,104 KB
testcase_31 AC 136 ms
109,012 KB
testcase_32 AC 98 ms
76,696 KB
testcase_33 AC 76 ms
82,420 KB
testcase_34 AC 97 ms
79,212 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N,M=map(int,input().split())
par=[i for i in range(N)]
E=[0]*N;F=[0]*N
rank=[0]*(N)
friend=[0]*N
block=[0]*N
size=[1]*N
def find(x):
  if par[x]==x:
    return x
  else:
    par[x]=find(par[x])
    return par[x]
 
#同じ集合か判定
def same(x,y):
  return find(x)==find(y)
 
def union(x,y):
  x=find(x)
  y=find(y)
  if x==y:
    return 
  if rank[x]>rank[y]:
    par[y]=x
    size[x]+=size[y]
  else:
    par[x]=y
    size[y]+=size[x]
    if rank[x]==rank[y]:
      rank[y]+=1
 
A=[]
for i in range(M):
  a,b=map(int,input().split())
  a-=1; b-=1
  E[a]+=1;F[b]+=1
  A.append((a,b))
  union(a,b)

D={}
for i in range(N):
  d=find(i)
  if d not in D:
    D[d]=[]
  D[d].append(i)
ans=0
for d in D:
  p,q=0,0
  for i in D[d]:
    if E[i]>F[i]:
      p+=E[i]-F[i]
    else:
      q+=F[i]-E[i]
  if p>1:
    ans+=p-1
G={} 
for a,b in A:
  d=find(a)
  if d not in G:
    G[d]=1
print(ans+len(G)-1)
0