War

A war is being lead between two countries, A and B. As a loyal citizen of C, you decide to help your countrys 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 Cs 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. Now, more formally, consider a black box with the following operations: setFriends(x,y) setEnemies(x,y) areFriends(x,y) areEnemies(x,y) shows that x and y are from the same country shows that x and y are from different countries returns true if you are sure that x and y are friends 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. Ifx∼yandy∼zthenx∼z(Thefriendsofmyfriendsaremyfriendsaswell. 2. If x ∼ y then y ∼ x (Friendship is mutual.)
  2. x ∼ x (Everyone is a friend of himself.) ∗ is symmetric and irreflexive
  3. If x∗y then y∗x (Hatred is mutual.)
  4. Not x ∗ x (Nobody is an enemy of himself.) Also
  5. Ifx∗yandy∗zthenx∼z(Acommonenemymakestwopeoplefriends. 2. Ifx∼yandy∗zthenx∗z(Anenemyofafriendisanenemy. 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 c=1, c=2, c=3, c=4, following lines contains a triple of integers, cxy, where c is the code of the operation: setFriends setEnemies areFriends 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. The only constraint is n < 10000, the number of operations is unconstrained.

2/2 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 one line break. Sample Input 10 101 112 205 302 389 415 412 489 189 152 352 000 Sample Output 1 0 1 0 0 -1 0