题目链接:uva 10410 - Tree Reconstruction
题目大意:给定一个树的BFS和DFS,求这棵树。
解题思路:用栈维护即可。对应BFS序列映射出了每个节点和根节点的距离,遍历dfs序列,对当前节点和栈顶节点比较,如果该节点距离根节点更远,则说明该节点为栈顶节点个孩子节点,则记录后将节点放入栈中。否则弹掉栈顶元素继续比较。需要注意一点,即当元素与栈顶元素的距离值大1的时候要视为相等,因为它们属于兄弟节点
#include <cstdio>
#include <cstring>
#include <stack>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn = 1005;
int N, pos[maxn];
vector<int> g[maxn];
int main () {
while (scanf("%d", &N) == 1) {
int x;
for (int i = 1; i <= N; i++) {
scanf("%d", &x);
g[i].clear();
pos[x] = i;
}
int root;
stack<int> sta;
scanf("%d", &root);
sta.push(root);
for (int i = 1; i < N; i++) {
scanf("%d", &x);
while (true) {
int u = sta.top();
if (u == root || pos[u] + 1 < pos[x]) {
g[u].push_back(x);
sta.push(x);
break;
} else
sta.pop();
}
}
for (int i = 1; i <= N; i++) {
printf("%d:", i);
for (int j = 0; j < g[i].size(); j++)
printf(" %d", g[i][j]);
printf("\n");
}
}
return 0;
}
分享到:
相关推荐
Research on Three-dimensional Reconstruction of the Liver CT Images.pdf
matlab-Data reconstruction and analysis tools for tomography data acquired at the P05 Imaging Beamline (IBL) and the P07 High-Energy Material Science (HEMS) beamline at PETRA III at DESY, both ...
Image-Reconstruction.zip
computed tomography algorithm for image reconstraction
CT-Image-Reconstruction-master源码.zip
Image-Reconstruction的Matlab代码
这是关于图像重建的电子书,高清,最新版本,经典著作,英文版
基于M线的3DCT图像重建文章,可用于感兴趣区域的图像重建!
基于压缩感知的非均匀空间构型分布式SAR动目标三维成像信号重构研究,刘梅,张雷,本文主要研究了一种基于压缩感知的分布式SAR动目标三维成像方法。推导了动目标三维稀疏信号模型,并建立了动目标三维信号重构新方
Ultrasound Imaging Based on the Improved CNN MethodZixia Zhou1, Yuanyuan Wang1,2
(2012)Covariance propagation and next best view planning for 3D reconstruction.pdf
This is 3D reconstruction algorithm
一种能量优化的传感器网络移动单点信号重建算法,赵明,,在信号处理领域,通过数据采样来估计原始信号的一组参数值是一个经典问题。现有对移动信号源的估计算法大都没有综合考虑采样之间
MVS最具权威的综述性文章,详细讲解了近几年MVS的发展
在这个包中,我们实现了标准 PCA、核 PCA 和高斯核 PCA 的前像重建。 我们还提供了三个演示:(1)两个同心球嵌入; (2)PCA/kPCA人脸分类; (3) 使用 kPCA 的主动形状模型。 标准 PCA 并未针对非常高维的数据...
Advances in photometric 3D-Reconstruction,2020年
3D-reconstruction-matlab.zip
基于机器学习用于图像去雾雾霾的算法,matlab编程,比较实用
Abstract—Three-dimensional reconstruction is an importan- t research direction of computer vision. The existing three- dimensional reconstruction method who based on the poor convenience and limited ...
An Efficient I/O-Redirection-Based Reconstruction Scheme for Erasure-Coded Storage Clusters