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 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 Input
3
2 3
1 0
1 0
2
1 1
1 0
Sample Output
2
1 0
2 0
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".
思路
题意:
有一个森林包含0-n-1这n个节点,给出每个节点与它相邻节点的个数以及与它相邻节点的异或和,问有几条边,每条边的连接的两个节点是多少。
题解:
可以看作是一个拓扑排序,每次找出只有一个节点与之相邻的节点,那么与之相邻的节点的序号就是这个节点的异或和。
#include<bits/stdc++.h>
using namespace std;
const int maxn = (1<<16)+5;
int deg[maxn],sum[maxn]; int main()
{
int n;
queue<int>que;
scanf("%d",&n);
for (int i = 0;i < n;i++)
{
scanf("%d%d",°[i],&sum[i]);
if (deg[i] == 1) que.push(i);
}
vector<pair<int,int> >ans;
while (!que.empty())
{
int u = que.front();
que.pop();
if (deg[u] == 0) continue;
int v = sum[u];
ans.push_back(make_pair(u,v));
deg[v]--,sum[v] ^= u;
if (deg[v] == 1) que.push(v);
}
int size = ans.size();
printf("%d\n",size);
for (int i = 0;i < size;i++) printf("%d %d\n",ans[i].first,ans[i].second);
return 0;
}
Codeforces Round #285 (Div. 2)C. Misha and Forest(拓扑排序)的更多相关文章
- 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 ...
- 图论/位运算 Codeforces Round #285 (Div. 2) C. Misha and Forest
题目传送门 /* 题意:给出无向无环图,每一个点的度数和相邻点的异或和(a^b^c^....) 图论/位运算:其实这题很简单.类似拓扑排序,先把度数为1的先入对,每一次少一个度数 关键在于更新异或和, ...
- 水题 Codeforces Round #285 (Div. 2) C. Misha and Forest
题目传送门 /* 题意:给出无向无环图,每一个点的度数和相邻点的异或和(a^b^c^....) 图论/位运算:其实这题很简单.类似拓扑排序,先把度数为1的先入对,每一次少一个度数 关键在于更新异或和, ...
- 字符串处理 Codeforces Round #285 (Div. 2) B. Misha and Changing Handles
题目传送门 /* 题意:给出一系列名字变化,问最后初始的名字变成了什么 字符串处理:每一次输入到之前的找相印的名字,若没有,则是初始的,pos[m] 数组记录初始位置 在每一次更新时都把初始pos加上 ...
- Codeforces Round #292 (Div. 1) B. Drazil and Tiles 拓扑排序
B. Drazil and Tiles 题目连接: http://codeforces.com/contest/516/problem/B Description Drazil created a f ...
- Codeforces Round #541 (Div. 2) D 并查集 + 拓扑排序
https://codeforces.com/contest/1131/problem/D 题意 给你一个n*m二维偏序表,代表x[i]和y[j]的大小关系,根据表构造大小分别为n,m的x[],y[] ...
- Codeforces Round #285 (Div. 1) B - Misha and Permutations Summation 康拓展开+平衡树
思路:很裸的康拓展开.. 我的平衡树居然跑的比树状数组+二分还慢.. #include<bits/stdc++.h> #define LL long long #define fi fir ...
- Codeforces Round #292 (Div. 2) D. Drazil and Tiles [拓扑排序 dfs]
传送门 D. Drazil and Tiles time limit per test 2 seconds memory limit per test 256 megabytes Drazil cre ...
- Codeforces Round #660 (Div. 2) Captain Flint and Treasure 拓扑排序(按照出度、入读两边拓扑排序)
题目链接:Captain Flint and Treasure 题意: 一种操作为 选一个下标 使得ans+=a[i] 且 把a[b[i]]+a[i] 要求每个下标都进行一种这样的操作,问怎么样的 ...
随机推荐
- 2019 Multi-University Training Contest 1 - 1001 - Blank - dp
http://acm.hdu.edu.cn/showproblem.php?pid=6578 不会做,看题解. 设dp[i][j][k][l]表示4种颜色出现的最后的位置分别是i,j,k,l的方法数, ...
- 问题 D: 小k的硬币问题
问题 D: 小k的硬币问题 时间限制: 1 Sec 内存限制: 128 MB提交: 21 解决: 5[提交] [状态] [命题人:jsu_admin] 题目描述 小k和小p一起玩一个游戏,有n堆硬 ...
- Spring AOP 简单应用,对请求参数进行拦截处理
AOP的主要角色 切面:使用切点表达式表示,指定了当前切面逻辑所要包裹的业务模块的范围大小: Advice:也即切面逻辑,指定了当前用于包裹切面指定的业务模块的逻辑 Advice的主要类型 @Befo ...
- 一gradle创建SSM项目——依赖包
build.gradle compile:编译时必须. runtime:运行时必须,包括编译时. testCompile:测试编译时必须. testRuntime:测试运行时必须,包括编译时. 注:此 ...
- ll字段 详解 文件权限
文件类型和权限 硬链接总数 属主用户名 属组组名 文件大小(字节) 上次修改时间 文件/目录名 drwxr-xr-x 15 lb lb 4096 10月 19 01:11 ./ drwxr-xr-x ...
- Java虚拟机——类加载机制
转自:http://blog.csdn.net/ns_code/article/details/17881581 类加载过程 类从被加载到虚拟机内存中开始,到卸载出内存为止,它的整个生命周期包括:加载 ...
- linux--mongodb安装与配置
linux下的mongodb的安装: 在mongodb的官网上下载:mongodb-linux-x86_64-rhel62-3.2.3.gz1.解压: tar -xvf mongodb-linux-x ...
- 将两个列表合并为字典_其中一个列表为Key_一个列表为Value
#定义两个列表 list1 = range(0,10) list2 = range(10,20) #合并为字典,调用dict(zip()) dict_name = dict(zip(list1,lis ...
- 2017ICPC南宁M The Maximum Unreachable Node Set (偏序集最长反链)
题意:给你一张DAG,让你选取最多的点,使得这些点之间互相不可达. 思路:此问题和最小路径可重复点覆盖等价,先在原图上跑一边传递闭包,然后把每个点拆成两个点i, i + n, 原图中的边(a, b)变 ...
- Kubernetes部署DNS
前言 阅读地址 http://thoreauz.com/2017/04/16/docker/Kubernetes%E9%83%A8%E7%BD%B2DNS%E5%92%8CDashboard/ Kub ...