Created
April 4, 2017 16:34
-
-
Save youqad/21e51fa695f7649da3e99459c2d6aea6 to your computer and use it in GitHub Desktop.
Concours Meilleur Dev : problème final
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from collections import deque | |
lines = [] | |
for line in sys.stdin: | |
lines.append(line.rstrip('\n')) | |
N = int(lines[0]) | |
G = [set() for _ in range(N)] | |
for l in lines[1:]: | |
u, v = map(int, l.split()) | |
local_print(str(u) + ' ' + str(v)) | |
if not (u==N or v==N): | |
G[u].add(v) | |
G[v].add(u) | |
weight = [1 for _ in range(N)] | |
Q = deque([u for u in range(N) if len(G[u])==1]) | |
min_loss = N-1 # min loss at worst | |
already_seen = set() | |
while Q: | |
u = Q.popleft() | |
min_loss = min(min_loss, max(max((weight[v] for v in already_seen & G[u]), default=0), N-weight[u])) | |
already_seen.add(u) | |
for v in G[u]-already_seen: | |
weight[v]+=weight[u] | |
if len(G[v]-already_seen) <= 1: | |
Q.append(v) | |
print(min_loss) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment