Let ' s define a forest as a non-directed acyclic graph (also without loops and parallel edges). One day Misha played with the forest consisting ofn vertices. For each vertex v from 0 to n?-? 1 He wrote down integers, degreev andsv, were the first integer is the number of vertices adjacent to VERTEX v, and the second integer is the XOR sum of the numbers of vertices adjacent to v (if there were no Adjac ENT vertices, he wrote down 0).
Next day Misha couldn ' t remember what graph he initially had. Misha has the valuesdegreev andsv left, though. Help him find the number of edges and the edges of the initial graph. It is guaranteed this there exists a forest that corresponds to the numbers written by Misha.
Input
The first line contains integer n (1?≤? N. ≤?2), the number of vertices in the graph.
The i-th of the next lines contains numbersdegreei andsi (0?≤? ) Degreei? ≤? n?-? 1,0?≤? si? <?2), separated by a space.
Output
In the first line print number m, the number of edges of the graph.
Next print m lines, each containing, distinct numbers,a and b (0?≤? A? ≤? n?-? 1,0?≤? b? ≤? n?-? 1), corresponding to edge(a,? b).
Edges can printed in any order; Vertices of the edge can also is printed in any order.
Sample Test (s) Input
32 31 01 0
Output
21 02 0
Input
21 11 0
Output
10 1
Note
The XOR sum of numbers is the result of bitwise adding numbers modulo 2. This operation exists in many modern programming languages. For example, in languages C + +, Java and Python it is represented as "^", and in Pascal-as "XOR".
Test instructions: Give a non-circular graph (note without loop) A total of n points enter the degree of each point and the XOR of all points adjacent to it
All edges in the output diagram
According to a non-circular graph, there must be a point with a degree of 1 (that is, adjacent to only one vertex), then its XOR is the subscript of its neighboring vertex.
This allows you to get an edge, and then simulate removing the vertex with the previous 1 degrees (the nearest vertex is 1, and the other, or the part that removes the vertex). Repeat the above operation to get all the edges
Code:
#include <iostream> #include <cstdio> #include <cstring> #include <queue>using namespace std; int Degree[66666],xsum[66666];int Edge[666666][2];int Main () { int i,j,k1,k2,n; int x=0; scanf ("%d", &n); queue<int>q; for (i=0; i<n; i++) { scanf ("%d%d", °ree[i],&xsum[i]); if (degree[i]==1) q.push (i); } while (!q.empty ()) { k1=q.front (); Q.pop (); if (degree[k1]==0) //The degree is 0 i.e. there is no edge because the degree becomes 0 continue after the queue may be joined ; x + +; K2=XSUM[K1]; Only one vertex is connected to it xorsum is that vertex subscript edge[x][0]=k1; EDGE[X][1]=K2; degree[k2]--; if (degree[k2]==1) Q.push (K2); XSUM[K2]=XSUM[K2]^K1; The essence of XOR is a^b=c a^c=b b^c=a; } cout<<x<<endl; for (i=1;i<=x;i++) printf ("%d%d\n", edge[i][0],edge[i][1]); return 0;}
Codeforce #501 c Misha and Forest