結果
| 問題 |
No.2403 "Eight" Bridges of Königsberg
|
| コンテスト | |
| ユーザー |
ntuda
|
| 提出日時 | 2023-08-05 12:56:07 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,210 bytes |
| コンパイル時間 | 215 ms |
| コンパイル使用メモリ | 82,304 KB |
| 実行使用メモリ | 98,584 KB |
| 最終ジャッジ日時 | 2024-10-15 10:02:16 |
| 合計ジャッジ時間 | 6,306 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 15 WA * 16 |
ソースコード
'''
YC2403
"Eight" Bridges of Königsberg
有向グラフに辺を追加して、各辺を1回ずつ通るようにする
最小追加変数は幾つか?
必要な条件
・連結であること
・出次数と入次数がすべて等しいか、差が+1の頂点と-1の頂点が1個ずつある
'''
def root(x):
if P[x] < 0: return x
P[x] = root(P[x]) # 経路圧縮
return P[x]
def unite(x,y):
x = root(x)
y = root(y)
if x == y: return
if x > y: x,y = y,x
P[x] += P[y]
P[y] = x
def same(x,y):
return root(x) == root(y)
def size(x):
x = root(x)
return -P[x]
def group_count():
S = set()
for i in range(N+1):
if size(i) > 1:
S.add(root(i))
if size(i) == 1 and G[i] == True:
S.add(i)
return len(S)
N,M = map(int,input().split())
P = [-1] * (N + 1)
UV = [list(map(int,input().split())) for _ in range(M)]
F = [[0,0] for _ in range(N+1)]
G = [False] * (N+1)
for u,v in UV:
F[u][0] += 1
F[v][1] += 1
G[u] = True
G[v] = True
unite(u,v)
tmp = group_count()
tmp2 = 0
for i in range(N+1):
tmp2 += abs(F[i][0] - F[i][1])
tmp2 //= 2
if tmp2 > 0:
tmp2 -= 1
print(tmp2 + tmp - 1)
ntuda