Problem:
Disjoint Set

Write a program which manipulates a disjoint set S = {S1, S2, . . . , Sk}.

First of all, the program should read an integer n, then make a disjoint set where each element consists of 0, 1, ... n−1 respectively.

Next, the program should read an integer q and manipulate the set for q queries. There are two kinds of queries for different operations:

unite(x, y): unites sets that contain x and y, say Sx and Sy, into a new set.

same(x, y): determine whether x and y are in the same set.

Input

n q
com1 x1 y1
com2 x2 y2
...
comq xq yq

In the first line, n and q are given. Then, q queries are given where com represents the type of queries. '0' denotes unite and '1' denotes same operation.

Output

For each same operation, print 1 if x and y are in the same set, otherwise 0, in a line.

Constraints

1 ≤ n ≤ 10000

1 ≤ q ≤ 100000

x ≠ y

Sample Input

5 12
0 1 4
0 2 3
1 1 2
1 3 4
1 1 4
1 3 2
0 1 3
1 2 4
1 3 0
0 0 4
1 0 2
1 3 0

Sample Output

0
0
1
1
1
0
1
1

Solution in C (judge verdict: Memory Limit Exceeded)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N 10001

struct DSL1A{
	
	short values[N];
	short len;
	
};
typedef struct DSL1A DSL1A;

DSL1A *array;
int n;

DSL1A newData(int num);
DSL1A cat(DSL1A a, DSL1A b);

int isFound(DSL1A data, int n);
int getIdx(int num);

void uniteData(int idx1, int idx2);

void display(DSL1A data);

int main(){
	
	int q;
	int i, k;
	int com, x, y;
	int output;
	int idx1, idx2;
	char temp[100];
	
	scanf("%d %d", &n, &q);
	
	array = (DSL1A*)malloc((sizeof(DSL1A)*n)+1);
	
	for(i=0; i<n; i++){
		
		array[i] = newData(i);
		
		//display(array[i]);
		
	}
	
	
	for(i=0; i<q; i++){
		
		scanf("%d %d %d", &com, &x, &y);
		
		//do unite
		if(com == 0){
			
			idx1 = getIdx(x);
			idx2 = getIdx(y);
			
			//printf("uniteData(%d, %d)\n", idx1, idx2);
			
			uniteData(idx1, idx2);
			
			//printf("united::");
			//display(array[idx1]);
			//puts("");
			
		}
		
		//do same
		else{
			
			output = 0;
			
			for(k=0; k<n; k++){
				
				if(isFound(array[k], x)){
					
					if(isFound(array[k], y)){
						
						output = 1;
						
					}
					
				}
				
			}
			
			printf("%d\n", output);
			
		}
		
	}
	
	free(array);
	
	return 0;
	
}



DSL1A newData(int num){
	
	DSL1A ret = {{num}, 1};
	
	//snprintf(ret.values, sizeof(ret.values), "%d", num);
	
	
	return ret;
	
}



int isFound(DSL1A data, int num){
	
	int i;
	int ret = 0;
	
	for(i=0; i<data.len; i++){
		
		if(data.values[i] == num){
			
			ret = 1;
			break;
			
		}
		
	}
	
	return ret;
	
}



int getIdx(int num){
	
	int i;
	
	for(i=0; i<n; i++){
		
		if(isFound(array[i], num)){
			
			return i;
			
		}
		
	}
	
	
}


void uniteData(int idx1, int idx2){
	
	int i;
	DSL1A temp;
	
	if(idx1 == idx2){
		
		return;
		
	}
	
	//printf("do cat\n");
	
	for(i=0; i<array[idx2].len; i++){
		
		array[idx1].values[array[idx1].len++] = array[idx2].values[i];
		
	}
	
	
	//array[idx1].len = strlen(array[idx1].values);
	
	for(i=0; i<array[idx2].len; i++){
		
		array[idx2].values[i] = -1;
		
	}
	
	//array[idx2].values[0] = '\0';
	
	array[idx2].len = 0;
	
}


DSL1A cat(DSL1A a, DSL1A b){
	
	int i;
	
	
	display(a);
	display(b);
	
	for(i=0; i<b.len; i++){
		
		a.values[a.len++] = b.values[i];
		
	}
	
	return a;
	
}



void display(DSL1A data){
	
	int i;
	
	for(i=0; i<data.len; i++){
		
		printf("%d", data.values[i]);
		
	}
	
	printf(", %d\n", data.len);
	
	
}