• URAL 1069 Prufer Code(模拟)


    Prufer Code

    Time limit: 0.25 second
    Memory limit: 8 MB
    A tree (i.e. a connected graph without cycles) with vertices is given (N ≥ 2). Vertices of the tree are numbered by the integers 1,…,N. A Prufer code for the tree is built as follows: a leaf (a vertex that is incident to the only edge) with a minimal number is taken. Then this vertex and the incident edge are removed from the graph, and the number of the vertex that was adjacent to the leaf is written down. In the obtained graph once again a leaf with a minimal number is taken, removed and this procedure is repeated until the only vertex is left. It is clear that the only vertex left is the vertex with the number N. The written down set of integers (N−1 numbers, each in a range from 1 to N) is called a Prufer code of the graph.
    Your task is, given a Prufer code, to reconstruct a tree, i.e. to find out the adjacency lists for every vertex in the graph.
    You may assume that 2 ≤ N ≤ 7500

    Input

    A set of numbers corresponding to a Prufer code of some tree. The numbers are separated with a spaces and/or line breaks.

    Output

    Adjacency lists for each vertex. Format: a vertex number, colon, numbers of adjacent vertices separated with a space. The vertices inside lists and lists itself should be sorted by vertex number in an ascending order (look at sample output).

    Sample

    inputoutput
    2 1 6 2 6
    
    1: 4 6
    2: 3 5 6
    3: 2
    4: 1
    5: 2
    6: 1 2
    
    Problem Author: Magaz Asanov
    【题意】给你一个树(无向),每次去掉一个入度为零的编号最小的点,然后记录与这个点相邻的那个点,一直操作下去知道只剩下一个点。然后输入记录的点序列,输出那棵树。
    【分析】输入数据中未出现的点肯定是入度为0的,将他们全部放入优先队列(从小到大),然后依次取出输入数据和优先队列中的点,将其连边,并将输入数据的入度-1,若==1,
     放入优先队列。
     
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <algorithm>
    #include <cmath>
    #include <string>
    #include <map>
    #include <stack>
    #include <queue>
    #include <vector>
    #define inf 0x3f3f3f3f
    #define met(a,b) memset(a,b,sizeof a)
    #define pb push_back
    typedef long long ll;
    using namespace std;
    const int N = 7e3+600;
    const int M = 124750+10;
    const int mod=1e9+7;
    int n=0,m,k,tot=0,s,t;
    int head[N],vis[N],in[N],sum[N];
    struct cmp{bool operator () (int &a,int &b){return a>b;} };
    int main()
    {
        priority_queue<int,vector<int>,cmp>q;
        vector<int>vec,edg[N];int cnt=0;
        for(int i=1;i<N;i++)in[i]=1;
        while(~scanf("%d",&k)){
            vec.pb(k);in[k]++;n=max(n,k);//cnt++;if(cnt==7)break;
        }
        in[n]--;
        for(int i=1;i<=n;i++){
            if(in[i]==1){
                q.push(i);
            }
        }
        for(int i=0;i<vec.size();i++){
            int u=vec[i];
            int v=q.top();q.pop();
            edg[u].pb(v);edg[v].pb(u);
            in[u]--;
            if(in[u]==1)q.push(u);
        }
        for(int i=1;i<=n;i++){
            printf("%d:",i);
            sort(edg[i].begin(),edg[i].end());
            for(int j=0;j<edg[i].size();j++){
                printf(" %d",edg[i][j]);
            }printf("
    ");
        }
        return 0;
    }
  • 相关阅读:
    补交20145226蓝墨云班课 -- 程序设计中临时变量的使用
    补交20145226蓝墨云班课 -- MyCP
    补交20145226蓝墨云班课 -- MyOD
    补交20145226蓝墨云班课 -- Arrays和String单元测试
    补交20145226蓝墨云班课 -- 后缀表达式
    20145226夏艺华 《Java程序设计》 课堂实践
    20145226夏艺华 网络对抗技术 EXP9 web安全基础实践
    (转载)充分理解QML的属性绑定
    (转载)UML类图中的六大关系:关联、聚合、组合、依赖、继承、实现
    (转载)链路层MTU的概念?为什么MTU值普遍都是1500?
  • 原文地址:https://www.cnblogs.com/jianrenfang/p/6027634.html
Copyright © 2020-2023  润新知