codeforces 501C Misha and Forest(找边)
来源:互联网 发布:自卫队 解放军 知乎 编辑:程序博客网 时间:2024/05/01 12:12
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.
32 31 01 0
21 02 0
21 11 0
10 1
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".
题目大意:
已知每个点的度,每个点的所有相邻点的异或值。输出这个图的边。
题解:
由于图是个森林,所以一定存在度为1的点,而度为1的点相邻的点只有一个,我们就可以确定与它相邻的点。删除这个度为1的点以后,更新与它相邻的点的度和相应的异或值。继续找度为1的点,直到所有的点都被删除。这时我们就求出整个图了。
#include<stdio.h>#include<iostream>#include<algorithm>#include<queue>using namespace std;queue<int>q;int a[1000000],b[1000000],c[1000000],d[10000000],n,m,x;int main(){scanf("%d",&n);x=0;for(int i=0;i<n;i++){scanf("%d%d",&a[i],&b[i]);if(a[i]==1)q.push(i);} while(!q.empty()) { int k1=q.front(); q.pop(); if(a[k1]==0) continue; x++; int k2=b[k1]; c[x]=k1; d[x]=k2; a[k2]--; if(a[k2]==1) q.push(k2); b[k2]=b[k2]^k1; } printf("%d\n",x);for(int i=1;i<=x;i++ )printf("%d %d\n",c[i],d[i]);}
- codeforces 501C Misha and Forest(找边)
- CodeForces 501C Misha and Forest
- Codeforces 501C - Misha and Forest (机智)
- 【codeforces 501 C Misha and Forest】
- CodeForces 501C Misha and Forest
- codeforces 501 C. Misha and Forest (思维)
- CodeForces 501C Misha and Forest(异或和)
- codeforces 501C Misha and Forest(思维题)
- Codeforces 501C Misha and Forest【bfs+位运算】
- Codeforces 501C Misha and Forest 拓扑序
- codeforces#285--C - Misha and Forest(拓扑排序变形)
- Codeforces Round #285 C. Misha and Forest
- Misha and Forest CodeForces
- CodeForces 501C Misha and Forest (STL queue)(拓扑排序)
- Codeforces 501C:Misha and Forest(好题+思维+异或的运算规律+树)
- C - Misha and Forest
- codeforce #501 c Misha and Forest
- Codeforces Round #285 (Div. 2) C. Misha and Forest
- 周报9.19-9.25异常org.springframework.dao.InvalidDataAccessApiUsageException的处理
- Matlab基础之稀疏矩阵
- JAVA版GZipUtil.java
- Ubuntu16.04下Java环境安装与配置
- JBoss Web和 Tomcat的区别
- codeforces 501C Misha and Forest(找边)
- 多线程学习-ListenableFuture使用介绍以及示例
- 构建自己的 LINUX 系统(二)
- 算法-第四版-练习1.3.5解答
- nginx upstream的五种分配方式
- 变强
- 华为oj之字符串分割
- TreeMap
- 剑指offer--面试题50:树中两个结点的最低公共祖先