Advertisement
eg0rmaffin

inheritance_emulator

Dec 11th, 2020
555
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.49 KB | None | 0 0
  1. import sys
  2. sys.stdin = open("input.txt", "r")
  3.  
  4.  
  5. def find_all_paths(graph, start, end, path=[]):
  6.     path = path + [start]
  7.     if start == end:
  8.         return [path]
  9.     paths = []
  10.     for node in graph[start]:
  11.         if node not in path:
  12.             newpaths = find_all_paths(graph, node, end, path)
  13.             for newpath in newpaths:
  14.                 paths.append(newpath)
  15.     return paths
  16.  
  17.  
  18. d = {}
  19. d2 = {}
  20.  
  21. a = input()
  22. i = 1
  23.  
  24. while i <= int(a):
  25.     s = input()
  26.     arr = []
  27.     ind = 2
  28.     d[s.split()[0]] = arr
  29.     if len(s.split()) == 1:
  30.         d[s.split()[0]] = [s]
  31.     while ind < len(s.split()):
  32.         arr.append(s.split()[ind])
  33.         ind += 1
  34.     i += 1
  35.  
  36. for x in d:
  37.     if x not in d2:
  38.         for c in d:
  39.             if x in d[c]:
  40.                 if (x != c):
  41.                     if x in d2:
  42.                         d2[x].append(c)
  43.                     else:
  44.                         d2[x] = [c]
  45. for z in d:
  46.     if z not in d2:
  47.         d2[z] = ''
  48.  
  49.  
  50.  
  51.  
  52.  
  53. a = input()
  54. i = 1
  55. while i <= int(a):
  56.     s = input()
  57.     if s.split()[0] not in d2:
  58.         print('No')
  59.         i += 1
  60.         continue
  61.     if s.split()[0] == s.split()[1] and s.split()[0] in d2:
  62.         print('Yes')
  63.         i += 1
  64.         continue
  65.     if not find_all_paths(d2, s.split()[0], s.split()[1]):
  66.         print('No')
  67.         i += 1
  68.         continue
  69.     if find_all_paths(d2, s.split()[0], s.split()[1]) != []:
  70.         print('Yes')
  71.         i += 1
  72.         continue
  73.     i += 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement