Under construction

Still being built. Things may change

Colorgraph

Time limit2sMemory limit512 MB

Summary
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.
Level

Medium7 of 10

Topics
Graph, Math
Solved
No attempts yet

Problem

Lora is playing an online puzzle game. She receives an undirected graph with NN vertices, numbered from 1 to NN. 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 (A,B)(A, B), where:

  • A=1A=1 if the graph is red-connected, and A=0A=0 if it is not
  • B=1B=1 if the graph is blue-connected, and B=0B=0 if it is not

For example, state (1,0)(1, 0) 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 NN, the number of vertices. Each of the next NN lines contains NN space-separated numbers describing the edge colors. Let GijG_{ij} be the jj-th number on the ii-th line. If Gij=0G_{ij}=0, the edge between ii and jj is red. If Gij=1G_{ij}=1, it is blue. It is guaranteed that Gij=GjiG_{ij}=G_{ji}. For i=ji=j, the value of GijG_{ij} does not matter because the graph has no loops.

The last line contains two space-separated numbers AA and BB, 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 KK, the minimum number of clicks, on the first line. Each of the next KK 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.

3≤N≤2503 \le N \le 250

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.

Examples3

  1. Example 1

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

    Input
    3
    0 1 1
    1 0 0
    1 0 0
    1 1
    
    Expected output
    -1
    
  3. Example 3

    Input
    3
    0 1 1
    1 0 0
    1 0 0
    0 1
    
    Expected output
    0