fork download
  1. from collections import deque
  2. import sys
  3. read = sys.stdin.readline
  4.  
  5. T = int(read())
  6. for _ in range(T):
  7. N, R, M = map(int, read().split())
  8. graph = [[] for _ in range(N)]
  9. for _ in range(R):
  10. a, b = map(int, read().split())
  11. #przesuniecie bo indeksowanie jest od 1
  12. a-=1
  13. b-=1
  14. graph[a].append(b)
  15. graph[b].append(a)
  16.  
  17. soldiers = []
  18. for _ in range(M):
  19. K, S = map(int, read().split())
  20. soldiers.append((K-1, S))
  21.  
  22. def multi_source_bfs(sources,n,G):
  23. visited_by = [-1] * n
  24. q = deque()
  25. for index, (s, reach) in enumerate(sources):
  26. q.append((reach-1,s,index))
  27. visited_by[s] = index
  28. while q:
  29. r, v, id = q.popleft()
  30. #pomin jesli r < 0
  31. if r < 0:
  32. continue
  33. for u in G[v]:
  34. if visited_by[u] == -1:
  35. visited_by[u] = id
  36. q.append((r-1,u,id))
  37. elif visited_by[u] != id:
  38. return False
  39. return True
  40.  
  41. if multi_source_bfs(soldiers,N,graph):
  42. print("Yes")
  43. else:
  44. print("No")
  45.  
Success #stdin #stdout 0.11s 14088KB
stdin
1
3 2 3
1 2
2 3
1 0
2 0
3 0
stdout
Yes