Skip to content

Instantly share code, notes, and snippets.

@Abdujabbar
Created May 7, 2016 23:25
Show Gist options
  • Save Abdujabbar/b5ffd07d413e340867c2bca9fa0b7df0 to your computer and use it in GitHub Desktop.
Save Abdujabbar/b5ffd07d413e340867c2bca9fa0b7df0 to your computer and use it in GitHub Desktop.
STEPIC
import json
import operator
def dfs(graph, start):
visited, stack = set(), [start]
while stack:
vertex = stack.pop()
if vertex not in visited:
visited.add(vertex)
stack.extend(graph[vertex] - visited)
return visited
_input = input()
j = json.loads(_input)
_dict = dict()
for i in j:
_dict[i['name']] = set()
for i in j:
for item in i['parents']:
_dict[item].add(i['name'])
__dict = sorted(_dict, key=operator.itemgetter(0))
for v in __dict:
print(v + " : " + str(len(dfs(_dict, v))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment