Skip to content

Instantly share code, notes, and snippets.

@pavlovdog
Created May 11, 2017 13:51
Show Gist options
  • Save pavlovdog/291c1e329a89285a7b4caf9d5d2d08dd to your computer and use it in GitHub Desktop.
Save pavlovdog/291c1e329a89285a7b4caf9d5d2d08dd to your computer and use it in GitHub Desktop.
null created by pavlovdog1 - https://repl.it/HsyV/0
from collections import defaultdict
number_nodes, number_edges = map(int, input().split())
# Store input data
graph = {}
for i in range(number_edges):
edge_from, edge_to = map(int, input().split())
graph[edge_from] = graph.get(edge_from, [])
graph[edge_from].append(edge_to)
# Check for semicompleteness
connected_components = defaultdict(set)
def dfs(node):
global connected_components, graph
if node not in connected_components:
# this is important, so neighbors won't try to traverse current node
connected_components[node] = set()
for next_ in graph[node]:
dfs(next_)
connected_components[node] = connected_components[next_]
# all that's left is add the current node
connected_components[node].add(node)
for node_ in graph:
dfs(node_)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment