码迷,mamicode.com
首页 > 编程语言 > 详细

codeforces#285--C - Misha and Forest(拓扑排序变形)

时间:2015-01-16 10:05:48      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:

C - Misha and Forest
Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

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.

Sample Input

Input
3
2 3
1 0
1 0
Output
2
1 0
2 0
Input
2
1 1
1 0
Output
1
0 1

Hint

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个点组成一个森林,给出每个定点的周围的点数和点数的异或值,找出深林的边数。

每个叶子节点的相邻点数为1,异或值为它的父节点,一直从叶子节点向前找。。。


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std ;
struct node
{
    int d , v ;
} p[70000] , q[70000];
queue <int> que ;
int vis[70000] ;
int main()
{
    int n , i , j , cnt ;
    while( scanf("%d", &n) != EOF )
    {
        memset(vis,0,sizeof(vis)) ;
        cnt = 0 ;
        while( !que.empty() ) que.pop() ;
        for(i = 0 ; i < n ; i++)
        {
            scanf("%d %d", &p[i].d, &p[i].v) ;
            if( p[i].d == 1 && vis[p[i].v] == 0 )
            {
                q[cnt].d = i ;
                q[cnt++].v = p[i].v ;
                que.push(i) ;
                vis[i] = 1 ;
            }
        }
        while( !que.empty() )
        {
            i = que.front() ;
            que.pop() ;
            j = p[i].v ;
            p[j].d -= 1 ;
            p[j].v = p[j].v ^ i ;
            if( p[j].d == 1 && vis[ p[j].v ] == 0 )
            {
                que.push( j ) ;
                vis[j] = 1 ;
                q[cnt].d = j ;
                q[cnt++].v = p[j].v ;
            }
        }
        printf("%d\n", cnt) ;
        for(i = 0 ; i < cnt ; i++)
            printf("%d %d\n", q[i].d, q[i].v) ;
    }
    return 0 ;
}


codeforces#285--C - Misha and Forest(拓扑排序变形)

标签:

原文地址:http://blog.csdn.net/winddreams/article/details/42773329

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!