Colorgraph
Time limit2sMemory limit512 MB
Given a complete graph with red and blue edges, find the fewest edge color flips that reach the target red and blue connectivity state, and list those edges.
Problem
Lora is playing an online puzzle game. She receives an undirected graph with vertices, numbered from 1 to . Every pair of distinct vertices is joined by an edge, colored either blue or red.
The graph is red-connected if any vertex can reach any other vertex using only red edges. Similarly, it is blue-connected if any vertex can reach any other vertex using only blue edges.
The state of the graph is a pair , where:
- if the graph is red-connected, and if it is not
- if the graph is blue-connected, and if it is not
For example, state describes a graph that is red-connected but not blue-connected.
With one click on an edge, Lora can change its color, from blue to red or from red to blue. Given an initial graph and a desired final state, the goal is to reach a graph in that final state with the minimum number of clicks. Write a program colorgraph that finds this minimum number of clicks.
Input
The first line contains the positive integer , the number of vertices. Each of the next lines contains space-separated numbers describing the edge colors. Let be the -th number on the -th line. If , the edge between and is red. If , it is blue. It is guaranteed that . For , the value of does not matter because the graph has no loops.
The last line contains two space-separated numbers and , the desired final state.
Output
If the graph cannot be transformed into the desired state, print -1 on a single line.
Otherwise, print the integer , the minimum number of clicks, on the first line. Each of the next lines contains a pair of numbers, the two endpoints of an edge that Lora should click. If there is more than one solution, print any of them. The order of the edges and the order of the two endpoints in each pair do not matter.
Constraints
Time limit: 2000 ms. Memory limit: 512 MB.
Hints
Red edges are drawn with solid lines, and blue edges with dotted lines.
In the first sample, the initial graph is in state (1, 0):

After changing edges 1-3 and 4-3, the graph reaches the desired state (0, 1):

In the second sample, no graph with 3 vertices has state (1, 1).
In the third sample, the graph already has the desired final state.