zoj 1097 普吕弗序列
题目大意:输入一颗无根树的括号序列,求这棵树的普吕弗序列。
分析思路:
1)普吕弗序列,可以参考维基百科,其做法是找出树中编号最小的叶子节点,并将此叶子节点及边删除,并输出其邻接的节点标号;
2)递归地构造树,可以使用list<int> 数组来表示一个“邻接表”,以存储构造的树;
3)使用优先队列来进行删除,奈何priority_queue没有迭代器访问,只能用堆排序,取最值;
代码:
#include<iostream>
#include<vector>
#include<map>
#include<queue>
#include<string>
#include<algorithm>
#include<fstream>
#include<list>
using namespace std; struct nodeAndDegree
{
int degree; //度
int nodeNumber; //结点编号
bool operator < (const nodeAndDegree& n1)const
{
return degree == n1.degree ? (nodeNumber > n1.nodeNumber) : (degree > n1.degree);
}
}; const int MAX_LEN = 55;
list<int> vGraph[MAX_LEN];
vector<int> v;
int rootNumber = 0; void dfs(int start, int end, int parent, string str)
{
if (start == end) //只有单个点
{
return;
} //放入邻接矩阵
int currentNode = 0;
for (int i = start + 1; i <= end - 1; i++)
{
if (str[i] == ' ' || str[i] == '\0' || str[i] == '(')
{
break;
}
currentNode = currentNode * 10 + (int)(str[i] - 48);
} //放入邻接矩阵
vGraph[parent].push_back(currentNode);
vGraph[currentNode].push_back(parent);
v.push_back(currentNode); int mark = 0;
int tmpStart = -1;
for (int i = start + 1; i <= end - 1; i++)
{
if (str[i] == '(')
{
mark++;
if (tmpStart == -1) tmpStart = i;
continue;
} if (str[i] == ')')
{
mark--;
if (mark == 0)
{
dfs(tmpStart, i, currentNode, str);
tmpStart = -1;
}
}
}
} void print_prufer_sequnce()
{
//首先修改根节点对应的长度
int tmp = vGraph[0].front();
vGraph[tmp].remove(0); vector<nodeAndDegree> listNodeDegree;
for (int i = 0; i < v.size(); i++)
{
nodeAndDegree *nd = new nodeAndDegree();
nd->nodeNumber = v[i];
nd->degree = vGraph[v[i]].size(); listNodeDegree.push_back(*nd);
} int n = v.size() - 1;
int index = 0; while (index < n)
{
make_heap(listNodeDegree.begin(), listNodeDegree.end());
int number = listNodeDegree[0].nodeNumber; //当前结点
int front = vGraph[number].front(); cout << front ;
if (index != n - 1)
{
cout << " ";
}
vGraph[front].remove(number);
vGraph[number].remove(front);
for (int j = 1; j < listNodeDegree.size(); j++)
{
if (listNodeDegree[j].nodeNumber == front)
{
listNodeDegree[j].degree--;
break;
}
} listNodeDegree.erase(listNodeDegree.begin()); index++;
}
} int main()
{
string s;
//fstream cin("1097.txt");
while (getline(cin, s))
{
for (int i = 0; i < MAX_LEN; i++)
{
vGraph[i].clear();
}
v.clear();
dfs(0, s.size() - 1, 0, s);
print_prufer_sequnce();
cout << endl;
}
return 0;
}
以纪念我那逝去的耗费精力的兴奋的AC。
zoj 1097 普吕弗序列的更多相关文章
- 线性时间构造普吕弗(Prüfer)序列
tree -> sequence 首先预处理数组 deg[N], deg[i]表示编号为i的节点的度数,我们每次要删除的节点肯定是 满足deg[i]=1 的编号最小节点, 首先找到所有叶子并选出 ...
- 计蒜客NOIP模拟赛(2) D2T1 劫富济贫
[问题描述] 吕弗·普自小从英国长大,受到骑士精神的影响,吕弗·普的梦想便是成为一位劫富济贫的骑士. 吕弗·普拿到了一份全国富豪的名单(不在名单上的都是穷人),上面写着所有富豪的名字以及他们的总资产, ...
- hdu 1394 zoj 1484 求旋转序列的逆序数(并归排序)
题意:给出一序列,你可以循环移动它(就是把后面的一段移动到前面),问可以移动的并产生的最小逆序数. 求逆序可以用并归排序,复杂度为O(nlogn),但是如果每移动一次就求一次的话肯定会超时,网上题解都 ...
- 无向图的完美消除序列 判断弦图 ZOJ 1015 Fish net
ZOJ1015 题意简述:给定一个无向图,判断是否存在一个长度大于3的环路,且其上没有弦(连接环上不同两点的边且不在环上). 命题等价于该图是否存在完美消除序列. 所谓完美消除序列:在 vi,v ...
- ZOJ 3963 Heap Partition set维护。给一个序列,将其划分成尽量少的序列,使每一个序列满足按照顺序构造二叉树,父母的值<=孩子的值。
Heap Partition Time Limit: Seconds Memory Limit: KB Special Judge A sequence S = {s1, s2, ..., sn} i ...
- ZOJ 3795 Grouping 求最长链序列露点拓扑
意甲冠军:特定n积分.m向边条. 该点被划分成多个集合随机的每个集合,使得2问题的关键是无法访问(集合只能容纳一个点) 问至少需要被分成几个集合. 假设没有戒指,接着这个话题正在寻求产业链最长的一个有 ...
- (队列的应用5.3.1)ZOJ 3210 A Stack or A Queue?根据进入结构的序列和离开结构的序列确定是stack还是queue)
/* * ZOJ_3210.cpp * * Created on: 2013年10月30日 * Author: Administrator */ #include <iostream> # ...
- ZOJ 2319 Beatuiful People(单调递增序列的变形)
Beautiful People Time Limit: 5 Seconds Memory Limit: 32768 KB Special Judge The most prest ...
- 弗拉特利定律:Illumina怎样缔造基因革命
蕾妮·瓦林特(Renee Valint)的女儿谢尔碧(Shelby)在2000年出生时.看起来虚弱无力,就如同一仅仅耷拉着的布娃娃.谢尔碧学着走路和说话,但学得很慢.错过了儿童发展的重要阶段.到4岁时 ...
随机推荐
- (7)UI(基础对象)
1.地图控件: 地图控件可支持导入Tiled地图编辑器导出的tmx格式文件,地图是制作游戏关卡地形图的控件,对于即时战略类型游戏的制作,为了使玩家流畅的切换游戏画面,经常会使用超过屏幕尺寸的地图. ...
- hiho一下 第九十四周 数论三·约瑟夫问题
数论三·约瑟夫问题 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho的班级正在进行班长的选举,他们决定通过一种特殊的方式来选择班长. 首先N个候选人围成一个 ...
- Nested List Weight Sum I & II
Nested List Weight Sum I Given a nested list of integers, return the sum of all integers in the list ...
- php开发网站编码统一问题
一个良好的网站代码整洁,注释适当是最基本的,也是好的习惯,这可以避免以后的非常乱了自己感觉都乱,一旦重构麻烦就大了耗时耗力,其中网站整个体系的编码是最重要的一个方面,为了网站的稳定性建议php程序,H ...
- codeforces 479B Towers 解题报告
题目链接:http://codeforces.com/problemset/problem/479/B 题目意思:有 n 座塔,第 i 座塔有 ai 个cubes在上面.规定每一次操作是从最多 cub ...
- poj 1007 DNA Sorting 解题报告
题目链接:http://poj.org/problem?id=1007 本题属于字符串排序问题.思路很简单,把每行的字符串和该行字符串统计出的字母逆序的总和看成一个结构体.最后把全部行按照这个总和从小 ...
- springJDBC一对多关系,以及Java递归,jsp递归的实现
maven编译,springMVC+spring+springJDBC框架. 要实现的功能是一个文件夹下,可能显示n个文件夹,每个文件夹下又可能显示n个文件夹.... 前台效果:
- WebStorm快捷键操作
1. ctrl + shift + n: 打开工程中的文件,目的是打开当前工程下任意目录的文件. 2. ctrl + j: 输出模板 3. ctrl + b: 跳到变量申明处 4. ctrl + al ...
- vs c++ 将string转换为double
可以用atof()这个函数,但是这个函数的参数是char*类型的,因此需将string类型强制转换,方法为在函数的参数中写成 const_cast<const char *>(str.c_ ...
- CityGML文件格式
1 LOD3中,wall是由cuboid组成的,一个墙面包括8个面,分为wall-1, wall-2...wall-8,door也是,因此他们都是multisurface (一般由8个面片组成). 在 ...