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

input output
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("\n");}return 0;
}

转载于:https://www.cnblogs.com/jianrenfang/p/6027634.html

URAL 1069 Prufer Code(模拟)相关推荐

  1. Ural 1780 Gray Code 乱搞暴力

    原题链接:http://acm.timus.ru/problem.aspx?space=1&num=1780 1780. Gray Code Time limit: 0.5 second Me ...

  2. UA MATH575B 数值分析下VI 统计物理的随机模拟方法2

    UA MATH575B 数值分析下VI 统计物理的随机模拟方法2 扩散过程的停时问题 理论解 模拟解 扩散过程的停时问题 简单一点,考虑一维的随机扩散问题,假设扩散系数D=12D=\frac{1}{2 ...

  3. android uinput 按键_android 使用uinput模拟输入设备的方法

    在google remote中,android接收端接收socket发来的IR CODE,然后将IR CODE模拟出来发给系统处理,这就是google remote接收端的原理. 系统端怎样模拟inp ...

  4. selenium 模拟登陆 古诗文网 含验证码

    ocr.py / 阿里云市场 import base64 import json import urllib.request from urllib import parse import ssl s ...

  5. pku,zju题目分类

    哎呦喂,直接ctrl+A了.话说浙江大学的题还见过的呢.. 公告: [意见反馈][官方博客]   ural pku Zju 题目分类 收藏   感谢 mugu 的提供.... Ural Problem ...

  6. 《题目与解读》红书 训练笔记目录《ACM国际大学生程序设计竞赛题目与解读》

    虽然2012年出版的老书了,但是是由三次世界冠军的上海交大ACM队出版的书籍,选择的题目是ACM经典中的经典,书中有非常详细的题解,可以学到很多东西,值得一刷. 目录 第一部分 第一章 数学 1.1 ...

  7. day3----编码-集合-深浅copy-文件操作-函数初识

    day3----编码-集合-深浅copy-文件操作-函数初识 本文档主要内容: 一 编码 二 集合 三 深浅copy 四 文件操作 五 函数初识 首先,我们来看看两个字符串的比较 打开cmd,进入do ...

  8. 计算器软件----表达式求值

    学完栈写了一个计算器小程序, 基本功能如下: 1.支持基本的+ - * / % 运算 2.支持sin.cos.tan.ln.log.乘方(^).开方(sqrt).指数(exp) 求值 3.支持括号及括 ...

  9. java编程代码大全_掌握Java编程技巧,代码重构

    代码重构在不改变软件系统外部行为的前提下,改善它的内部结构,通过调整程序代码改善软件的质量.性能,使其程序的设计模式和架构更趋合理,提高软件的扩展性和维护性. 代码重构目标 持续纠偏和改进软件设计 随 ...

  10. python的主要应用领域及常用的函数模块_python之常用模块篇5

    一.日志模块,logging模块 1)logging模块简单使用,屏幕输出.默认级别30 import logging logging.debug('debug...') # 10logging.in ...

最新文章

  1. python变量类型-Python变量类型
  2. 2016年9月c语言真题,2016年9月计算机二级C语言基础试题及答案
  3. android动态权限封装,Android 动态权限申请的封装
  4. pep 8 规范的一些记录
  5. 15 PP配置-生产计划-主数据-定义特殊采购类型
  6. Qt使用Qtxlsx读写xlsx文件
  7. NFC技术:Android中的NFC技术
  8. R语言常用数据文件的导入
  9. 【MisakaHookFinder使用方法】关于如何提取一个文字游戏的文本钩子以供翻译的方法
  10. A Density-Based Algorithmfor Discovering Clusters in LargeSpatial Databaseswith Noise(KDD-96)
  11. linux安装mailx发邮件
  12. 二次开发手册——百家CMS微商城说明文档(5)
  13. HTTP接口测试工具及使用
  14. 计划排程系统用户画象分析
  15. spydroid-ipcamera-master.zip
  16. SCI和EI期刊会议知识导航
  17. Hive3.x数仓开发
  18. 大数据系统计算的概念全面解析
  19. MT8168中断流程
  20. grails 调用java_GSP在Grails中的使用

热门文章

  1. Typora图片上传 —— PicGo-Core篇
  2. POI XssfCellStyle背景颜色对照
  3. WPS的JS宏设置文档的表格底色
  4. 计算机桌面ie图标无法删除,Win10系统中桌面ie图标无法删除如何解决
  5. 笔记本linux系统重装步骤(Centos7.0)
  6. GitHub怎么搜索项目
  7. DateTime.Now函数详解 所有用法
  8. 小明左右手分别拿两张纸牌:黑桃10和红心8,现在交换手中的牌。编写并输出互换后的结果,输出结果如图所示
  9. 网络安全职业_如何开始网络安全职业
  10. 查询ISBN号验证ISBN号调用api接口查询书籍信息