Advertisement
DeepRest

H. Johny and Contribution

Jun 16th, 2021
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. #python 3.7.1
  2. from collections import OrderedDict
  3. m, n = map(int, input().split())
  4. references = [list(map(int, input().split())) for _ in range(n)]
  5. link={}
  6. for a, b in references:
  7. if a not in link.keys():
  8. link[a] = set()
  9. if b not in link.keys():
  10. link[b] = set()
  11. link[a].add(b)
  12. link[b].add(a)
  13.  
  14. blogs = list(map(int, input().split()))
  15. topicBlog = {}
  16. stack=OrderedDict()
  17. for blog, topic in enumerate(blogs):
  18. blog+=1
  19. if topic not in topicBlog.keys():
  20. topicBlog[topic] = []
  21. topicBlog[topic].append(blog)
  22. topicBlog=sorted(topicBlog.items(), key=lambda x:x[0])
  23.  
  24. flag=True
  25. for topic, blogs in topicBlog:
  26. for blog in blogs:
  27. desired=list(range(1,topic))
  28. if blog not in link.keys():
  29. link[blog]=set()
  30. for neighbor in link[blog]:
  31. if neighbor in stack.keys():
  32. if stack[neighbor]==topic:
  33. print(-1)
  34. flag=False
  35. break
  36. if stack[neighbor] in desired:
  37. desired.remove(stack[neighbor])
  38. if flag==False:
  39. break
  40. if len(desired)!=0:
  41. print(-1)
  42. flag=False
  43. break
  44. stack[blog] = topic
  45. if flag==False:
  46. break
  47. if flag==True:
  48. print(' '.join([str(i) for i in stack]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement