• Codeforces 501C Misha and Forest【bfs+位运算】


    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 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 - 10 ≤ 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 - 10 ≤ 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
    input
    3
    2 3
    1 0
    1 0
    
    output
    2
    1 0
    2 0
    
    input
    2
    1 1
    1 0
    
    output
    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".


    题意:
    定义一个森林是一个无向无环的图,给你n个顶点(从0 - n-1)。对于每一个顶点有输入两个值,一个表示顶点的度数,一个表示与该顶点按位异或的和。(没有,就记住0)。现在要你根据这个条件把图复原出来。
    思路:
    对于叶子来说,只有一条边与他相连,把这条边打印,然后去掉,继续寻找叶子,直到结束。
    如果一个数异或另外一个数两次,那么就变成了这个数本身。利用这个特点,将每个叶子的影响去掉。

    #include <map>
    #include <set>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <queue>
    #include <iostream>
    #include <stack>
    #include <cmath>
    #include <string>
    #include <vector>
    #include <cstdlib>
    //#include <bits/stdc++.h>
    //#define LOACL
    #define space " "
    using namespace std;
    //typedef long long LL;
    //typedef __int64 Int;
    typedef pair<int, int> PAI;
    const int INF = 0x3f3f3f3f;
    const double ESP = 1e-5;
    const double PI = acos(-1.0);
    const int MOD = 1e9 + 7;
    const int MAXN = (1 << 16) + 10;
    PAI data[MAXN];
    int main() {
        int n;
        while (scanf("%d", &n) != EOF) {
            queue <int> que;
            for (int i = 0; i < n; i++) {
                scanf("%d%d", &data[i].first, &data[i].second);
                if (data[i].first == 1) que.push(i);
            }
            PAI ans[MAXN];
            int cnt = 0;
            while (que.size()) {
                int u = que.front(); que.pop();
                if (data[u].first != 1) continue;
                int v = data[u].second; data[u].first = 0;
                ans[cnt].first = u, ans[cnt++].second = v;
                data[v].second ^= u; data[v].first--;
                if (data[v].first == 1) {
                    que.push(v);
                }
            }
            printf("%d
    ", cnt);
            for (int i = 0; i < cnt; i++) {
                printf("%d %d
    ", ans[i].first, ans[i].second);
            }
        }
        return 0;
    }



  • 相关阅读:
    BP神经网络的数学原理及其算法实现
    机器学习必知的10大算法
    支持向量机通俗导论(理解SVM的三层境界)
    svm算法 最通俗易懂讲解
    read/write/fsync与fread/fwrite/fflush的关系和区别
    ubuntu16.04编译QT5.6所依赖的库
    Linux下的tar压缩解压缩命令详解
    Ubuntu16下编译linux内核,报"mkimage" command not found错的解决
    Git之(一)Git是什么[转]
    mac上完整卸载删除:android studio方案
  • 原文地址:https://www.cnblogs.com/cniwoq/p/6770792.html
Copyright © 2020-2023  润新知