codeforces #285 div.2 C. Misha and Forest

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

题意:    给出图的每个顶点的度数和相邻顶点的异或值的和,求出这个图的边的情况和边的数目:

分析:   找出度为一的点,其所对应的异或值即为与之相邻的顶点(<- ^ ->),再将与之相邻顶点的度数减一:

**********代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;

const int maxn=1<<16+5;
int du[maxn],ytot[maxn];
vector< pair<int ,int > > V;

int main()
{
    int n,u,v;
    int i;
    while(scanf("%d",&n)!=EOF)
    {
         queue<int> Q;
         for(i=0;i<n;i++)
            V.clear();
         for(i=0;i<n;i++)
            scanf("%d%d",&du[i],&ytot[i]);
         for(i=0;i<n;i++)
            if(du[i]==1)   Q.push(i);
         while(!Q.empty())
            {
               u=Q.front();
               Q.pop();
               if(du[u]!=1) continue;
                 v=ytot[u];
                V.push_back(make_pair(u,v));
                du[v]--;
                 ytot[v]^=u;
                if( du[v]==1) Q.push(v);
            }
            printf("%d\n",V.size());
            for(i=0;i<V.size();i++)
                printf("%d %d\n",V[i].first,V[i].second);
    }
    return 0;
}
时间: 2025-01-15 06:11:40

codeforces #285 div.2 C. Misha and Forest的相关文章

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

题目传送门 1 /* 2 题意:给出无向无环图,每一个点的度数和相邻点的异或和(a^b^c^....) 3 图论/位运算:其实这题很简单.类似拓扑排序,先把度数为1的先入对,每一次少一个度数 4 关键在于更新异或和,精髓:a ^ b = c -> a ^ c = b, b ^ c = a; 5 */ 6 #include <cstdio> 7 #include <cstring> 8 #include <cmath> 9 #include <algorith

Codeforces Round #285 (Div. 2) C - Misha and Forest

思路:类似一个拓扑排序的题,根据 对度数为1的点,它所连的边的编号即为异或和这一 规律,直接进行拓扑排序即可. 每次对得到的节点度数减一,并且异或上他所连的节点. 代码: #include<cstdio> #include<cstring> #include<iostream> #include<vector> #include<algorithm> #include<queue> using namespace std; typed

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

题目传送门 1 /* 2 题意:给出一系列名字变化,问最后初始的名字变成了什么 3 字符串处理:每一次输入到之前的找相印的名字,若没有,则是初始的,pos[m] 数组记录初始位置 4 在每一次更新时都把初始pos加上去,那么就保证更新了初始的名字,这也是唯一要思考的地方了:) 5 */ 6 #include <cstdio> 7 #include <iostream> 8 #include <cstring> 9 #include <algorithm> 1

【codeforces #285(div 1)】

打完这场,从紫名回到蓝名了 A. Misha and Forest time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Let's define a forest as a non-directed acyclic graph (also without loops and parallel edges). One day Mish

CodeForces Round #285 Div.2

C.Misha and Forest (图论 BFS) 比赛进行了一半才想起来有场CF没打,=_=|| 前两道题快速切掉,C题一直卡没什么好的思路 憋了几天,忍不住偷偷瞄了一下别人AC的代码,发现我题没看清题目,题中说了给出的图是森林. 于是切入点找到了! 题意: 一个由n个节点构成的森林,编号从0到n-1,给出每个节点的 度数 和 相邻节点编号的异或和,输出图中的边数和所有邻接节点的编号. 分析: 因为是森林,所以图中的叶子节点就是突破口.叶子节点最明显的特征就是: 度数为1 它相邻节点编号的

codeforces#285--C - Misha and Forest(拓扑排序变形)

C - Misha and Forest Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Description Let's define a forest as a non-directed acyclic graph (also without loops and parallel edges). One day Misha played with the

Codeforces #258 Div.2 E Devu and Flowers

大致题意: 从n个盒子里面取出s多花,每个盒子里面的花都相同,并且每个盒子里面花的多数为f[i],求取法总数. 解题思路: 我们知道如果n个盒子里面花的数量无限,那么取法总数为:C(s+n-1, n-1) = C(s+n-1, s). 可以将问题抽象成:x1+x2+...+xn = s, 其中0<=xi <= f[i],求满足条件的解的个数. 两种方法可以解决这个问题: 方法一:这个问题的解可以等价于:mul = (1+x+x^2+...+x^f[1])*(1+x+x^2+...+x^f[2]

Codeforces #259 Div.2

A. Little Pony and Crystal Mine 模拟题. 用矩阵直接构造或者直接根据关系输出 B. Little Pony and Sort by Shift 模拟题. 通过提供的操作得到的序列只能是两段递增或者整个序列递增. 那么可以求得第一段递增序列长度为0-p 如果整个序列是递增,即 p= n-1 那么操作次数就是0. 否则,假设是两段递增,把原始的序列恢复出来,设当前序列是AB,那么A就是0-p BA = (A'B')', '表示对序列进行翻转, 如果BA是有序的,那么需

Codeforces #250 (Div. 2) C.The Child and Toy

之前一直想着建图...遍历 可是推例子都不正确 后来看数据好像看出了点规律 就抱着试一试的心态水了一下 就....过了..... 后来想想我的思路还是对的 先抽象当前仅仅有两个点相连 想要拆分耗费最小,肯定拆相应权值较小的 在这个基础上考虑问题就能够了 代码例如以下: #include <cstdio> #include <iostream> #include <algorithm> #define MAXN 10010 #define ll long long usi