Codeforces 501C Misha and Forest(bfs)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces 501C Misha and Forest(bfs)相关的知识,希望对你有一定的参考价值。

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

题意:

 

思路:

 

代码:

#include <iostream>
#include <stdio.h>
#include <cstring>
#include <queue>
#include <set>
#include <cmath>
#include <time.h>
#include <cstdlib>
#include <algorithm>
#define lson (i<<1)
#define rson (lson|1)
using namespace std;

typedef long long LL;
const int N = 1000007;
const int INF = 0x7fffffff;

struct Node
{
    int idx;
    int de;
    int val;
}dt[N];

struct Ans
{
    int a, b;
}ans[N];

int n, vis[N], cnt_ans;

void bfs()
{
    queue<int> que;
    for (int i = 0; i < n; i++)
    {
        if (dt[i].de == 1)
        {
            vis[i] = 1;
            que.push(i);
        }
    }
    while (!que.empty())
    {
        int t = que.front();
        que.pop();
        struct Node temp = dt[t];
        if (dt[temp.idx].de == 0)
            continue;
        ans[cnt_ans].a = temp.idx;
        ans[cnt_ans].b = temp.val;
        cnt_ans++;
        dt[temp.val].de--;
        dt[temp.val].val ^= temp.idx;
        if (dt[temp.val].de == 1 && !vis[temp.val])
        {
            vis[temp.val] = 1;
            que.push(temp.val);
        }
    }
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0);
    while (cin >> n)
    {
        cnt_ans = 0;
        memset(vis, 0, sizeof(vis));
        for (int i = 0; i < n; i++)
        {
            dt[i].idx = i;  
            cin >> dt[i].de >> dt[i].val;
            if (dt[i].de == 0)
                vis[i] = 1;
        }
        bfs();
        cout << cnt_ans << endl;
        for (int i = 0; i < cnt_ans; i++)
            cout << ans[i].a << " " << ans[i].b << endl;
    }
    return 0;
}

 

以上是关于Codeforces 501C Misha and Forest(bfs)的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces 832D - Misha, Grisha and Underground

CodeForces 501B Misha and Changing Handles(STL map)

Misha, Grisha and Underground CodeForces - 832D (倍增树上求LCA)

Codeforces Round #425 (Div. 2) Problem D Misha, Grisha and Underground (Codeforces 832D) - 树链剖分 - 树状

CodeForces - 501B Misha and Changing Handles

Codeforces Round #425 (Div. 2) D. Misha, Grisha and Underground