zoj-1097-code the tree
来源:互联网 发布:淘宝u站推广 编辑:程序博客网 时间:2024/06/06 04:49
参考题解:http://www.cnblogs.com/pcoda/archive/2012/09/02/2667930.html
Code
这是一道解关于 无根树 的问题, 算法是将 叶节点 送入 优先队列, 然后从小到大遍历 叶节点, 可以用STL很方便地解决。
//program: code the tree// author: slowlight93// date: 2013-08-08#include<cstdio>#include<iostream>#include<stack>#include<vector>#include<queue>#include<set>#include<cstring>#include<cstdlib>using namespace std;char str[300];void init( vector< set<int> > & adj ){//the value of a verticeint value;//number of charactersint chNum;int pos = 0;int len = strlen(str);//fatherint y;//sonint x;//stackstack<int> vertices;while( pos < len - 1 ){if( str[pos] == '(' ){sscanf(str + pos + 1, "%d%n", &value, &chNum);pos += chNum + 1;vertices.push(value);}else if( str[pos] == ')' ){x = vertices.top();vertices.pop();y = vertices.top();adj[x].insert(y);adj[y].insert(x);pos++;}elsepos++;}}int main(){freopen("input.in", "r", stdin);freopen("output.out","w", stdout);while( gets( str ) ){//the number of verticesint sum = 0;//the fatherint y;//the sonint x;vector < set<int> > adj (100, set<int>());priority_queue<int,vector<int>,greater<int> >leafs;init(adj);for(int i=1;adj[i].size();i++){sum++;if(adj[i].size() == 1)leafs.push(i);}for(int i=1;i<sum;i++){x = leafs.top();leafs.pop();y = *(adj[x].begin());adj[x].erase(y);adj[y].erase(x);if( adj[y].size() == 1)leafs.push(y);if( i > 1 )cout << " ";cout << y;}cout << endl;}return 0;}
- ZOJ 1097 CODE the Tree
- zoj-1097-code the tree
- zoj 1097 Code the Tree(记忆化深搜构造树)
- Code the Tree
- G.Code the Tree
- zoj 1087 Cracking the Code
- poj 2567Code the Tree
- POJ 2567 Code the Tree
- POJ--2567--Code the Tree
- zoj 3506 Cut the Tree (树形dp)
- ZOJ 3863Paths on the Tree
- cracking the code interview balanced tree python
- Code the Tree(记忆化深搜构造树)
- ZOJ 3863 Paths on the Tree 树分治
- ZOJ 3863 Paths on the Tree 树分治
- poj 2567 Code the Tree 【还原树 求prufer序列】
- nyoj1254 Code the Tree (第七届河南省程序设计大赛)
- NYOJ Code the Tree(河南省第七届省赛题目)
- Android开发 Http访问被保护的网络资源
- RegionServer不停新建空的hlog
- Mahout K-means聚类
- hdu 1241 Oil Deposits
- Android优秀开源项目
- zoj-1097-code the tree
- ubuntu下安装Tomcat6
- 将一个值的二进制模式逆序结果输出
- hibernate防止sql注入
- PHP文件上传大小限制修改
- Android命令行工具logcat详细用法!
- IOS 知识收集
- “C++的数组不支持多态”?
- ARM伪指令