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 ...
随机推荐
- android string[] arraylist<string>互转
string[] to arraylist<string>:eg:string[] str; arraylist<string> arr; for(int i=0;i<s ...
- [Hive - LanguageManual] VirtualColumns
Virtual Columns Simple Examples Virtual Columns Hive 0.8.0 provides support for two virtual columns: ...
- Navicate
快捷键 1.ctrl+q 打开查询窗口 2.ctrl+/ 注释sql语句 3.ctrl+shift +/ 解除注释 4.ctrl+r 运行查询窗口的sql语句 5.ctrl+shift+r 只运行选中 ...
- 第三百零六天 how can I 坚持
今天做了件并不是我风格的事,送了张公交卡,还没送出去,好难搞啊.这天会铭记的.如果将来我们能走在一起. 中午去朝阳门拿了我的荣事达破壁机,好大啊,怎么带回家啊,还有,回家要不要买两只烤鸭啊. 今天聊了 ...
- Spark SQL概念学习系列之如何使用 Spark SQL(六)
val sqlContext = new org.apache.spark.sql.SQLContext(sc) // 在这里引入 sqlContext 下所有的方法就可以直接用 sql 方法进行查询 ...
- Spark RDD概念学习系列之Spark的算子的作用(十四)
Spark的算子的作用 首先,关于spark算子的分类,详细见 http://www.cnblogs.com/zlslch/p/5723857.html 1.Transformation 变换/转换算 ...
- 【转】你真的了解iOS代理设计模式吗?
转自:http://www.cocoachina.com/ios/20160317/15696.html 在项目中我们经常会用到代理的设计模式,这是iOS中一种消息传递的方式,也可以通过这种方式来传递 ...
- JSF 2 dropdown box example
In JSF, <h:selectOneMenu /> tag is used to render a dropdown box – HTML select element with &q ...
- JSF 2.0 + Ajax hello world example
In JSF 2.0, coding Ajax is just like coding a normal HTML tag, it's extremely easy. In this tutorial ...
- [iOS UI进阶 - 5.0] 手势解锁Demo
A.需求 1.九宫格手势解锁 2.使用了绘图和手势事件 code source: https://github.com/hellovoidworld/GestureUnlockDemo B ...