Skip to content

Instantly share code, notes, and snippets.

@zsrinivas
Created May 1, 2015 15:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zsrinivas/8ae83807578c8753f139 to your computer and use it in GitHub Desktop.
Save zsrinivas/8ae83807578c8753f139 to your computer and use it in GitHub Desktop.
hackerrank: All Domains ↠ Algorithms ↠ Graph Theory ↠ Even Tree
#!/usr/bin/python
# -*- encoding: utf-8 -*-
# pylint: disable=invalid-name,missing-docstring,bad-builtin,star-args
from collections import defaultdict
def numberOfBreaks(graph):
bks = [0]
visited = set()
def recur(i):
visited.add(i)
if len(graph[i]) == 1 and graph[i][0] in visited:
return 1
children = 0
for x in graph[i]:
if x not in visited:
y = recur(x)
if y % 2 == 0:
bks[0] += 1
else:
children += y
return children + 1
recur(graph.iterkeys().next())
return bks[0]
def main():
n, m = map(int, raw_input().split())
edges = defaultdict(list)
for _ in xrange(m):
p, q = map(int, raw_input().split())
edges[p].append(q)
edges[q].append(p)
print numberOfBreaks(edges)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment