Hot Potato Routing
Time limit2sMemory limit1024 MB
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 nodes, and some pairs of nodes are connected by network links. Each node has a schedule. The schedule of node is a sequence of node numbers . The network works in ticks as follows.
During tick , 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 a packet is at its destination node, it is delivered at moment and removed from the network. Otherwise it is forwarded to node , 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 , the number of nodes (). The next lines describe the nodes. The -th node is described by its schedule length , followed by numbers (, ).
The next line contains , the number of packets (). Then follow lines, each describing one packet by its source node , destination node , and appearance time (, ).
Output
The first line contains , the maximum number of packets that can be delivered. The second line contains the numbers of the packets that can be delivered. Packets are numbered from 1 to in the order given in the input.