Advertisement
Guest User

Untitled

a guest
Jan 29th, 2020
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 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. for(Set<Integer> set : list){
  32. if(set.contains(s)&&set.contains(t)) break;
  33. set.remove(s);
  34. if(set.contains(t)) set.add(s);
  35. }
  36. }
  37. public static void main(String[] args) {
  38. Scanner sc = new Scanner(System.in);
  39. int n = sc.nextInt();
  40. int m = sc.nextInt();
  41. sc.nextLine();
  42. Main main = new Main(n);
  43. for(int i = 0; i < m; i++){
  44. String[] parts = sc.nextLine().split(" ");
  45. String operation = parts[0];
  46. int s = Integer.parseInt(parts[1]);
  47. int t = Integer.parseInt(parts[2]);
  48. if(operation.equals("0")){
  49. if(!query(s,t)){
  50. System.out.println("0");
  51. } else {
  52. System.out.println("1");
  53. }
  54. }else if(operation.equals("1")){
  55. union(s,t);
  56. }else if(operation.equals("2")){
  57. move(s, t);
  58. }
  59. }
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement