Under construction

Still being built. Things may change

Hot Potato Routing

Time limit2sMemory limit1024 MB

Summary
Choose the largest set of packets that all reach their destinations on fixed node schedules, with no two packets at the same node in the same tick.
Level

Medium7 of 10

Topics
Simulation, Graph, Brute force
Solved
No attempts yet

Problem

The hot potato principle is a packet routing method for networks. Its main advantages are that it needs no routing tables and no packet analysis during routing.

The network has nn nodes, and some pairs of nodes are connected by network links. Each node has a schedule. The schedule of node ii is a sequence of node numbers ai,0,ai,1,…,ai,li−1a_{i,0}, a_{i,1}, \ldots, a_{i, l_i - 1}. The network works in ticks as follows.

During tick tt, several packets may be in the network. Each packet has a source node and a destination node, and during a tick it is located at some node. Each packet has an appearance time. At the start of that tick, it appears at its source node. If during tick tt a packet is at its destination node, it is delivered at moment tt and removed from the network. Otherwise it is forwarded to node ai,t mod lia_{i, t \bmod l_i}, where it arrives at the start of the next tick. If several packets arrive at the same node at the start of a tick, all of them are destroyed.

The main drawback of this routing principle follows from its main advantages. Without routing tables and packet analysis, the node schedules and packet appearance times must be chosen very carefully so that all packets are delivered. Even with a good choice, often not all packets can be delivered.

You are given the network description and a set of packets. For each packet you may decide whether to try to deliver it. Choose the maximum number of packets such that all of them are delivered during the operation of the network.

Input

The first line contains nn, the number of nodes (1≤n≤1001 \le n \le 100). The next nn lines describe the nodes. The ii-th node is described by its schedule length lil_i, followed by lil_i numbers ai,0,ai,1,…,ai,li−1a_{i,0}, a_{i,1}, \ldots, a_{i,l_i-1} (1≤li≤81 \le l_i \le 8, ai,j≠ia_{i,j} \ne i).

The next line contains pp, the number of packets (1≤p≤1001 \le p \le 100). Then follow pp lines, each describing one packet by its source node sis_i, destination node did_i, and appearance time tit_i (si≠dis_i \ne d_i, 0≤ti≤10000 \le t_i \le 1000).

Output

The first line contains kk, the maximum number of packets that can be delivered. The second line contains the kk numbers of the packets that can be delivered. Packets are numbered from 1 to pp in the order given in the input.

Examples1

  1. Example 1

    Input
    4
    2 2 3
    3 1 1 3
    3 4 4 2
    1 3
    3
    1 3 0
    3 1 2
    4 2 3
    
    Expected output
    2
    2 3