C.Misha and Forest (图论 BFS)

比赛进行了一半才想起来有场CF没打,=_=||

前两道题快速切掉,C题一直卡没什么好的思路

憋了几天,忍不住偷偷瞄了一下别人AC的代码,发现我题没看清题目,题中说了给出的图是森林。

于是切入点找到了!

题意:

一个由n个节点构成的森林,编号从0到n-1,给出每个节点的 度数 和 相邻节点编号的异或和,输出图中的边数和所有邻接节点的编号。

分析:

因为是森林,所以图中的叶子节点就是突破口。叶子节点最明显的特征就是:

  • 度数为1
  • 它相邻节点编号的异或和 就是 它唯一的邻居本身的编号

这样就找到一条边。

所以我们就可以像剥洋葱一样,把外面的叶子节点一层一层的剥去。

可以用一个队列来维护,开始将所有的叶子节点入队,然后逐个向内拓展。如果将这个叶子节点去掉后又出现新的叶子节点,则将新节点入队,直到队列为空。

 #include <cstdio>

 const int maxn = ( << ) + ;

 int degree[maxn], xorsum[maxn];
int edge[maxn][], cnt = ;
int Q[maxn], head = , tail = ;
int main()
{
int n;
scanf("%d", &n);
for(int i = ; i < n; i++)
{
scanf("%d%d", &degree[i], &xorsum[i]);
if(degree[i] == ) Q[tail++] = i;
} while(head < tail)
{
int t = Q[head++];
if(degree[t] == ) continue;
degree[t] = ;
int neighbor = xorsum[t];
edge[cnt][] = t; edge[cnt++][] = neighbor;
degree[neighbor]--;
xorsum[neighbor] ^= t;
if(degree[neighbor] == ) Q[tail++] = neighbor;
} printf("%d\n", cnt);
for(int i = ; i < cnt; ++i) printf("%d %d\n", edge[i][], edge[i][]); return ;
}

代码君

CodeForces Round #285 Div.2的更多相关文章

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

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

  2. 字符串处理 Codeforces Round #285 (Div. 2) B. Misha and Changing Handles

    题目传送门 /* 题意:给出一系列名字变化,问最后初始的名字变成了什么 字符串处理:每一次输入到之前的找相印的名字,若没有,则是初始的,pos[m] 数组记录初始位置 在每一次更新时都把初始pos加上 ...

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

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

  4. Codeforces Round #285 (Div. 2) A B C 模拟 stl 拓扑排序

    A. Contest time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  5. Codeforces Round #285 (Div. 2) A, B , C 水, map ,拓扑

    A. Contest time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  6. Codeforces Round #285 (Div.1 B & Div.2 D) Misha and Permutations Summation --二分+树状数组

    题意:给出两个排列,求出每个排列在全排列的排行,相加,模上n!(全排列个数)得出一个数k,求出排行为k的排列. 解法:首先要得出定位方法,即知道某个排列是第几个排列.比如 (0, 1, 2), (0, ...

  7. 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 ...

  8. Codeforces Round #285 (Div. 1) B - Misha and Permutations Summation 康拓展开+平衡树

    思路:很裸的康拓展开.. 我的平衡树居然跑的比树状数组+二分还慢.. #include<bits/stdc++.h> #define LL long long #define fi fir ...

  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. C#快速学习笔记(译)

    下面是通过代码快速学习C#的例子. 1.学习任何语言都必定会学到的hello,world! using System; public class HelloWorld { public static ...

  2. Cocos2d-x第一个坑,NDK 编译环境

    这些天搭建windows cocos2d-x的环境,基本上崩溃到死.目前好转.终于可以编译通过: 生成模板工程:在cmd下进入cocos2d-x的主目录,D:\Android\cocos2d-x-2. ...

  3. Microsoft Visual Studio Ultimate 2013 旗舰版 有效注册密钥

    Visual Studio Ultimate 2013 KEY(密钥):BWG7X-J98B3-W34RT-33B3R-JVYW9 Visual Studio Premium 2013 KEY(密钥) ...

  4. Intel Edison的那些事:修改Edison的HTTP服务的页面

    Intel Edison配置好之后,按住PWR键2-7秒(4秒恰到好处),就可以进入AP热点模式(此时,Arduino扩展板上的灯不停闪烁),可以将笔记本接入Edison的热点,然后在浏览器中访问“h ...

  5. [转]java gridbag 说明

    gridx = 2; // X2 gridy = 0; // Y0 gridwidth = 1; // 横占一个单元格 gridheight = 1; // 列占一个单元格 weightx = 0.0 ...

  6. centos6.5安装配置LDAP服务[转]

    安装之前查一下 1 find / -name openldap* centos6.4默认安装了LDAP,但没有装ldap-server和ldap-client 于是yum安装 1 su root 2 ...

  7. js 判断页面加载状态

    //----判断当前页面是否加载状态 开始 ---- document.onreadystatechange = subSomething;//当页面加载状态改变的时候执行这个方法. function ...

  8. 自定义UICollectionViewLayout并添加UIDynamic - scorpiozj(转)

    转载自:http://www.tuicool.com/articles/jM77Vf     自定义UICollectionViewLayout并添加UIDynamic UICollectionVie ...

  9. html template

    https://wrapbootstrap.com/tag/single-page http://themeforest.net/ https://wrapbootstrap.com/themes h ...

  10. oracle安装完成后解锁scott用户

    需要以管理员的身份 进行 解锁scott alter user scott account unlock;