URAL 1069 Prufer Code(模拟)
Prufer Code
Memory limit: 8 MB
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.
Input
Output
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 |
#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+;
const int M = +;
const int mod=1e9+;
int n=,m,k,tot=,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=;
for(int i=;i<N;i++)in[i]=;
while(~scanf("%d",&k)){
vec.pb(k);in[k]++;n=max(n,k);//cnt++;if(cnt==7)break;
}
in[n]--;
for(int i=;i<=n;i++){
if(in[i]==){
q.push(i);
}
}
for(int i=;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]==)q.push(u);
}
for(int i=;i<=n;i++){
printf("%d:",i);
sort(edg[i].begin(),edg[i].end());
for(int j=;j<edg[i].size();j++){
printf(" %d",edg[i][j]);
}printf("\n");
}
return ;
}
URAL 1069 Prufer Code(模拟)的更多相关文章
- ural 1069. Prufer Code
1069. Prufer Code Time limit: 0.25 secondMemory limit: 8 MB A tree (i.e. a connected graph without c ...
- URAL 1069 Prufer Code 优先队列
记录每个节点的出度,叶子节点出度为0,每删掉一个叶子,度数-1,如果一个节点的出度变成0,那么它变成新的叶子. 先把所有叶子放到优先队列中. 从左往右遍历给定序列,对于root[i],每次取出叶子中编 ...
- Prufer Code
1069. Prufer Code Time limit: 0.25 secondMemory limit: 8 MB A tree (i.e. a connected graph without c ...
- URAL 1792. Hamming Code (枚举)
1792. Hamming Code Time limit: 1.0 second Memory limit: 64 MB Let us consider four disks intersectin ...
- [fun code - 模拟]孤独的“7”
今天看到朋友圈里有人发了一张孤独的7的题目,第一反应就是模拟后计算出结果,而女朋友则更爱推理,手算.
- URAL 1944 大水题模拟
D - Record of the Attack at the Orbit Time Limit:1000MS Memory Limit:65536KB 64bit IO Format ...
- UVA 1593: Alignment of Code(模拟 Grade D)
题意: 格式化代码.每个单词对齐,至少隔开一个空格. 思路: 模拟.求出每个单词最大长度,然后按行输出. 代码: #include <cstdio> #include <cstdli ...
- Ural 1780 Gray Code 乱搞暴力
原题链接:http://acm.timus.ru/problem.aspx?space=1&num=1780 1780. Gray Code Time limit: 0.5 secondMem ...
- URAL 2073 Log Files (模拟)
题意:给定 n 场比赛让你把名称,时间,比赛情况按要求输出. 析:很简单么,按照要求输出就好,注意如果曾经AC的题再交错了,结果也是AC的. 代码如下: #pragma comment(linker, ...
随机推荐
- Rhel6-keepalived+lvs配置文档
系统环境: rhel6 x86_64 iptables and selinux disabled 主机: 192.168.122.119 server19.example.com 192.168.12 ...
- 通过获取DNS解析的未转义主机名,区分测试环境和正式环境代码
ASP.Net编程中经常有一些代码,测试环境下需要执行,而正式环境下不需要执行(或者反之). 我们经常做的方式是:去掉注释,测试,再注释,再编译上传(或者反之). 现在,不妨试试以下办法: Reque ...
- history对象
1.history对象前进 history.forward() 2.history对象后退 history.back() 3.history对象跳入指定页面 history.go(-1): //当前 ...
- HTML--9表单和验证事件
1.表单验证<form></form> (1).非空验证(去空格) (2).对比验证(跟一个值对比) (3).范围验证(根据一个范围进行判断) (4).固定格式验证:电话号码, ...
- 一个很好介绍js的例子
function UpdateInit(opt){ this.init(opt);} UpdateInit.prototype={ loadUrl:null, loadParam:null, befo ...
- python随笔
1. 使用iter实现接收用户多行输入 stopword = '' str = '' print('请将要添加的内容输入下方,输入空白行按回车退出程序:') for line in iter(inpu ...
- USB peripherals can turn against their users
Turning USB peripherals into BadUSB USB devices are connected to – and in many cases even built into ...
- 利用烧鹅制作简单BadUSB,插谁谁怀孕
所用硬件设备为烧鹅,烧鹅是RadioWar基于Teensy++ 2.0 AT90USB1286芯片设计的USB Rubber Ducky类开发板. 使用veil编码meterpreter生成paylo ...
- 怎么在官网下载jstl【配图详解】
JSTL(JSP Standard Tag Library,JSP标准标签库)是一个非常优秀的开源JSP标签库,如果要在系统使用JSTL标签,则必须将jstl.jar和 standard.jar文件放 ...
- Interview----判断整数序列是否是二叉搜索树的后序遍历结果
题目:输入一个整数数组,判断该数组是不是某二元查找树的后序遍历的结果. 如果是返回true,否则返回false. 例如输入5.7.6.9.11.10.8,由于这一整数序列是如下树的后序遍历结果: ...