Advertisement
Guest User

Untitled

a guest
Jan 29th, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Main{
  4. private static List<Set<Integer>> list;
  5. public Main(int n){
  6. list = new LinkedList<Set<Integer>>();
  7. for(int i = 0; i < n; i++){
  8. Set<Integer> set = new HashSet<Integer>();
  9. set.add(i);
  10. list.add(set);
  11. }
  12. }
  13. static boolean query(int s, int t){
  14. for(Set<Integer> set : list){
  15. if(set.contains(s) && set.contains(t)) return true;
  16. }
  17. return false;
  18. }
  19. static void union(int s, int t){
  20. Set<Integer> S=null, T=null;
  21. for(Set<Integer> set : list){
  22. if(set.contains(s)) S = set;
  23. if(set.contains(t)) T = set;
  24. }
  25. if(S != T){
  26. S.addAll(T);
  27. list.remove(T);
  28. }
  29. }
  30. static void move(int s, int t){
  31. int c, m;
  32. for(Set<Integer> set : list){
  33. if(set.contains(s)) c = s;
  34. }
  35. }
  36. public static void main(String[] args) {
  37. Scanner sc = new Scanner(System.in);
  38. int n = sc.nextInt();
  39. int m = sc.nextInt();
  40. sc.nextLine();
  41. Main main = new Main(n);
  42. for(int i = 0; i < m; i++){
  43. String[] parts = sc.nextLine().split(" ");
  44. String operation = parts[0];
  45. int s = Integer.parseInt(parts[1]);
  46. int t = Integer.parseInt(parts[2]);
  47. if(operation.equals("0")){
  48. if(!query(s,t)){
  49. System.out.println("0");
  50. } else {
  51. System.out.println("1");
  52. }
  53. }else if(operation.equals("1")){
  54. union(s,t);
  55. }
  56. }
  57. }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement