题意:给你一个字符串表示一棵无根树。让你求出该树的purfer序列。

解法:stl好题!(vector+set+stack+priority_queue)

#include<iostream>
#include<string.h>
#include<set>
#include<queue>
#include<vector>
#include<stack>
#include<algorithm>
#include<stdio.h>
using namespace std;
char str[300];
struct cmp
{bool operator ()(const int a,const int b)const{return a>b;}
};
int init(vector<set<int> >&pq)
{int num,n,x,y,len=strlen(str),i=0;stack<int>qp;while(i<len-1){if(str[i]=='('){sscanf(str+i+1,"%d%n",&num,&n);qp.push(num);i+=n+1;}else if(str[i]==')'){y=qp.top(),qp.pop();x=qp.top();pq[y].insert(x);pq[x].insert(y);i++;}else if(str[i]==' ')i++;}return 0;
}
int main()
{while(gets(str)){vector<set<int> >pq(105,set<int>());priority_queue<int,vector<int>,cmp>yz;init(pq);for(int i=1;pq[i].size();i++)if(pq[i].size()==1)yz.push(i);int f=0;while(yz.size()>1){int x=yz.top(),y;yz.pop();y=*(pq[x].begin());pq[x].erase(y);pq[y].erase(x);if(pq[y].size()==1)yz.push(y);if(f)cout<<" ";cout<<y;f=1;}cout<<"\n";}return 0;
}

poj 2567 code thr tree相关推荐

  1. POJ 2054 Color a Tree (贪心)

    $ POJ~2054~Color~a~Tree $ $ solution: $ 我们先从题中抽取信息,因为每个点的费用和染色的次数有关,所以我们可以很自然的想到先给权值大的节点染色.但是题目还说每个节 ...

  2. poj 3013 Big Christmas Tree(最短路变形)

    传送门:POJ 3013 Big Christmas Tree 描述: Big Christmas Tree Time Limit: 3000MS   Memory Limit: 131072K To ...

  3. POJ 2054 Color a Tree解题报告

    题干 Bob is very interested in the data structure of a tree. A tree is a directed graph in which a spe ...

  4. 【POJ - 2486】Apple Tree (树形背包,dp)

    题干: Wshxzt is a lovely girl. She likes apple very much. One day HX takes her to an apple tree. There ...

  5. 【POJ - 3321】 Apple Tree(dfs序 + 线段树维护 或 dfs序 + 树状数组维护)

    题干: There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the t ...

  6. POJ 2054 Color a Tree

    贪心....                    Color a Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions:  ...

  7. 【poj 3321】Apple Tree 题意&题解&代码(C++)

    题目链接: http://poj.org/problem?id=3321 题意: 给你一颗n个节点的树,每个节点开始有一个苹果,然后m次修改,每次修改使得某个节点的苹果改变,有变成没有,没有变成有.询 ...

  8. 【POJ 3321】Apple Tree(树的dfs序+树状数组)

    传送门 Solution: 我们只需要采用和树链剖分近似的思想--把整个树的dfs序整理出来,排成线型. 这样一个节点的子树肯定是连续的一段,于是乎就可以用树状数组维护单点修改+区间查询的任务了. # ...

  9. POJ 1850 Code(组合数学)

    题目链接 这个题目的组合解法,太巧妙了.长度为n的方案数总和为C(26,n),我竟没有发现..然后+长度和字符串相等的时候的情况,第一位默认,以后默认为前一位+1,这样才能保证递增特性. 网上还有DP ...

  10. POJ 1850 Code

    //这题和上题没什么区别// 就是数据大了点而已// 直接参考上题 http://www.cnblogs.com/372465774y/p/3604095.html#include <iostr ...

最新文章

  1. 2022-2028年中国分离膜外壳行业市场调查研究及投资前景预测报告
  2. 软件中的易用性设计及测试(三)之实践
  3. RabbitMQ OS X下安装及常用命令-1
  4. centernet2 win10使用笔记
  5. android 单选、多选弹出菜单
  6. Angular @HostListener 装饰器的使用笔记
  7. engagement center里如何获得Access Token
  8. Springboot 通过Ftp协议下载文件,并在Vue平台上显示其内容
  9. Http的三次握手与四次挥手的流程:SYN包--同步包 ACK包--应答包 FIN包--终止包
  10. 计算机网络设置端口转发,怎么设置路由器端口转发功能?
  11. 图像处理---LoMo
  12. Linux RAID磁盘阵列配置
  13. js bind 传参、_Node.js 在微医的应用场景及实践
  14. 用 Python 写软件原来这么简单!
  15. Jude - 体验UML高速建模
  16. 1.0 如何使用cubemx并且移植RTX操作系统?(方法一)
  17. 广州金蝶财务软件|番禺金蝶财务软件|金蝶迷你版特价优惠中
  18. 15 Rest高级客户端实践(一):文档索引
  19. windows 系统扩容C盘注意事项
  20. PPT:烟草行业互联网智能工厂

热门文章

  1. 手撕力扣之贪心法:买卖股票的最佳时机 II、最大子序和、跳跃游戏、跳跃游戏 II、分发饼干、划分字母区间、重构字符串、分发糖果、种花问题、加油站
  2. windows定时开关机方法
  3. python计算贷款购房月供、利息
  4. python字符串转负数_python 字符串 步进 负数
  5. java feature envy_《重构-改善既有代码的设计 第3章代码的坏味道》学习笔记
  6. python爬虫之数据提取、正则表达式、xml、XPath、etree、CSS选择器 BeautifulSoup4
  7. 为什么用交叉线而不是直通线连接相同的设备
  8. [Ember Zigbee]zigbee应用程序RAM
  9. K8S实战进阶篇:一文带你深入了解K8S持久化存储解决方案
  10. [BZOJ4200][NOI2015]小园丁与老司机