Guest User

Untitled

a guest
May 21st, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.49 KB | None | 0 0
  1. import sys
  2. n = int(input())
  3. resultado = n
  4. grafo = [[] for _ in range(n)]
  5.  
  6. def decompor(i,pai):
  7. global resultado
  8. peso = 1
  9. for p in grafo[i-1]:
  10. if(p != pai):
  11. peso += decompor(p,i)
  12. diferenca = abs(n-2*peso)
  13. if(diferenca < resultado):
  14. resultado = diferenca
  15. return peso
  16.  
  17. for i in range(1,n):
  18. [A,B] = [int(c) for c in input().split(" ")]
  19. grafo[A-1].append(B)
  20. grafo[B-1].append(A)
  21. sys.setrecursionlimit(10**5)
  22. decompor(0,-2)
  23.  
  24. print(resultado,"\n")
Add Comment
Please, Sign In to add comment