結果
| 問題 |
No.2403 "Eight" Bridges of Königsberg
|
| コンテスト | |
| ユーザー |
qwewe
|
| 提出日時 | 2025-05-14 12:49:45 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,939 bytes |
| コンパイル時間 | 410 ms |
| コンパイル使用メモリ | 82,464 KB |
| 実行使用メモリ | 111,260 KB |
| 最終ジャッジ日時 | 2025-05-14 12:51:15 |
| 合計ジャッジ時間 | 4,417 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 21 WA * 10 |
ソースコード
import sys
def main():
N, M = map(int, sys.stdin.readline().split())
in_degree = [0] * (N + 1)
out_degree = [0] * (N + 1)
for _ in range(M):
u, v = map(int, sys.stdin.readline().split())
out_degree[u] += 1
in_degree[v] += 1
delta_initial = [0] * (N + 1)
sum_initial = 0
for u in range(1, N + 1):
delta_initial[u] = out_degree[u] - in_degree[u]
sum_initial += abs(delta_initial[u])
# Compute K1 for case 1
K1 = sum(max(0, delta_initial[u]) for u in range(1, N + 1))
# Compute A and B for all nodes
A = []
B = []
for u in range(1, N + 1):
a_val = abs(1 - delta_initial[u]) - abs(delta_initial[u])
A.append((a_val, u))
b_val = abs(-1 - delta_initial[u]) - abs(delta_initial[u])
B.append((b_val, u))
# Find top two smallest A and B
A.sort()
B.sort()
# Extract top two A and B with their indices
a_candidates = A[:2]
if len(A) < 2:
a_candidates += [(float('inf'), -1)] * (2 - len(A))
b_candidates = B[:2]
if len(B) < 2:
b_candidates += [(float('inf'), -1)] * (2 - len(B))
min_sum = float('inf')
# Check all possible pairs
for a_val, a_idx in a_candidates:
for b_val, b_idx in b_candidates:
if a_idx == -1 or b_idx == -1:
continue
if a_idx != b_idx:
current_sum = a_val + b_val
if current_sum < min_sum:
min_sum = current_sum
# If no valid pairs found, case 2 is impossible
if min_sum == float('inf'):
K2 = float('inf')
else:
sum_total = sum_initial + min_sum
if sum_total < 0:
K2 = float('inf')
else:
K2 = sum_total // 2
# Determine the answer
ans = min(K1, K2)
if ans == float('inf'):
print(-1)
else:
print(ans)
if __name__ == "__main__":
main()
qwewe