第七届河南省赛G.Code the Tree(拓扑排序+模拟)
G.Code the Tree
Time Limit: 2 Sec Memory Limit: 128 MB Submit: 35 Solved: 18 [Submit][Status][Web Board]
Description
A tree (i.e. a connected graph without cycles) with vertices numbered by the integers 1, 2, ..., n is given. The "Prufer" code of such a tree is built as follows: the leaf (a vertex that is incident to only one edge) with the minimal number is taken. This leaf, together with its incident edge is removed from the graph, while the number of the vertex that was adjacent to the leaf is written down. In the new obtained tree, this procedure is repeated, until there is only one vertex left (which, by the way, always has number n). The written down sequence of n-1 numbers is called the Prufer code of the tree. Your task is, given a tree, to compute its Prufer code. The tree is denoted by a word of the language specified by the following grammar:
T ::= "(" N S ")"
S ::= " " T S | empty
N ::= number
That is, trees have parentheses around them, and a number denoting the identifier of the root vertex, followed by arbitrarily many (maybe none) subtrees separated by a single space character. As an example, take a look at the tree in the figure below which is denoted in the first line of the sample input. To generate further sample input, you may use your solution to Problem 2568. Note that, according to the definition given above, the root of a tree may be a leaf as well. It is only for the ease of denotation that we designate some vertex to be the root. Usually, what we are dealing here with is called an "unrooted tree".
Input
The input contains several test cases. Each test case specifies a tree as described above on one line of the input file. Input is terminated by EOF. You may assume that 1<=n<=50
Output
For each test case generate a single line containing the Prufer code of the specified tree. Separate numbers by a single space. Do not print any spaces at the end of the line.
Sample Input
(2 (6 (7)) (3) (5 (1) (4)) (8))
(1 (2 (3)))
Sample Output
5 2 5 2 6 2 8
2 3
HINT
Source
题解:
是一个叫什么Prufer树的东西,这个树有一定规律,就是每次找树枝节点最小的那个数的父节点,去掉这个枝;队友提供了思路,想了想敲了敲,就ac了,由于数字可以读取多位,错了几次;
代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<set>
#include<stack>
using namespace std;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define SL(x) scanf("%lld",&x)
#define PI(x) printf("%d",x)
#define PL(x) printf("%lld",x)
#define P_ printf(" ")
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
typedef long long LL;
const int MAXN=1010;
char s[2010];
int mp[MAXN][MAXN];
int usd[MAXN];
int ans[MAXN];
int vis[MAXN];
int main(){
stack<int>S;
int t;
while(mem(s,0),gets(s)){
int mx=0;
mem(usd,0);
int len=strlen(s),temp=0;
for(int i=0;i<len;i++){
if(s[i]=='(')temp=1;
if(isdigit(s[i])){
if(temp){
t=0;
while(isdigit(s[i]))t=t*10+s[i]-'0',i++;
mx=max(mx,t);
if(!S.empty())mp[t][S.top()]=mp[S.top()][t]=1,usd[S.top()]++,usd[t]++;
S.push(t);
temp=0;
}
}
if(s[i]==')')if(!S.empty())S.pop();
}
int k=0;
mem(vis,0);
int num=mx;
for(int i=1;i<=mx;i++)usd[i]--;
while(true){
temp=INF;
for(int i=1;i<=mx;i++)
if(!vis[i])if(!usd[i])temp=min(i,temp);
if(temp==INF)break;
num--;
for(int i=1;i<=mx;i++){
if(!vis[i]&&mp[temp][i]){
ans[k++]=i;break;
}
}
vis[temp]=1;
for(int i=1;i<=mx;i++)
if(!vis[i]&&mp[temp][i])mp[temp][i]=mp[i][temp]=0,usd[i]--;
}
for(int i=0;i<k;i++){
if(i)printf(" ");
PI(ans[i]);
}puts("");
}
return 0;
}
第七届河南省赛G.Code the Tree(拓扑排序+模拟)的更多相关文章
- 第七届河南省赛10403: D.山区修路(dp)
10403: D.山区修路 Time Limit: 2 Sec Memory Limit: 128 MB Submit: 69 Solved: 23 [Submit][Status][Web Bo ...
- 第七届河南省赛10402: C.机器人(扩展欧几里德)
10402: C.机器人 Time Limit: 2 Sec Memory Limit: 128 MB Submit: 53 Solved: 19 [Submit][Status][Web Boa ...
- 第七届河南省赛B.海岛争霸(并差集)
B.海岛争霸 Time Limit: 2 Sec Memory Limit: 128 MB Submit: 130 Solved: 48 [Submit][Status][Web Board] D ...
- 第七届河南省赛A.物资调度(dfs)
10401: A.物资调度 Time Limit: 2 Sec Memory Limit: 128 MB Submit: 95 Solved: 54 [Submit][Status][Web Bo ...
- 第七届河南省赛H.Rectangles(lis)
10396: H.Rectangles Time Limit: 2 Sec Memory Limit: 128 MB Submit: 229 Solved: 33 [Submit][Status] ...
- 第七届河南省赛F.Turing equation(模拟)
10399: F.Turing equation Time Limit: 1 Sec Memory Limit: 128 MB Submit: 151 Solved: 84 [Submit][St ...
- poj 2567 Code the Tree 河南第七届省赛
Code the Tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2350 Accepted: 906 Desc ...
- 第八届河南省赛G.Interference Signal(dp)
G.Interference Signal Time Limit: 2 Sec Memory Limit: 128 MB Submit: 35 Solved: 17 [Submit][Status ...
- 河南省第十届省赛 Plumbing the depth of lake (模拟)
title: Plumbing the depth of lake 河南省第十届省赛 题目描述: There is a mysterious lake in the north of Tibet. A ...
随机推荐
- Android API在不同版本系统上的兼容性
随着安卓版本的不断更新,新的API不断涌出,有时候高版本的API会在低版本crash的. 如果minSdkVersion设置过低,在build的时候,就会报错(Call requires API le ...
- placeholder 兼容 IE
placeholder 是 html5 的新属性,仅支持 html5 的浏览器才支持 placeholder,目前最新的 FF.Chrome.Safari.Opera 以及 IE10 都支持,IE6- ...
- STRUTS2核心控制器:FilterDispatcher
1. 在 struts1.x 系列中 , 所有的请求是通过一个 servlet(ActionServlet) 来管理控制的 , 在 Struts2.X 而是经过一个 Filter 来处理请求的. St ...
- 解决“无法连接到Python代码运行助手。请检查本机的设置”问题
廖雪峰老师python课程里有个代码运行助手,可以让你在线输入Python代码,然后通过本机运行的一个Python脚本来执行代码,很方便的一个脚本工具,但是很多人用过之后出现了这样的提示:“无法连接到 ...
- 游标-----内存中的一块区域,存放的是select 的结果
游标-----内存中的一块区域,存放的是select 的结果 游标用来处理从数据库中检索的多行记录(使用SELECT语句).利用游标,程序可以逐个地处理和遍历一次检索返回的整个记录集 ...
- USB OTG ID 检测原理
OTG 检测的原理是: USB OTG标准在完全兼容USB2.0标准的基础上,增添了电源管理(节省功耗)功能,它允许设备既可作为主机,也可作为外设操作(两用OTG).USB OTG技术可实现没有主机时 ...
- android中获取root权限的方法以及原理(转)
一. 概述 本文介绍了android中获取root权限的方法以及原理,让大家对android 玩家中常说的“越狱”有一个更深层次的认识. 二. Root 的介绍 1. Root 的目的 可以让我们拥有 ...
- Ubuntu下获取Nexus7的Root权限
一.准备 下载获取Root权限的工具包. 下载地址: http://downloadandroidrom.com/file/Nexus7/rooting/Nexus7Root.zip 二.解锁 Ubu ...
- python setattr(),getattr()函数
setattr(object,name,value): 作用:设置object的名称为name(type:string)的属性的属性值为value,属性name可以是已存在属性也可以是新属性. get ...
- S3C6410嵌入式应用平台构建(二)
[2014-4/11~4/14]经过之前的实验,对Uboot已经有了大体的了解,前我们已经把led灯给点亮,但这不是我们的根本目的,我们是要进入boot启动,经过两天的分析代码和反复的实验,终于可以进 ...