結果
問題 | No.2403 "Eight" Bridges of Königsberg |
ユーザー |
![]() |
提出日時 | 2023-08-04 22:19:26 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
AC
|
実行時間 | 711 ms / 2,000 ms |
コード長 | 776 bytes |
コンパイル時間 | 197 ms |
コンパイル使用メモリ | 12,800 KB |
実行使用メモリ | 26,496 KB |
最終ジャッジ日時 | 2024-11-26 17:48:32 |
合計ジャッジ時間 | 8,648 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 31 |
ソースコード
class UF:def __init__(self,n):self.p = [-1]*ndef f(self,x):if self.p[x]<0: return xelse: self.p[x] = self.f(self.p[x]); return self.p[x]def u(self,x,y):x = self.f(x); y = self.f(y)if x==y: returnif -self.p[x]<-self.p[y]: x,y = y,xself.p[x] += self.p[y]; self.p[y] = xn,m = map(int,input().split())a = [0]*n; b = [0]*n; c = [[] for _ in range(n)]; uf = UF(n)for _ in range(m):u,v = map(int,input().split())a[u-1] += 1; b[v-1] += 1; uf.u(u-1,v-1)ans = 0for i,(u,v) in enumerate(zip(a,b)):if u>v or u==v>0: c[uf.f(i)].append(u-v)p = q = ans = 0for i in range(n):if c[i]:p += 1; ans += sum(c[i])if sum(c[i])==0: q += 1print(q-1 if p==q else max(ans-1,0)+q)