``````/*
* UVA_10158_1.cpp
*
*  Created on: 2013年11月5日
*/

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int maxn = 10010;

int p[2*maxn];
int n;

int find(int x){
if(p[x] == x){
return x;
}

return (p[x] = find(p[x]));
}

void setFriends(int x,int y){
int x1 = find(x);
int x2 = find(y);
int y1 = find(x+n);
int y2 = find(y+n);

if(x1 == y2 || x2 == y1){
printf("-1\n");
}else{
p[x2] = x1;//将y加入到x的阵营之中
p[y2] = y1;//将y的敌人加入到x的敌人的阵营之中
}
}

void setEnemies(int x,int y){
int x1 = find(x);
int x2 = find(y);
int y1 = find(x+n);
int y2 = find(y+n);

if(x1 == x2){
printf("-1\n");
}else{
p[y1] = x2;//将y将入到x的敌人之中
p[y2] = x1;
}
}

void areFriends(int x,int y){
if(find(x) == find(y)){
printf("1\n");
}else{
printf("0\n");
}
}

void areEnemies(int x,int y){
int x1 = find(x);
int x2 = find(y);
int y1 = find(x + n);
int y2 = find(y + n);

if(x2 == y1 || x1 == y2){
printf("1\n");
}else{
printf("0\n");
}
}

int main(){
scanf("%d",&n);

int i;
for(i = 1 ; i <= 2*n ; ++i){
p[i] = i;
}

int c,x,y;
while(scanf("%d%d%d",&c,&x,&y),c||x||y){
if(c == 1){
setFriends(x,y);
}else if(c == 2){
setEnemies(x,y);
}else if(c == 3){
areFriends(x,y);
}else if(c == 4){
areEnemies(x,y);
}
}

return 0;
}``````