A war is being lead between two countries,
Now, more formally, consider a black box with the following operations:
setFriends(x,y)
shows that
setEnemies(x,y)
shows that
areFriends(x,y)
returns 1
if you are sure that 0
otherwise
areEnemies(x,y)
returns 1
if you are sure that 0
otherwise
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.:
- If
∼ and ∼ , then ∼ (The friends of my friends are my friends as well.) - If
∼ , then ∼ (Friendship is mutual.) ∼ (Everyone is a friend of himself.)
∗
is symmetric and irreflexive:
- If
∗ , then ∗ (Hatred is mutual.) - Not
∗ (Nobody is an enemy of himself.)
Also:
- If
∗ and ∗ , then ∼ (A common enemy makes two people friends.) - If
∼ and ∗ , then ∗ (An enemy of a friend is an enemy.)
Operations setFriends(x,y)
and setEnemies(x,y)
must preserve these properties.
Input Specification
The first line contains a single integer,
Each of the following lines contains a triple of integers,
setFriends
setEnemies
areFriends
areEnemies
and
The last line contains 0 0 0
.
All integers in the input file are separated by at least one space or line break. The only constraint
is
Output Specification
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 one line break.
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
(Note that this problem is sourced from UVa.)
Comments