C. Misha and Forest

 

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 - 1, 0 ≤ 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 - 1, 0 ≤ 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 test(s)
input
3
2 3
1 0
1 0
output
2
1 0
2 0
input
2
1 1
1 0
output
1
0 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,现在对于每一个节点,给出degi,sumi

degi表示节点i的度(入度+出度)

sumi表示所有和节点i直接相连的节点的编号的xor和

输出边的数量和所有边的2个端点

(e=(u,v),则输出u v)

思路:

突破口:当节点i为叶子节点时,只有一个入度,没有出度,则degi=1,这个时候fa[i]=sumi

这个时候和fa[i]相连的边确定了1条了,则:

deg[fa[i]]--

sum[fa[i]]=change(i,sum[fa[i]])

函数change(int b,int c)的功能:

有方程式x^b=c,现在我们知道了b,c,这个函数的返回值是x

那么当deg[fa[i]]==1的时候,就说明节点fa[i]还剩下1条边没有确定,而这条边的另一个顶点就是此时的sum[fa[i]]

则fa[fa[i]]=sum[fa[i]]

对于每一个节点,degi=0时说明没有边与之相连,不用管它

degi>0,就总会有degi=1的时候,这个时候,节点i的父亲节点就确定了

但是这样有一个问题:

对于根节点root是没有父亲节点的,但是会有deg[root]==1的时刻,按照上面的想法这个时候我们给了root一个父亲节点,而实际上, 我们不应该给root这个父亲节点

所以:对于节点i,满足2个条件,才可以确定父亲节点

1.deg[i]==1

2.fa[i.sum]==-1(为了防止给root父亲节点)

 #include<cstdio>
#include<cstring>
#include<queue> using namespace std; const int maxn=(<<)+; int fa[maxn];
struct Node
{
int deg,sum,cnt;
};
Node node[maxn]; inline int change(int b,int c)
{
int ret=;
int i=;
while(b>||c>)
{
if((b&)!=(c&))
{
ret+=(<<i);
}
b>>=;
c>>=;
i++;
}
return ret;
} void solve(int ); int main()
{
int n;
scanf("%d",&n);
int tmp=;
for(int i=;i<n;i++)
{
scanf("%d %d",&node[i].deg,&node[i].sum);
tmp+=node[i].deg;
node[i].cnt=i;
}
printf("%d\n",tmp/);
solve(n); return ;
} void solve(int n)
{
memset(fa,-,sizeof fa);
queue<Node>que;
while(!que.empty())
que.pop();
for(int i=;i<n;i++)
{
if(node[i].deg==)
que.push(node[i]);
}
while(!que.empty())
{
Node u=que.front();
que.pop();
if(fa[u.sum]<)
fa[u.cnt]=u.sum;
int j=fa[u.cnt];
node[j].deg--;
node[j].sum=change(u.cnt,node[j].sum);
if(node[j].deg==)
que.push(node[j]);
}
for(int i=;i<n;i++)
{
if(fa[i]!=-)
{
printf("%d %d\n",i,fa[i]);
}
}
return ;
}

CF 501C Misha and Forest 好题的更多相关文章

  1. codeforces 501C. Misha and Forest 解题报告

    题目链接:http://codeforces.com/problemset/problem/501/C 题目意思:有 n 个点,编号为 0 - n-1.给出 n 个点的度数(即有多少个点跟它有边相连) ...

  2. 水题 Codeforces Round #285 (Div. 2) C. Misha and Forest

    题目传送门 /* 题意:给出无向无环图,每一个点的度数和相邻点的异或和(a^b^c^....) 图论/位运算:其实这题很简单.类似拓扑排序,先把度数为1的先入对,每一次少一个度数 关键在于更新异或和, ...

  3. 图论/位运算 Codeforces Round #285 (Div. 2) C. Misha and Forest

    题目传送门 /* 题意:给出无向无环图,每一个点的度数和相邻点的异或和(a^b^c^....) 图论/位运算:其实这题很简单.类似拓扑排序,先把度数为1的先入对,每一次少一个度数 关键在于更新异或和, ...

  4. Codeforces Round #285 (Div. 1) A. Misha and Forest 拓扑排序

    题目链接: 题目 A. Misha and Forest time limit per test 1 second memory limit per test 256 megabytes 问题描述 L ...

  5. CF上的3道小题(2)

    CF上的3道小题(2) T1:CF630K Indivisibility 题意:给出一个数n,求1到n的数中不能被2到9中任意一个数整除的数. 分析:容斥一下,没了. 代码: #include < ...

  6. CF上的3道小题(1)

    CF上的3道小题 终于调完了啊.... T1:CF702E Analysis of Pathes in Functional Graph 题意:你获得了一个n个点有向图,每个点只有一条出边.第i个点的 ...

  7. 清橙A1206.小Z的袜子 && CF 86D(莫队两题)

    清橙A1206.小Z的袜子 && CF 86D(莫队两题) 在网上看了一些别人写的关于莫队算法的介绍,我认为,莫队与其说是一种算法,不如说是一种思想,他通过先分块再排序来优化离线查询问 ...

  8. 【Codeforces 501C】Misha and Forest

    [链接] 我是链接,点我呀:) [题意] 给你一棵树 但是每个节点只告诉你出度个数 以及所有和它相连的点的异或和. 让你还原这棵树 [题解] 叶子节点的话,他所有节点的异或和就是它那唯一的一个爸爸 因 ...

  9. Codeforces Round #285 (Div. 2)C. Misha and Forest(拓扑排序)

    传送门 Description Let's define a forest as a non-directed acyclic graph (also without loops and parall ...

随机推荐

  1. android中的Handler

    android的Handler   前言 学习android一段时间了,为了进一步了解android的应用是如何设计开发的,决定详细研究几个开源的android应用.从一些开源应用中吸收点东西,一边进 ...

  2. JsonModelStrategy策略添加

    今天在学习 zf2的时候,发现后端如果返回的是JsonModel的话,则在前台异步请求的时候发现取不到数据了,后来经过多次研究,才发现要在view_manager中创建策略,代ma是这样子的,stra ...

  3. HDU-4003 Find Metal Mineral (树形DP+分组背包)

    题目大意:用m个机器人去遍历有n个节点的有根树,边权代表一个机器人通过这条边的代价,求最小代价. 题目分析:定义状态dp(root,k)表示最终遍历完成后以root为根节点的子树中有k个机器人时产生的 ...

  4. Linux目录规范和含义(转)

      Linux目录配置 在了解了每个文件的相关种类与属性,以及了解了如何更改文件属性/权限的相关信息后,再来要了解的就是, 为什么每套Linux distributions他们的配置文件啊.执行文件啊 ...

  5. jQuery - 设置内容和属性

    设置内容 - text().html() 以及 val() 我们将使用前一章中的三个相同的方法来设置内容: text() - 设置或返回所选元素的文本内容 html() - 设置或返回所选元素的内容( ...

  6. 用max-width来防止图片溢出---不兼容ie6-----将max-width设置成父元素的宽度,当图片过大时会换行,以及限制图片的宽度。但是如果使用table,仍然可能超出限制。

    img{ max-width:100%;  height:auto; } 如题.

  7. Oracle数据库——Scheduler Job

    日常的运维工作中,我们经常使用Linux Server的anacron服务来使得服务器执行一下计划之内的任务,可以按照特定的时间间隔,重复的执行相关的命令或者相关的脚本,来完成预期的目标,能够节省相关 ...

  8. 5分钟让你学会用最高效的工具解析所有Json

    如果你是一个Android开发工程师,学会解析Json字符串是你的必修课,本篇文章主要以实例的方式手把手教你怎么做,花五分钟时间阅读本篇文章你就可以学会解析所有的Json字符串啦. 准备: json字 ...

  9. <button>会自动提交表单吗?

    点击button以后,表单先由ajax提交,然后无论后台返回什么结果,页面都会跳转到表单action属性指定的路劲,也就是login.html使用的是html.jquery.javascript,后台 ...

  10. linux概念之时间与时区

    http://www.cnblogs.com/liuyou/archive/2012/07/29/2614338.html Linux时间基准 以上我们了解了RTC(实时时钟.硬件时钟)和OS时钟(系 ...