Code the Tree
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 2292 | Accepted: 878 |
Description
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
Output

Sample Input
(2 (6 (7)) (3) (5 (1) (4)) (8))
(1 (2 (3)))
(6 (1 (4)) (2 (3) (5)))
Sample Output
5 2 5 2 6 2 8
2 3
2 1 6 2 6
难点是 字符串的分离, (A)根据这个特点,当读到‘('时,下一个一定是数字,我们遇到’)‘就结束。见代码
#include"iostream"
#include"cstdio"
#include"map"
#include"queue"
#include"vector"
#include"set"
#include"cstring"
#include"algorithm"
using namespace std;
void solve(vector< set<int> > &v,int p=)
{
int x;
cin>>x;
if(p)
{
v[x].insert(p);
v[p].insert(x);
}
while()
{
char ch;
cin>>ch;
if(ch==')')
break;
solve(v,x);
}
return ;
}
int main()
{
int i,j,n,k;
char ch;
while(cin>>ch)
{
vector< set<int> > vec(,set<int>());
priority_queue<int,vector<int>,greater<int> > que;
n=;
solve(vec);
for(i=;i<vec.size();i++)
{
if(vec[i].size())
{
n++;
if(vec[i].size()==)
que.push(i);
}
}
for(i=;i<n;i++)
{
int t=que.top();
que.pop();
int p=*vec[t].begin();
if(i>)
printf(" ");
printf("%d",p);
vec[p].erase(t);
if(vec[p].size()==)
que.push(p);
}
printf("\n");
}
return ;
}
#include"iostream"
#include"cstdio"
#include"map"
#include"queue"
#include"vector"
#include"set"
#include"cstring"
#include"algorithm"
using namespace std;
void solve(map<int,set<int> > &m,int p=)
{
int x;
cin>>x;
if(p)
{
m[p].insert(x);
m[x].insert(p);
}
while()
{
char ch;
cin>>ch;
if(ch==')')
break;
solve(m,x);
}
return ;
}
int main()
{
int i,j,n,k;
char ch;
while(cin>>ch)
{
priority_queue<int,vector<int>,greater<int> > que;
map<int,set<int> > mp;
solve(mp);
for(i=;i<=mp.size();i++)
{
if(mp[i].size()==)
{
que.push(i);
}
}
for(i=;i<mp.size();i++)
{
int t=que.top();
que.pop();
int p=*mp[t].begin();
if(i>)
printf(" ");
printf("%d",p);
mp[p].erase(t);
if(mp[p].size()==)
que.push(p);
}
printf("\n");
}
return ;
}
Code the Tree的更多相关文章
- Code the Tree(图论,树)
ZOJ Problem Set - 1097 Code the Tree Time Limit: 2 Seconds Memory Limit: 65536 KB A tree (i.e. ...
- poj 2567 Code the Tree 河南第七届省赛
Code the Tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2350 Accepted: 906 Desc ...
- POJ Code the Tree 树的pufer编号
Code the Tree Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2259 Accepted: 859 Desc ...
- 第七届河南省赛G.Code the Tree(拓扑排序+模拟)
G.Code the Tree Time Limit: 2 Sec Memory Limit: 128 MB Submit: 35 Solved: 18 [Submit][Status][Web ...
- #Leet Code# Same Tree
语言:Python 描述:使用递归实现 # Definition for a binary tree node # class TreeNode: # def __init__(self, x): # ...
- POJ 2567 Code the Tree & POJ 2568 Decode the Tree Prufer序列
题目大意:2567是给出一棵树,让你求出它的Prufer序列.2568时给出一个Prufer序列,求出这个树. 思路:首先要知道Prufer序列.对于随意一个无根树,每次去掉一个编号最小的叶子节点,并 ...
- #Leet Code# Binary Tree Max[待精简]
描述:递归调用,getMax返回 [节点值,经过节点左子节点的最大值,经过节点右节点的最大值],每次递归同时查看是否存在不经过节点的值大于max. 代码:待优化 def getLargeNode(se ...
- #Leet Code# Unique Tree
语言:Python 描述:使用递归实现 class Solution: # @return an integer def numTrees(self, n): : elif n == : else: ...
- 20162314 Experiment 2 - Tree
Experiment report of Besti course:<Program Design & Data Structures> Class: 1623 Student N ...
随机推荐
- HDU 1707
思路:标记课程表上的课程,询问时遍历课程表,再以字典序输出名字. #include<iostream> #include<stdio.h> #include<stdlib ...
- C# 颜色转换
十六进制颜色与Color对象的互相转换[C#] C#十六进制颜色与Color对象的互相转换 把十六进制颜色转化为color对象ColorTranslator.FromHtml("#FF0 ...
- Nodejs与Net 和SQL 交互利器Edge.js
https://github.com/tjanczuk http://www.cnblogs.com/joylee/archive/2013/02/05/msnodesql.html edge.js这 ...
- iomanip.h
http://baike.baidu.com/link?url=zuNLgcUVylhUYYefyV13F38NChIMx8nnCEWV5zkkTQMrrSdKPxUERZuydSHtp6sXukWv ...
- AngularJS 学习笔记二
AngularJS指令 指令 描述 讲解 ng_app 定义应用程序的根元素. 指令 ng_bind 绑定 HTML 元素到应用程序数据. 简介 ng_click 定义元素被单击时的行为. HTML ...
- 2015年必火的五个Html5移动开发工具推荐
NO.1 DCloudHBuilder:基于HTML5开发工具 中文官网:http://www.dcloud.io/ DCloudHBuilder:基于HTML5开发工具是当前最快的HTML开发工 ...
- C#学习笔记(十五):预处理指令
C#和C/C++一样,也支持预处理指令,下面我们来看看C#中的预处理指令. #region 代码折叠功能,配合#endregion使用,如下: 点击后如下: 条件预处理 条件预处理可以根据给出的条件决 ...
- JS 命名空间 实现方式 收集
一. 对象注册式 // 声明一个全局对象Namespace,用来注册命名空间Namespace = new Object(); // 全局对象仅仅存在register函数,参数为名称空间全路径,如&q ...
- php 处理高并发的思路
1.nginx 服务器,提高网站服务器并发性能 2.控制大文件的下载,减少CPU的消耗. 3.对于sql查询做缓存. 4.静态页面文件缓存. 5.CND缓存静态文件, 6.反向代理到多个服务器,用来分 ...
- Spring声明式事务的配置~~~
/*2011年8月28日 10:03:30 by Rush */ 环境配置 项目使用SSH架构,现在要添加Spring事务管理功能,针对当前环境,只需要添加Spring 2.0 AOP类库即可.添加 ...