#P8383. [POI 2004] Bra

[POI 2004] Bra

Problem Description

Let us consider a circuit containing nn gates.

The gates are numbered from 00 to n1n-1. Each gate has several inputs and one output.

Each input and output can only be in one of three states: 00, 11, or 12\dfrac{1}{2}. Each input is connected to the output of some gate, so the state of an input is equal to the state of the output it is connected to. Each output may be connected to any number of inputs.

Gates 00 and 11 are two special gates. The output of gate 00 is always 00, and the output of gate 11 is always 11.

The condition for a gate’s output state is as follows:

  1. If the number of 00 among its inputs is greater than the number of 11, then the output state is 00.

  2. If the number of 00 among its inputs is equal to the number of 11, then the output state is 12\dfrac{1}{2}.

  3. If the number of 00 among its inputs is less than the number of 11, then the output state is 11.

  4. For gates 00 and 11, they output 00 and 11 respectively.

Now you are given the circuit information. Please write a program to determine the states of all gates whose states can be determined.

Input Format

The first line contains an integer nn.

The next n2n-2 lines describe the connections of the gates. Line ii describes the input terminals of gate ii: an integer kik_i indicating the number of its input terminals, followed by kik_i integers giving the indices of the gates connected to each input terminal.

Output Format

Output nn lines, representing the output states of the nn gates. If it can be determined, output the specific state value; otherwise output ??.

5
2 0 1
2 4 2
2 2 4
0
1
1/2
?
?

Hint

For all testdata, 2n100002 \le n \le 10000, ki1k_i \ge 1. The testdata guarantees that the total number of input terminals over all gates does not exceed 200000200000.

Translated by ChatGPT 5