-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph_path.py
38 lines (31 loc) · 855 Bytes
/
graph_path.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import sys
sys.stdin = open('inputs/graph_path_input.txt')
def solution(adj, s, f):
visited = [0] * (len(adj)+1)
stack = [s]
while stack:
s = stack[-1]
if s == f:
return 1
visited[s] = 1
flag = False
for w in adj[s]:
if not visited[w]:
stack.append(w)
flag = True
break
if not flag:
stack.pop()
return 0
def main():
test_cases = int(input())
for test_case in range(test_cases):
v, e = map(int, input().split())
adj = [[] for _ in range(v+1)]
for i in range(e):
s, f = map(int, input().split())
adj[s].append(f)
s, f = map(int, input().split())
print(f'#{test_case+1} {solution(adj, s, f)}')
if __name__ == '__main__':
main()