Persuading the Advisers
Time limit1sMemory limit256 MB
Two rivals take turns claiming undecided experts for opposite sides, and the first asks whether he can force the majority-vote tree to favor him.
- Level
Hard8 of 10
- Topics
- Game theory, Tree, Dynamic programming, Greedy
- Solved
- No attempts yet
Problem
Bajtazar wants to build a racetrack in Byteotia. Bajtymon competes for the same money and would rather build a ski jump. Both projects are expensive, so the two men ask the king of Byteotia for funding.
The king funds exactly one of them, the racetrack or the ski jump. Before he decides he asks the Chief Adviser, who heads the hierarchy of royal advisers. An adviser is one of two kinds. An expert gives a recommendation on his own, and every other adviser heads a team of advisers. A team head recommends the option that the majority of his team members support. Every team has an odd number of members, so the majority is always settled. The final recommendation therefore depends only on what the experts support, an expert being an adviser who heads no team. Every adviser other than the Chief Adviser has exactly one superior.
Bajtazar and Bajtymon do not wait idly. They persuade the experts. Persuading one expert takes exactly one day, and a persuaded expert never changes his mind. Some experts already hold an opinion at the start and will not change it.
Every day at dawn Bajtazar picks one undecided expert and visits him to win him over to the racetrack. Bajtymon does not get up that early, so later the same day he picks one of the remaining undecided experts and wins him over to the ski jump. That is why he loses the chance to persuade the expert Bajtazar visited that day. If no undecided expert is left that day, Bajtymon persuades nobody. The two act this way until every expert holds an opinion. Both of them know the hierarchy of advisers.
Decide whether Bajtazar can plan his persuading so that the Chief Adviser recommends building the racetrack, whatever Bajtymon does.
Input
The first line contains one integer (), the number of advisers. The advisers are numbered from to , and adviser is the Chief Adviser. The -th of the next lines describes adviser . The line starts with an integer (). If , then adviser is an expert and the line contains only . The values , and mean that he is for the racetrack, for the ski jump, and undecided, respectively. If , then is odd and adviser heads a team of members whose numbers follow on the rest of the line. Every adviser with a number greater than belongs to exactly one team.
Output
If Bajtazar cannot persuade the experts in a way that makes the Chief Adviser recommend the racetrack, print one line with the word NIE. Otherwise print two lines. The first line contains the word TAK and an integer . Here counts the ways Bajtazar can choose the expert to persuade on the first day such that, playing optimally on the later days, he is still sure of the favorable recommendation. The second line contains the numbers of those experts in increasing order. If every expert already holds an opinion at the start and the recommendation favors Bajtazar, print and leave the second line empty.