CodeForces 501C Misha and Forest (STL queue)(拓扑排序)

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

C. Misha and Forest
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.


题目大意:给定一个数字n(范围在2^16之内),代表着一个无向图(不存在环)中的点的个数。在接下来的n行中,从点编号0到点编号n-1,给出每个点所拥有的相邻点的个数,以及相邻点编号的异或和。最后,输出这个图中每条边的情况。

解析:每次将度数为1的点放入一个队列中,对这个队列中的每一个点做处理。


AC代码如下:

#include <iostream>
#pragma comment(linker, "/STACK:1024000000,1024000000") 
#include <stdio.h>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <string>
#include <string.h>
#include <sstream>
#include <cctype>
#include <climits>
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <vector>
#include <iterator>
#include <algorithm>
#include <stack>
#include <functional>
//cout << "OK" << endl;
#define _clr(x,y) memset(x,y,sizeof(x))
#define _inf(x) memset(x,0x3f,sizeof(x))
#define pb push_back
#define mp make_pair
#define FORD(i,a,b) for (int i=(a); i<=(b); i++)
#define FORP(i,a,b) for (int i=(a); i>=(b); i--)
#define REP(i,n) for (int i=0; i<(n); i++)
using namespace std;
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
const double EULER = 0.577215664901532860;
const double PI = 3.1415926535897932384626;
const double E = 2.71828182845904523536028;
typedef long long LL;
LL pow_mod(LL a,LL n,LL m)
{
    if(n == 0) return 1;
    LL x = pow_mod(a,n>>1,m);
    LL ans = x*x%m;
    if(n&1) ans = ans*a%m;
    return ans;
}
int gcd(int a,int b){return b == 0 ? a : gcd(b,a%b);}

//定义一个结构体储存边,亦可以用vector<pair<int,int>>
struct{
    int a;
    int b;
}edges[70000];
int d[70000],s[70000];
int main(){
    int n;
    cin >> n;
    _clr(d,0);
    _clr(s,0);
    int numedges = 0, index = 0;
    //读入并求出总共的边数,边数为每个点的度数的和除以2.
    FORD(i,0,n-1){
        cin >> d[i] >> s[i];
        numedges += d[i];
    }
    numedges /= 2;
    queue<int> q;
    //将当前度数已经为1的点放入队列中.
    FORD(i,0,n-1){
        if(d[i] == 1){
            q.push(i);
        }
    }
    //对队列中的点进行处理.
    while(!q.empty()){
        int j = q.front();
        //这里要确认队列中的点是否在轮到处理它时,度数还仍旧为1,如果它的相邻边已经都被处理过了,
        //那么这时候它的度数为0,直接处理下一个.
        if(d[j] != 1){
            q.pop();
            continue;
        }
        //当当前处理的点度数为1的时候,它的s的值就是和它相连的那个点的编号.
        int temp = s[j];
        s[j] = 0;
        d[j]--;
        //储存边的信息.
        edges[index].a = temp;
        edges[index++].b = j;
        //处理当前点的唯一的相邻点的信息.
        s[temp] = s[temp]^j;
        d[temp]--;
        //如果当前点相邻的点的度数为1了,那么将其放入队列.
        if(d[temp] == 1){
            q.push(temp);
        }
        q.pop();
    }
    //输出结果
    cout << numedges << endl;
    FORD(i,0,numedges-1){
        cout << edges[i].a << " " << edges[i].b << endl;
    }
    return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值