Vasya's graph
Time limit2sMemory limit256 MB
Given K forbidden node pairs and M edges offered in order, keep each edge only if it does not connect any forbidden pair, and report which edges are kept.
- Level
Hard8 of 10
- Topics
- Union-find, Graph, DFS, Implementation
- Solved
- No attempts yet
Problem
Vasya has got a graph. The graph has nodes, but it has no edges yet. Vasya cares a lot about the future graph structure: he knows pairs of nodes {, }, such that if there is a path between these nodes in the graph, the irredeemable will happen to the graph. Vasya must prevent it at all costs.
Vasya has made a list of unoriented edges. Vasya will examine the edges in the preset order and he will surely put them into the graph, if possible. If adding another edge will cause the irredeemable, Vasya will simply discard such an edge. Your task is to find out which edges are good for the graph and which ones must end up in the trash.
Input
The first line of the input file contains three integers , and (, ).
It is followed by lines, with the -th line containing two integers and , the numbers of conflicting nodes, which should not have any edges between them (). The conflicting node pairs are unique.
Next come lines with the -th line containing two integers and , the numbers of nodes of the edge which can be added to the graph (). These edges are provided in the order of examination. Edges in the list are unique.
Output
The first line of the output file must contain the number of edges that Vasya can accomodate into the graph. The second line must contain space-separated numbers of edges in the ascending order.