zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    蝴蝶自在——《萍踪侠影》
    学习OpenCV——关于三通道的CvMat的求和问题
    MFC中的OnTimer和SetTimer
    慎重选择博士后(或博士生)导师
    MFC界面的完善
    MFC CSplitterWnd的用法
    断言(ASSERT)的用法
    OpenCV中lib的添加
    【转】数据结构之位图
    【转】关于windows server 2003不能共享问题
  • 原文地址:https://www.cnblogs.com/jianrenfang/p/6027634.html
Copyright © 2011-2022 走看看