CodeForces 501C Misha and Forest

来源:互联网 发布:经合组织数据库 编辑:程序博客网 时间:2024/05/22 08:24
C. Misha and Forest
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 of n vertices. For each vertex v from 0 to n - 1 he wrote down two integers, degreev and sv, 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 adjacent vertices, he wrote down 0).

Next day Misha couldn't remember what graph he initially had. Misha has values degreev and sv left, though. Help him find the number of edges and the edges of the initial graph. It is guaranteed that there exists a forest that corresponds to the numbers written by Misha.

Input

The first line contains integer n (1 ≤ n ≤ 216), the number of vertices in the graph.

The i-th of the next lines contains numbers degreei and si (0 ≤ degreei ≤ n - 10 ≤ si < 216), 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 two distinct numbers, a and b (0 ≤ a ≤ n - 10 ≤ b ≤ n - 1), corresponding to edge (a, b).

Edges can be printed in any order; vertices of the edge can also be printed in any order.

Examples
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".

题意:有n个点,编号为0~n-1,给出每个点的入度,以及与它相连点的编号异或结果,求出所有边的关系。
思路:这是一个无向无环图,也就是一棵树,那么它就肯定有叶子结点,叶子节点的度数为1,此时它相邻点的异或结果实际上就是所求点的编号了。然后把跟叶子节点相邻的点的度数-1,代表把叶子节点去除,此时异或结果是有变的。需要用本来的异或结果跟该叶子节点再异或一次,就得出除了这个叶子节点外其他点的异或值了,即a^b^c^a=b^c;  
#include<stdio.h>#include<string.h>#include<queue>#include<algorithm>using namespace std;struct node{int from,to;}t[110000];int ans[110000],degree[110000];int main(){int n,m,i,j,k,l,a,b;while(scanf("%d",&n)!=EOF){k=0;queue<int>q;for(i=0;i<n;i++){scanf("%d%d",°ree[i],&ans[i]);if(degree[i]==1)q.push(i);}k=0;while(!q.empty()){n=q.front();q.pop();if(degree[n]==1){m=ans[n];t[k].from=n;t[k++].to=m;degree[m]--;ans[m]^=n;if(degree[m]==1)q.push(m);}}printf("%d\n",k);for(i=0;i<k;i++)printf("%d %d\n",t[i].from,t[i].to) ;}return 0;}


0 0
原创粉丝点击