Masters and Slaves

Time limit: 2s Memory limit: 256MB Input: Output:

William has finally solved the nasty “L/10-L/16-bipasso” problem, and is now ready to start earning money immediately! For this noble purpose, he arranged his NN fully functional computers (numbered from 00 to N1N - 1) into a tree-shaped network rooted at server 00, so that computer ii is a “slave” of computer MiM_i (conventionally, we assume that M0=1M_0 = -1). Now the most demanding tasks from all over the world are being delivered to his farm, and will eventually be completed (hopefully)!

Sadly, some of his computers are already experiencing severe heating problems, not being able to handle the output of all of their slaves. Since the recycling bin for electronic waste is always filling up, William can promptly solve this emergencies through proxying: when some computers are overheating, William selects KK of their slaves S0S_0, \dots, SK1S_{K-1} and delegates those to a newly-recycled proxy. Afterwards, the proxy is connected to another computer CC.

In this case, computers 11 and 22 are overheating (left) thus William delegates K=3K = 3 of their slaves (computers 55, 66, and 77) to the new proxy 88, connecting it to computer C=2C = 2. Even though this reconfiguration can solve the overheating problem, William is worried that the tree structure would grow too high, slowing down the computations. What is worse, a wrong reconfiguration could even create a cycle causing an infinite data flow!

Given a sequence of EE reconfigurations, help William keep track of the situation by determining whether each emergency causes the height to increase or, even worse, creates a cycle.

Input data

The first line contains integers NN, EE. The second line contains integers M0M_0, M1M_1, \dots , MN1M_{N-1}. Each of the following EE lines contains integers CC, KK followed on the same line by integers S0S_0, S1S_1, \dots, SK1S_{K-1}.

Output data

You need to write a single line with a sequence of characters, such that the ii-th character is:

  • c: if the ii-th reconfiguration creates a cycle;
  • 1: if the ii-th reconfiguration causes the tree height to increase;
  • 0: if the ii-th reconfiguration does not cause the tree height to increase.

You need to output a character until either a cycle is created or the reconfigurations are finished. In other words, the sequence can either consist of EE characters among 00 and 11 or less than EE characters among 00 and 11 followed by a single c.

Constraints and clarifications

  • 1N1 000 0001 \leq N \leq 1 \ 000 \ 000.
  • 1E10 0001 \leq E \leq 10 \ 000.
  • 0C,Si,Mi<N0 \leq C, S_i, M_i < N for each i=1N1i = 1 \dots N - 1, M0=1M_0 = −1.
  • 0K1000 \leq K \leq 100 in each emergency.
  • The master/slave structure forms a tree (there are no circular links).
  • The height of the tree never grows above 100100.
# Score Constraints
1 5 Examples
2 25 N,E10N, E \leq 10
3 30 N1 000N \leq 1 \ 000
4 20 SiS_i’s master is CC, for each ii and reconfiguration.
5 20 No additional limitations.

Example 1

stdin

3 5
-1 0 0
0 2 1 2
3 0
2 2 4 3
1 1 0
6 5 4 2 3 1 5

stdout

10c

Explanation

In the first sample case, the network structure evolves as follows:

After the third reconfiguration, a cycle is detected hence the following reconfigurations do not take place.

Example 2

stdin

8 4
-1 0 0 2 2 2 2 1
2 3 5 6 7
2 2 5 4
8 2 6 7
1 0

stdout

1010

Explanation

The second sample case corresponds to the structure described in the text, which evolves as follows:

Log in or sign up to be able to send submissions!