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;
}
时间: 2024-08-01 19:42:20

Codeforces 501C Misha and Forest(bfs)的相关文章

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】C. Ice Cave(bfs)

我了个草,这个题明明bfs不知道谁挂了个dfs+剪枝的标签... 从起点bfs一步一步搜,碰到X判断是不是终点,如果是终点就结束,如果为'.',那么把该位置改成X,坐标入队. #include<cstdio> #include<cstring> #include<queue> #include<algorithm> #include<iostream> using namespace std; const int maxn = 505; cons

pots(BFS)

D - Pots Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Description You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: Input On the first and

USACO抓牛catchcow (bfs)

这题是黄巨大出的比赛题. http://poj.org/problem?id=3278 Description Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤

Codeforces Round #259 (Div. 2) (序列)

题目链接:http://codeforces.com/contest/454/problem/B B. Little Pony and Sort by Shift time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output One day, Twilight Sparkle is interested in how to sort a se

hdu 1728 逃离迷宫 (BFS)

逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 14376    Accepted Submission(s): 3458 Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可以穿越,有些地方

牛汇(BFS)入金具体流程(图文指导)

牛汇开户流程:bfsforex牛汇入金教程 所谓入金,也就是充值的意思,必须充钱到平台才能进行外汇交易.首先,我们先登录bfsforex牛汇官方网站,在交易办公室功能区域下面,点击账户入金: 为您提供中国各大银行的网银支付解决方案,支持人民币支付,和信用卡入金,入金是实时到账的. 牛汇(BFS)入金具体流程(图文指导),布布扣,bubuko.com

URAL 1930 Ivan&#39;s Car(BFS)

Ivan's Car Time limit: 1.5 secondMemory limit: 64 MB The world is in danger! Awful earthquakes are detected all over the world. Houses are destroyed, rivers overflow the banks, it is almost impossible to move from one city to another. Some roads are

【判重+广搜(bfs)】魔板

判重+广搜(bfs)]魔板 Time Limit: 1000MS Memory Limit: 32768KB Special Judge 有一个两行四列的魔板,每个格子里有一个1到8的数字(数字唯一),现在我们可以对魔板进行以下操作: 1.交换两行的数字. 2.将第一列移到第二列,第二列到第三列,第三列到第四列,第四列到第一列. 3.将中间四个数顺时针转一次. 现给你初始状态,我末状态请你用最小的步数将它从初始状态变到末状态. 输入: 前两行,每行4个数表示初状态. 后两行,每行4个数表示末状态