Skip to content

Commit f1c2bd6

Browse files
author
hangyeol
committed
105차 2번 문제 풀이
1 parent 214ab95 commit f1c2bd6

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
return True
31+
32+
return False
33+
34+
if __name__ == "__main__":
35+
main()

0 commit comments

Comments
 (0)