-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path41.graph_node.py
More file actions
31 lines (30 loc) · 889 Bytes
/
41.graph_node.py
File metadata and controls
31 lines (30 loc) · 889 Bytes
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
import sys
sys.stdin = open('input.txt','r')
T = int(input())
def dfs(graph, start_node):
visit = list()
stack = list()
stack.append(start_node)
while stack :
node = stack.pop()
if node not in visit:
visit.append(node)
stack.extend(graph[node])
return visit
for testcase in range(1,T+1):
V, E = map(int,input().split())
graph = {}
for i in range(1,V+1):
graph[i] = [] #기본 세팅 쫙 안 깔아주면 키값 오류남
for i in range(E):
a,b = map(int, input().split())
graph[a]+=[b] #get으로도 할수는 있지만 키값이 빵꾸남
print(graph)
S, G = map(int,input().split())
final_destination = dfs(graph, S)
result = -1
if G in final_destination:
result = 1
else:
result = 0
print('#{}'.format(testcase),result)