Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- sys.stdin = open("input.txt", "r")
- def find_all_paths(graph, start, end, path=[]):
- path = path + [start]
- if start == end:
- return [path]
- paths = []
- for node in graph[start]:
- if node not in path:
- newpaths = find_all_paths(graph, node, end, path)
- for newpath in newpaths:
- paths.append(newpath)
- return paths
- d = {}
- d2 = {}
- a = input()
- i = 1
- while i <= int(a):
- s = input()
- arr = []
- ind = 2
- d[s.split()[0]] = arr
- if len(s.split()) == 1:
- d[s.split()[0]] = [s]
- while ind < len(s.split()):
- arr.append(s.split()[ind])
- ind += 1
- i += 1
- for x in d:
- if x not in d2:
- for c in d:
- if x in d[c]:
- if (x != c):
- if x in d2:
- d2[x].append(c)
- else:
- d2[x] = [c]
- for z in d:
- if z not in d2:
- d2[z] = ''
- a = input()
- i = 1
- while i <= int(a):
- s = input()
- if s.split()[0] not in d2:
- print('No')
- i += 1
- continue
- if s.split()[0] == s.split()[1] and s.split()[0] in d2:
- print('Yes')
- i += 1
- continue
- if not find_all_paths(d2, s.split()[0], s.split()[1]):
- print('No')
- i += 1
- continue
- if find_all_paths(d2, s.split()[0], s.split()[1]) != []:
- print('Yes')
- i += 1
- continue
- i += 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement