fork download
  1. import sys
  2.  
  3. input = sys.stdin.readline
  4.  
  5. n, m = map(int, input().split())
  6. indicator = { i : i for i in range(n+1) } # 노드 연결 상태
  7. rank = { i : 0 for i in range(n+1) } # 노드가 위치한 단계
  8.  
  9. def find(x):
  10. if indicator[x] == x:
  11. #print(indicator, x)
  12. return x
  13. else:
  14. return find(indicator[x]) # 노드가 가리키는 노드로 다시 find
  15.  
  16.  
  17. for _ in range(m):
  18. command = list(map(int, input().split()))
  19. #print(indicator)
  20. fst = find(command[1])
  21. tnd = find(command[2])
  22. if command[0] == 1:
  23. if fst == tnd:
  24. print("YES")
  25. else:
  26. print("NO")
  27.  
  28. else:
  29. if fst == tnd:
  30. pass
  31. else:
  32. if rank[command[1]] > rank[command[2]]: # 랭크 더 높은 쪽으로 노드 연결
  33. indicator[command[2]] = command[1]
  34. else:
  35. indicator[command[1]] = command[2] # 그 반대
  36. if rank[command[1]] == rank[command[2]]: # 그 중에서 랭크 같다면 2번 노드에 연결하고 랭크 += 1
  37. rank[command[2]] += 1
  38. #print(indicator)
Success #stdin #stdout 0.03s 9744KB
stdin
3 3
0 1 2
0 1 3
1 2 3
stdout
NO