Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Main{
- private static List<Set<Integer>> list;
- public Main(int n){
- list = new LinkedList<Set<Integer>>();
- for(int i = 0; i < n; i++){
- Set<Integer> set = new HashSet<Integer>();
- set.add(i);
- list.add(set);
- }
- }
- static boolean query(int s, int t){
- for(Set<Integer> set : list){
- if(set.contains(s) && set.contains(t)) return true;
- }
- return false;
- }
- static void union(int s, int t){
- Set<Integer> S=null, T=null;
- for(Set<Integer> set : list){
- if(set.contains(s)) S = set;
- if(set.contains(t)) T = set;
- }
- if(S != T){
- S.addAll(T);
- list.remove(T);
- }
- }
- static void move(int s, int t){
- for(Set<Integer> set : list){
- if(set.contains(s)&&set.contains(t)) break;
- set.remove(s);
- if(set.contains(t)) set.add(s);
- }
- }
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int n = sc.nextInt();
- int m = sc.nextInt();
- sc.nextLine();
- Main main = new Main(n);
- for(int i = 0; i < m; i++){
- String[] parts = sc.nextLine().split(" ");
- String operation = parts[0];
- int s = Integer.parseInt(parts[1]);
- int t = Integer.parseInt(parts[2]);
- if(operation.equals("0")){
- if(!query(s,t)){
- System.out.println("0");
- } else {
- System.out.println("1");
- }
- }else if(operation.equals("1")){
- union(s,t);
- }else if(operation.equals("2")){
- move(s, t);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement