首页 > 其他分享 >Codeforces Round #285 (Div. 2) C. Misha and Forest

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

时间:2023-04-24 22:37:54浏览次数:33  
标签:degree int fath Misha Codeforces Forest vertices include numbers


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.

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

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

Examples
inputCopy
3
2 3
1 0
1 0
outputCopy
2
1 0
2 0
inputCopy
2
1 1
1 0
outputCopy
1
0 1
Note
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”.

给定节点的度数以及与其相邻节点的异或和,求边数以及边的两个端点;
思路:
从叶子入手,其异或和就是其父节点的编号,用 队列进行维护一下,记得到叶子节点时更新其父节点时degree–;

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<string>
#include<cstring>
#include<set>
#include<queue>
#include<map>
using namespace std;
#define maxn 100005
#define inf 0x3f3f3f3f
typedef long long ll;
typedef unsigned long long int ull;
struct node
{
    int degree,s,i;
    int vis;
    void output()
    {
        cout<<i<<' '<<degree<<' '<<s<<endl;
    }
}v[maxn];
typedef pair<int,int> pii;
vector<pii> edge;
vector<pii>::iterator it;
queue<int>q;

int main()
{
    ios::sync_with_stdio(false);
    int n;cin>>n;
    int cnt=0;
    for(int i=0;i<n;i++){
        cin>>v[i].degree>>v[i].s;
        if(v[i].degree==1)q.push(i);
    }
    while(!q.empty()){
        int i=q.front();
        q.pop();
        if(v[i].degree==1){
            int fath=v[i].s;
            v[fath].s=i^v[fath].s;
            v[fath].degree--;
            edge.push_back(make_pair(i,fath));
            if(v[fath].degree==1)q.push(fath);
        }
    }
    cout<<edge.size()<<endl;
    for(it=edge.begin();it!=edge.end();it++){
        pii p=*it;
        cout<<p.first<<' '<<p.second<<endl;
    }
}


标签:degree,int,fath,Misha,Codeforces,Forest,vertices,include,numbers
From: https://blog.51cto.com/u_15657999/6221931

相关文章

  • Codeforces Round #450 (Div. 2) D. Unusual Sequences 数学
    Countthenumberofdistinctsequencesa1, a2, …, an(1 ≤ ai)consistingofpositiveintegerssuchthatgcd(a1, a2, …, an) = xand.Asthisnumbercouldbelarge,printtheanswermodulo109 + 7.gcdheremeansthegreatestcommondivisor.Input......
  • Codeforces Round #272 (Div. 2) D. Dreamoon and Sets 数学
    Dreamoonlikestoplaywithsets,integersand.isdefinedasthelargestpositiveintegerthatdividesbothaandb.LetSbeasetofexactlyfourdistinctintegersgreaterthan0.DefineStobeofrankkifandonlyifforallpairsofdistinctelemen......
  • Codeforces Round #308 (Div. 2) E. Vanya and Brackets
    Vanyaisdoinghismathshomework.Hehasanexpressionofform,wherex1, x2, …, xnaredigitsfrom1to9,andsignrepresentseitheraplus‘+’orthemultiplicationsign‘*’.Vanyaneedstoaddonepairofbracketsinthisexpressionsothattoma......
  • Educational Codeforces Round 147
    A题意思路有前导零结果直接为0,出现在第一位的?贡献为9,其他地方的?贡献为10。代码#include<bits/stdc++.h>usingnamespacestd;usingll=longlong;chars[10];intmain(){intT;scanf("%d",&T);while(T--){scanf("%s",s);......
  • Educational Codeforces Round 147 (Rated for Div. 2)
    题目链接B核心思路真的需要加强了,看到这个最大值和最小值我们其实也需要往对应的最大值和最小值的相关操作去想,不如前缀最小值/前缀最大值或者是后缀最小值/后缀最大值。这里一个比较直接的想法就是想找到不同的地方,然后看不可以扩展。那么怎么看是否可以扩展呢,如果是左边的话......
  • Educational Codeforces Round 127 (Rated for Div. 2)
    题目链接D核心思路首先挖掘下操作的性质吧:x>1&&x+3<10:1xx+1x+2x+310我们可以发现这样子好像对于我们的结果是没有影响的,答案还是9.所以这个性质就挖掘出来了啊,只要我们把一段连续的插入到对应的区间里面就好了。也就是只要这个区间的左端点小于插入连续的数的最小值,......
  • Codeforces Beta Round 96 (Div. 1) -- C. Logo Turtle (dp,记忆化搜索)
    记忆化搜索就是暴力,多一步优化,走过的路别走了。说实话,可能是数据水了,居然能过。constintN=510;strings;intn,ans;boolst[501][501][2][50];voiddfs(intx,intidx,intdir,intk){ if(st[x][idx][dir][k])return; st[x][idx][dir][k]=1;//走过的路不走......
  • Educational Codeforces Round 147 (A-D)
    A.Matching橘子熊:这题太简单了我不想写题面Description给定给一个带问号的字符串,求有多少种可能的数字Input多次询问,一次一个字符串Output对于每次询问,输出可能的数字的总数数据范围与约定2e5次询问,单词询问不超过5个字符思路主要思路签到题大部分情况下,一个......
  • Educational Codeforces Round 39 (Rated for Div. 2) -- D. Timetable (DP)
    写得很折磨人,每次dp都写个一个多小时,写出来明明觉得不难.题目大意:可以进行K次操作,把删除1,进行k次操作后每行第一个1和最后一个1的位置相减的绝对值加1得到的结果最小。做法:每次肯定是要从左删或者从右边删,然后顺着这个思路,先把每行的进行小于等于k次操作时,每行最小......
  • codeforces 559C Gerald and Giant Chess(dp+组合数学)
    题目链接:codeforces559C题目大意:给出一个h*r的矩阵,从左上角走到右下角,中间有一些点不能经过,问不同的路径有多少种?题目分析:首先我们考虑一个n*m的矩阵,从左上角只能向右或向下走能走到右下角的方案数,也就是C(n+m,n),就是一共要走n+m次,选出n次横着走。那么我们定义dp[i]表示在前不经......