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
问题描述
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.
输入
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.
输出
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.
样例
input
3
2 3
1 0
1 0
output
2
1 0
2 0
题意
给你每个点的度数和所有与它直接相连的点的编号的xor的值,求这棵森林的所有的边。
题解
每次找度数为1的点,用队列维护一下,类似于拓扑排序跑一跑就可以了。
代码
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<vector>
#define X first
#define Y second
#define mp make_pair
using namespace std;
const int maxn =(1<<16)+10;
int n, m;
int deg[maxn], sum[maxn];
int main() {
scanf("%d", &n);
queue<int> Q;
for (int i = 0; i < n; i++) {
scanf("%d%d", °[i], &sum[i]);
if (deg[i] == 1) Q.push(i);
}
vector<pair<int, int> > ans;
while (!Q.empty()) {
int u = Q.front(); Q.pop();
if (deg[u] == 0) continue;
int v = sum[u];
ans.push_back(mp(v, u));
deg[v]--; sum[v] ^= u;
if (deg[v] == 1) Q.push(v);
}
printf("%d\n", ans.size());
for (int i = 0; i < ans.size(); i++) printf("%d %d\n", ans[i].X, ans[i].Y);
return 0;
}
Codeforces Round #285 (Div. 1) A. Misha and Forest 拓扑排序的更多相关文章
- 图论/位运算 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)C. Misha and Forest(拓扑排序)
传送门 Description Let's define a forest as a non-directed acyclic graph (also without loops and parall ...
- 字符串处理 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] 要求每个下标都进行一种这样的操作,问怎么样的 ...
随机推荐
- Javascript之动画1
我们来做一个小球运动的例子:在这个demo中,我们要求连续多次点击小球“开始”按钮,小球运动速度就越来越快.但是还是不完美存在BUG的,经过多次点击“开始”按钮再点击"停止"按钮就 ...
- JqueryPagination 分页插件使用说明
JqueryPagination是个简单轻量级的分页插件,使用起来很容易,只要初始化一个实例,并设置总数量.翻页回调函数.其它参数就可以实现无刷新分页功能了 1 首先引入相关js文件: <lin ...
- linux 环境变量的设置【转】
现在使用linux的朋友越来越多了,在linux下做开发首先就是需要配置环境变量,下面以java环境变量为例介绍三种配置环境变量的方法. 1.修改/etc/profile文件 如果你的计算机仅仅作为开 ...
- android 网络_网络源码查看器
xml设计 <?xml version="1.0"?> -<LinearLayout tools:context=".MainActivity" ...
- 使用tortoisegit管理git 和 权限验证
1 安装Git 2 安装Tortoise 3 在git.oschina 或者 github上创建项目 4 Tortoise配置 TortoiseGit 使用扩展名为ppk的密钥,而不是ssh-keyg ...
- Win7在CMD命令行中使用管理员权限运行命令
使用命令: runas /user:administrator 需要执行的命令 如下:
- linux服务器报No space left on device错误的解决过程记录
起因 今天在本地提交了点代码,但到服务器上git pull的时候提示No space left on device,第一反应是猜想可能硬盘满了(很有可能是log导致的),不过想想又觉得不太可能,这台服 ...
- PHP类的自动载入机制
php的自动加载: 在php5以前,我们要用某个类或类的方法,那必须include或者require,之后才能使用,每次用一个类,都需要写一条include,麻烦 php作者想简单点,最好能引用一个类 ...
- 2013-07-22 IT 要闻速记快想
### ========================= ### 如何让用户点击广告.观看广告并乐在其中?这个问题的答案精彩纷呈.有的公司开创模式,为点击广告的用户提供优惠券:有的公司想法新奇,让用 ...
- Linux 使用退格键时出现^H解决方法
当我们再和脚本交互的时候,在终端上输错了内容,使用退格键,屏幕上会出现乱码,比如 ^H.^H不是H键的意思,是backspace. 主要是当你的终端backspace有问题的时候才需要设置. 解决方法 ...