UVa10158,

## Problem:

Problem B: War

A war is being lead between two countries, A and B. As a loyal citizen of C, you decide to help your country’s espionage by attending the peace-talks taking place these days (incognito, of course). There are n people at the talks (not including you), but you do not know which person belongs to which country. You can see people talking to each other, and through observing their behaviour during their occasional one-to-one conversations, you can guess if they are friends or enemies. In fact what your country would need to know is whether certain pairs of people are from the same country, or they are enemies. You may receive such questions from C’s government even during the peace-talks, and you have to give replies on the basis of your observations so far. Fortunately nobody talks to you, as nobody pays attention to your humble appearance.

Abstract
Now, more formally, consider a black box with the following operations:
setFriends(x, y)     shows that x and y are from the same country
setEnemies(x, y)   shows that x and y are from different countries
areFriends(x, y)     returns true if you are sure that x and y are friends
areEnemies(x, y)   returns true if you are sure that x and y are enemies
The first two operations should signal an error if they contradict with your former knowledge. The two relations ‘friends’ (denoted by ~) and ‘enemies’ (denoted by *) have the following properties:
~ is an equivalence relation, i.e.
1.      If x ~ y and y ~ z then x ~ z  (The friends of my friends are my friends as well.)
2.      If x ~ y then y ~ x                  (Friendship is mutual.)
3.      x ~ x                                       (Everyone is a friend of himself.)
* is symmetric and irreflexive
4.      If x * y then y * x                  (Hatred is mutual.)
5.      Not x * x                                (Nobody is an enemy of himself.)
Also
6.      If x * y and y * z then x ~ z   (A common enemy makes two people friends.)
7.      If x ~ y and y * z then x * z   (An enemy of a friend is an enemy.)
Operations setFriends(x, y) and setEnemies(x, y) must preserve these properties.

Input
The first line contains a single integer, n, the number of people.
Each of the following lines contains a triple of integers, c x y, where c is the code of the operation:
c = 1, setFriends
c = 2, setEnemies
c = 3, areFriends
c = 4, areEnemies
and x and y are its parameters, which are integers in the range [0, n), identifying two (different) people. The last line contains 0 0 0.
All integers in the input file are separated by at least one space or line break.

Output
For every ‘areFriends’ and ‘areEnemies’ operation write 0 (meaning no) or 1 (meaning yes) to the output. Also for every ‘setFriends’ or ‘setEnemies’ operation which contradicts with previous knowledge, output a –1 to the output ; note that such an operation should produce no other effect and execution should continue. A successful ‘setFriends’ or ‘setEnemies’ gives no output.
All integers in the output file must be separated by at least one space or line break.

Constraints
n < 10000, the number of operations is unconstrained.

Sample Input
10
1 0 1
1 1 2
2 0 5
3 0 2
3 8 9
4 1 5
4 1 2
4 8 9
1 8 9
1 5 2
3 5 2
0 0 0

Sample Output
1
0
1
0
0
-1
0

## Source Code:

Using Union-Find data structure, Parent tells the root of the tree, while rank tells which side it is on.

```//Wed Jun  9 23:10:38 CDT 2010
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#define MMAX 10000
using namespace std;

class War
{
public:
int parent;
int rank; //0, means friends; otherwise, enemy;
};

vector<War> v(MMAX);

void Makeset(int N)
{
for (int i = 0; i <= N; i++)
{
v[i].parent = i;
v[i].rank = 0;
}
}

int Find(int x)
{
if (x == v[x].parent)
return x;
int temp = v[x].parent;
v[x].parent = Find(temp);
// v[x].rank = v[temp].rank == 0 ? v[x].rank : (1 - v[x].rank);
// int temp = Find(v[x].parent);
v[x].rank = (v[x].rank + v[temp].rank) & 1;
// v[x].parent = temp;
return v[x].parent;
}

void Union(int t1, int t2, int x, int y, int d)
{
v[t2].parent = t1;
//v[t2].rank = v[x].rank == v[y].rank ? d : (1 - d);
v[t2].rank = (v[x].rank + v[y].rank + d) & 1;
Find(x);
}

int Judge(int t1, int t2, int x, int y)
{
if (t1 == t2)
{
if (v[x].rank == v[y].rank)
return 0; //Friends;
else
return 1; //Enemy;
}
return -1;
}

int main(int argc, char* argv[])
{
// freopen("input.in", "r", stdin);
// freopen("output.out", "w", stdout);
int N;
cin >> N;
Makeset(N);
int a, b, c;
while (cin >> a >> b >> c && (a + b + c))
{
int t1, t2;
if (a == 1)
{
t1 = Find(b);
t2 = Find(c);
if (Judge(t1, t2, b, c) == 1)
cout << -1 << endl;
else
Union(t1, t2, b, c, 0);
}
else if (a == 2)
{
t1 = Find(b);
t2 = Find(c);
if (Judge(t1, t2, b, c) == 0)
cout << -1 << endl;
else
Union(t1, t2, b, c, 1);
}
else if (a == 3)
{
t1 = Find(b);
t2 = Find(c);
if (Judge(t1, t2, b, c) == 0)
cout << 1 << endl;
else
cout << 0 << endl;
}
else if (a == 4)
{
t1 = Find(b);
t2 = Find(c);
if (Judge(t1, t2, b, c) == 1)
cout << 1 << endl;
else
cout << 0 << endl;
}
}
// fclose(stdin);
// fclose(stdout);
return 0;
}
```