This page is still under construction.

Parts of this page are still being built. What you see may change.

Green Game

Time limit1sMemory limit128 MB

Summary
On a bipartite board where Ann and Billy alternately move a pawn, find all starting fields from which Ann can force the first repeated field's cycle to contain a green field.
Level

Hard8 of 10

Topics
Game theory, Graph, DFS, Implementation
Solved
No attempts yet

Problem

Green Game is a two-player game played by Ann and Billy. A single pawn is moved across a board of a+ba+b fields, numbered 11 through a+ba+b. Fields 11 through aa belong to Ann; fields a+1a+1 through a+ba+b belong to Billy. Each field is coloured either white or green.

Every field has a non-empty set of successor fields (the fields reachable from it in one move). The successors are arranged so that every move from one of Ann's fields leads to one of Billy's fields, and every move from one of Billy's fields leads to one of Ann's fields.

The pawn starts on a chosen field PP. The players then move the pawn in turns: from any field, its owner chooses one successor to move to. The first move is made by the owner of the starting field PP.

The game ends the moment the pawn lands on a field for the second time; call that field QQ. Consider the moves from the first visit of QQ up to its second visit. If the pawn stepped on at least one green field during that stretch, Ann wins; otherwise Billy wins.

Ann has a winning strategy for a starting field PP if she can play so that she wins no matter how Billy moves.

Given the board, determine every starting field for which Ann has a winning strategy.

Input

The first line contains two positive integers aa and bb, separated by a space: the number of fields owned by Ann and by Billy, with 1≤a+b≤30001 \le a+b \le 3000.

Each of the next a+ba+b lines describes one field, first Ann's fields (in order 1…a1 \dots a), then Billy's fields (in order a+1…a+ba+1 \dots a+b). The (i+1)(i+1)-th line describes field ii and starts with two integers zz and kk: the colour zz (00 for white, 11 for green) and the number of successors kk (1≤k<a+b1 \le k < a+b). It is followed by the kk successor field numbers. All integers on a line are separated by single spaces.

At most 100100 fields are green, and the total number of successors over all fields is at most 3000030000.

Output

On the first line, print a single integer ll: the number of fields for which Ann has a winning strategy. On each of the following ll lines, print one such field number, in increasing order.

Examples1

  1. Example 1

    Input
    5 3
    0 2 6 7
    0 3 6 7 8
    0 1 8
    1 1 7
    1 1 8
    1 2 1 2
    0 2 1 2
    0 2 3 4
    
    Expected output
    5
    1
    2
    4
    6
    7