We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 214ab95 commit f1c2bd6Copy full SHA for f1c2bd6
1 file changed
live10/test105/문제2/백한결.py
@@ -0,0 +1,35 @@
1
+from collections import defaultdict
2
+
3
+def main():
4
+ n = int(input())
5
+ premise = defaultdict(list)
6
7
+ for _ in range(n):
8
+ a, b = input().strip().split(" is ")
9
+ premise[a].append(b)
10
11
+ m = int(input())
12
+ questions = [input().strip().split(" is ") for _ in range(m)]
13
14
+ for a, b in questions:
15
+ visited = set()
16
17
+ if dfs(premise, a, b, visited):
18
+ print("T")
19
+ else:
20
+ print("F")
21
22
+def dfs(graph, start, target, visited):
23
+ if start == target:
24
+ return True
25
+ visited.add(start)
26
27
+ for neighbor in graph[start]:
28
+ if neighbor not in visited:
29
+ if dfs(graph, neighbor, target, visited):
30
31
32
+ return False
33
34
+if __name__ == "__main__":
35
+ main()
0 commit comments