题目大意:2567是给出一棵树,让你求出它的Prufer序列。2568时给出一个Prufer序列,求出这个树。

思路:首先要知道Prufer序列。对于随意一个无根树,每次去掉一个编号最小的叶子节点,并保存这个节点所连接的节点所得到的序列就是这棵树的Prufer序列。

这个序列有十分优雅的性质。它能与无根树一一相应。因此。两个标号一样的无根树得到的Prufer序列也一定是一样的。

此外,设一个节点的度数是d[i],那么他会在Prufer序列中出现d[i] - 1次。

2567:记录每个节点的度。依照Prufer序列的定义。最后输出队列中剩余的元素。

2568:由Prufer序列可以求出每一个点的度。把度数为1的点增加到队列中去。每次找一个队列中编号最小的。与当前Prufer序列中的数字连一条边,然后降低对应的度数。

CODE(2567):

#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAX 110
using namespace std; priority_queue<int,vector<int>,greater<int> > q; int points;
int degree[MAX];
int head[MAX],total;
int next[MAX << 1],aim[MAX << 1]; int stack[MAX],top;
bool v[MAX]; inline void Initialize()
{
points = total = top = 0;
memset(degree,0,sizeof(degree));
memset(v,false,sizeof(v));
memset(head,0,sizeof(head));
} inline void Add(int x,int y)
{
next[++total] = head[x];
aim[total] = y;
head[x] = total;
} inline char GetChar()
{
char c;
do c = getchar(); while(c == ' ' || c == '\n' || c == '\r');
return c;
} inline void TopSort()
{
for(int i = 1; i <= points; ++i)
if(degree[i] == 1)
q.push(i),v[i] = true;
for(int i = 1; i <= points - 2; ++i) {
int x = q.top(); q.pop();
for(int j = head[x]; j; j = next[j]) {
if(v[aim[j]]) continue;
--degree[aim[j]];
printf("%d ",aim[j]);
if(degree[aim[j]] == 1) {
v[aim[j]] = true;
q.push(aim[j]);
}
}
}
q.pop();
printf("%d\n",q.top());
q.pop();
} int main()
{
char c;
while(GetChar() != EOF) {
Initialize();
scanf("%d",&stack[++top]);
points = stack[top];
while(top) {
c = GetChar();
if(c == '(') {
int temp;
scanf("%d",&temp);
points = max(points,temp);
Add(stack[top],temp);
Add(temp,stack[top]);
++degree[stack[top]];
++degree[temp];
stack[++top] = temp;
}
else top--;
}
if(points == 1) {
puts("");
continue;
}
TopSort();
}
return 0;
}

CODE(2568):

#include <queue>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAX 1010
using namespace std; char s[MAX];
int src[MAX],cnt;
int degree[MAX];
int head[MAX],total;
int next[MAX],aim[MAX];
int points; inline void Initialize()
{
total = cnt = points = 0;
memset(degree,0,sizeof(degree));
memset(head,0,sizeof(head));
} inline void Add(int x,int y)
{
next[++total] = head[x];
aim[total] = y;
head[x] = total;
} inline void Decode()
{
static priority_queue<int,vector<int>,greater<int> > q;
while(!q.empty()) q.pop();
for(int i = 1; i <= points; ++i)
if(!degree[i]) q.push(i);
for(int i = 1; i <= cnt; ++i) {
int x = src[i];
int y = q.top(); q.pop();
Add(x,y); Add(y,x);
if(!--degree[x]) q.push(x);
}
//int x = q.top(); q.pop();
//int y = q.top(); q.pop();
//Add(x,y),Add(y,x);
} inline void OutPut(int x,int last)
{
if(x != 1) putchar(' ');
putchar('(');
printf("%d",x);
for(int i = head[x]; i; i = next[i]) {
if(aim[i] == last) continue;
OutPut(aim[i],x);
}
putchar(')');
} int main()
{
while(gets(s) != NULL) {
Initialize();
char *p = s;
while(*p != '\0') {
sscanf(p,"%d",&src[++cnt]);
points = max(points,src[cnt]);
++p,++p;
if(src[cnt] > 9) ++p;
}
memset(s,'\0',sizeof(s));
for(int i = 1; i <= cnt; ++i)
++degree[src[i]];
Decode();
OutPut(1,0);
puts("");
}
return 0;
}

POJ 2567 Code the Tree &amp; POJ 2568 Decode the Tree Prufer序列的更多相关文章

  1. poj 2567 Code the Tree 河南第七届省赛

    Code the Tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2350   Accepted: 906 Desc ...

  2. Tree Recovery POJ - 2255

    Tree Recovery POJ - 2255 根据树的前序遍历和中序遍历还原后序遍历. (偷懒用了stl的find) #include<iostream> #include<st ...

  3. ※数据结构※→☆非线性结构(tree)☆============二叉树 顺序存储结构(tree binary sequence)(十九)

    二叉树 在计算机科学中,二叉树是每个结点最多有两个子树的有序树.通常子树的根被称作“左子树”(left subtree)和“右子树”(right subtree).二叉树常被用作二叉查找树和二叉堆或是 ...

  4. [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列

    Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...

  5. 33. Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree

    Minimum Depth of Binary Tree OJ: https://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ Give ...

  6. [LeetCode] 255. Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列

    Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...

  7. [leetcode]1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree

    [leetcode]1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree 链接 leetcode 描述    ...

  8. POJ 2568/ZOJ 1965 Decode the Tree

    题意:在树中,每次删去节点值最小的叶子结点. 每删去一个点,就给出与这相连的点的值,直到最后只剩下一个根结点,给这N-1个数,重新建立这个树. 思路: 给出的节点号按次序存入到数组a中,将未给出的数存 ...

  9. [欧拉回路+手动开栈] poj 1780 Code

    题目链接: http://poj.org/problem? id=1780 Code Time Limit: 1000MS   Memory Limit: 65536K Total Submissio ...

随机推荐

  1. 阿里云rds linux平台使用wget 工具下载备份与日志文件

    1. 获取备份下载地址 RDS 控制台  备份恢复  数据备份,选择需要下载的备份集,点击“下载”. 点击“复制内网地址” 或 “复制外网地址” 来获取备份的 内网 或 外网 下载地址. 日志备份的地 ...

  2. brew install memcache get Error: Formulae found in multiple taps

    本篇文章由:http://xinpure.com/brew-install-memcache-get-error-formulae-found-in-multiple-taps/ 安装环境: Mac ...

  3. Nginx+Windows负载均衡(转载)

    一.下载Nginxhttp://nginx.org/download/nginx-1.0.8.zip解压到C:\nginx目录下二.在两台服务器上分别建一个网站:S1:192.168.16.35:80 ...

  4. Linux 指令篇:磁盘管理--tree

    Linux 指令篇:磁盘管理--tree 功能说明:以树状图列出目录的内容. 语 法:tree [-aACdDfFgilnNpqstux][-I <范本样式>][-P <范本样式&g ...

  5. AngularJS实现鼠标右键事件

    常规javascript鼠标右键直接在标签上加contextmenu="alert('a')"即可,现在angular通过directive来定义一个右键指令. app.direc ...

  6. max_int和-1

    #include <stdio.h> int main(int argc, char *argv[]) { unsigned ; ) printf("umax:%u == -1\ ...

  7. ubuntu apt-get方式安装与卸载

    在ubuntu终端中安装软件: 安装软件 apt-get install softname1 softname2 softname3……卸载软件 apt-get remove softname1 so ...

  8. lzma解压

    这个软件的使用方法有点特殊:需要将要压缩为lzma格式的文件拖放到批处理上面,会自动进行处理.压缩和解压同样是拖放到上面,程序会自动处理.程序默认使用2个CPU线程进行处理,会自动判断你是压缩还是解压 ...

  9. C++链接ODBC数据源:VS2013,Access

    参考资料:1.http://wenku.baidu.com/view/a92d1a812cc58bd63186bd8d.html 2.http://blog.sina.com.cn/s/blog_68 ...

  10. 初识STM32固件库

    因为基于 Cortex 系列芯片采用的内核都是相同的,区别主要为核外的片上外设的差异,这些差异却导致软件在同内核,不同外设的芯片上移植困难.为了解决不同的芯片厂商生产的 Cortex 微控制器软件 的 ...