Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MOD = 10 ** 9 + 7
- def get_adjacency_table(edges):
- adjacency_table = {}
- for v, u in edges:
- if v not in adjacency_table:
- adjacency_table[v] = {}
- adjacency_table[v][u] = adjacency_table[v].get(u, 0) + 1
- return adjacency_table
- def traverse(n, adjacency_table, start_vertex, end_vertex):
- planned = {start_vertex}
- visited = set()
- while planned:
- v = planned.pop()
- visited.add(v)
- if v in adjacency_table:
- planned |= set(adjacency_table[v].keys()) - visited
- for v in adjacency_table:
- if v not in visited:
- adjacency_table[v] = {}
- else:
- adjacency_table[v] = {u: c for u, c in adjacency_table[v].items() if u in visited}
- if end_vertex not in visited:
- return 0
- planned = {start_vertex}
- visited = set()
- counters = [0] * (n + 1)
- counters[start_vertex] = 1
- incoming = {}
- for v in adjacency_table:
- for u in adjacency_table[v]:
- if u not in incoming:
- incoming[u] = set()
- incoming[u].add(v)
- while planned:
- for v in planned:
- if v not in incoming or not incoming[v]:
- break
- planned.remove(v)
- if v not in visited:
- visited.add(v)
- if v in adjacency_table:
- for u in adjacency_table[v]:
- counters[u] = (counters[u] + counters[v] * adjacency_table[v][u]) % MOD
- if u not in visited and u not in planned:
- planned.add(u)
- incoming[u].remove(v)
- return counters[end_vertex]
- n, m = [int(x) for x in input().split()]
- edges = [[int(x) for x in input().split()] for _ in range(m)]
- start, end = [int(x) for x in input().split()]
- table = get_adjacency_table(edges)
- print(traverse(n, table, start, end))
Add Comment
Please, Sign In to add comment