题目链接:http://codeforces.com/problemset/problem/501/C

题目意思:有 n 个点,编号为 0 ~ n-1。给出 n 个点的度数(即有多少个点跟它有边相连)以及跟它相连的点的编号的异或结果。最后需要输出整幅图的所有边的情况。

  这道题确实是一道很好的题目!!!!它说拓扑排序的变形,需要队列的运用,还有就是异或计算的性质!!!(非一般厉害)

  由于是无向无环的简单图,换言之就是一棵树啦^_^。那么它就肯定有叶子结点,叶子节点的度数为1,此时它相邻点的异或结果实际上就是所求点的编号了。然后把跟叶子节点相邻的点的度数-1,代表把叶子节点去除,此时异或结果是有变的。需要用本来的异或结果跟该叶子节点再异或一次,就得出除了这个叶子节点外其他点的异或值了。举个例子吧,假如有一幅图是这样的。

  

0 的相邻点有1、 2、 3,异或出来的结果是0,它的度数是3.那么当处理0-1这条边时,容易知道去除1这个点后,只有2和3异或了:10 ^ 11 = 1,刚好等于 1 ^ 2 ^ 3 ^ 1 (01 ^ 10 ^ 11 ^ 01)。异或的一个性质就是a^b^c^a = b ^ c。是不是很神奇呢~~~~当然我们总是处理那些度数为1的点,把这些点放入队列里面,依次处理。

  

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <queue>
using namespace std; #define f first
#define s second const int maxn = (<<) + ;
int degree[maxn], XOR_sum[maxn]; int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif // ONLINE_JUDGE int n;
while (scanf("%d", &n) != EOF) {
queue<int> q;
pair<int, int> ans[maxn];
for (int i = ; i < n; i++) {
scanf("%d%d", &degree[i], &XOR_sum[i]);
if (degree[i] == )
q.push(i);
}
int cnt = ;
while (!q.empty()) {
int from = q.front();
q.pop();
if (degree[from] == ) { // 这句判断很重要,因为有可能degree[]--过程中使得变为0
int to = XOR_sum[from];
ans[cnt].f = from;
ans[cnt++].s = to;
degree[to]--;
XOR_sum[to] ^= from; // 异或性质
if (degree[to] == )
q.push(to);
}
}
printf("%d\n", cnt);
for (int i = ; i < cnt; i++)
printf("%d %d\n", ans[i].f, ans[i].s);
}
return ;
}

codeforces 501C. Misha and Forest 解题报告的更多相关文章

  1. Codeforces Educational Round 92 赛后解题报告(A-G)

    Codeforces Educational Round 92 赛后解题报告 惨 huayucaiji 惨 A. LCM Problem 赛前:A题嘛,总归简单的咯 赛后:A题这种**题居然想了20m ...

  2. codeforces 476C.Dreamoon and Sums 解题报告

    题目链接:http://codeforces.com/problemset/problem/476/C 题目意思:给出两个数:a 和 b,要求算出 (x/b) / (x%b) == k,其中 k 的取 ...

  3. Codeforces Round #382 (Div. 2) 解题报告

    CF一如既往在深夜举行,我也一如既往在周三上午的C++课上进行了virtual participation.这次div2的题目除了E题都水的一塌糊涂,参赛时的E题最后也没有几个参赛者AC,排名又成为了 ...

  4. CF 501C Misha and Forest 好题

    C. Misha and Forest   Let's define a forest as a non-directed acyclic graph (also without loops and ...

  5. codeforces 507B. Amr and Pins 解题报告

    题目链接:http://codeforces.com/problemset/problem/507/B 题目意思:给出圆的半径,以及圆心坐标和最终圆心要到达的坐标位置.问最少步数是多少.移动见下图.( ...

  6. codeforces 500B.New Year Permutation 解题报告

    题目链接:http://codeforces.com/problemset/problem/500/B 题目意思:给出一个含有 n 个数的排列:p1, p2, ..., pn-1, pn.紧接着是一个 ...

  7. codeforces B. Xenia and Ringroad 解题报告

    题目链接:http://codeforces.com/problemset/problem/339/B 题目理解不难,这句是解题的关键 In order to complete the i-th ta ...

  8. codeforces 462C Appleman and Toastman 解题报告

    题目链接:http://codeforces.com/problemset/problem/461/A 题目意思:给出一群由 n 个数组成的集合你,依次循环执行两种操作: (1)每次Toastman得 ...

  9. codeforces 460A Vasya and Socks 解题报告

    题目链接:http://codeforces.com/problemset/problem/460/A 题目意思:有一个人有 n 对袜子,每天早上会穿一对,然后当天的晚上就会扔掉,不过他会在 m 的倍 ...

随机推荐

  1. PostgreSQL中的时间操作总结

    取当前日期的函数: (1)       取当前时间:select now() (2)       取当前时间的日期: select current_date (3)       取当前具体时间(不含日 ...

  2. [译]git revert

    git revert git revert用来撤销一个已经提交了的快照. 但不是从项目历史中移除这个commit, 而是生成一个新的commit, 老的commit还是保留在历史项目里面的. 这样做的 ...

  3. [译]git checkout

    git checkout git checkout提供3种不同的功能: checking out文件, checking out commits, checking out branch. check ...

  4. [Storm] java.io.FileNotFoundException: File '../stormconf.ser' does not exist

    This bug will kill supervisors Affects Version/s: 0.9.2-incubating, 0.9.3, 0.9.4 Fix Version/s: 0.10 ...

  5. getRow()方法

    getRow :不是返回行数,而是返回当前是哪一行

  6. hdu.1044.Collect More Jewels(bfs + 状态压缩)

    Collect More Jewels Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  7. webservice测试实例

    webservice测试实例(LR8.1) 接口声明:这个接口是sina的短信服务接口,我只是用来做脚本学习使用,不会对其产生压力:希望读者也只是用来进行录制学习,而不是产生压力. 接口文档:http ...

  8. Android开发App工程结构搭建

    本文算是一篇漫谈,谈一谈关于android开发中工程初始化的时候如何在初期我们就能搭建一个好的架构.      关于android架构,因为手机的限制,目前我觉得也确实没什么大谈特谈的,但是从开发的角 ...

  9. 百度定位API报错:leaked ServiceConnection com.baidu.location.LocationClient$1@426122f0

    使用百度MapApi定位时候,当退出当时使用的activity后,则会报如题的异常,解决办法: 1:当退出当前定位的activity时,一定要在onDestroy方法中要mLocClient.stop ...

  10. 如何使用java自定义注解?demo

    1.Description.java package kzfy.bk.com; import java.lang.annotation.Documented; import java.lang.ann ...