UVa 10410 - Tree Reconstruction(树/dfs+bfs)

题目

树的节点数为n(n<=1e3),给定树的bfs序列和dfs序列,

bfs/dfs有多个节点选择时,是按点号增序进行bfs/dfs的

要求输出树的每个节点,都有哪几个儿子,多种方案时输出一种即可

思路来源

https://blog.csdn.net/keshuai19940722/article/details/38778243

题解

pos[x],指x在bfs序列中的位置

注意到,如果在dfs序列中,x<y且pos[y]>pos[x]+1,说明y一定是x的儿子①

 

为什么要刨去pos[y]==pos[x]+1这种情况,

仔细观察样例发现,当x<y在dfs序列和bfs序列中都相邻时,y既可以是x的儿子,也可以是x的兄弟

但是,当x<y在dfs序列中不相邻,但在bfs序列中相邻时,y一定是x的兄弟

所以,只需把一定是儿子的情况认定为儿子即可,剩下的向根回跳,

 

由于根不可能有兄弟,所以不满足①的情况下回跳,最终一定会被认定为根的儿子

且由于挂在树根下的各子树是独立的,保证这样挂一定有解,从而免去了对节点点号增序的特判

代码

#include<bits/stdc++.h>
using namespace std;
const int N=1e3+10;
int n,v,pos[N],len,rt;
stack<int>q;
vector<int>son[N]; 
int main()
{
	while(~scanf("%d",&n))
	{
		while(!q.empty())q.pop();
		for(int i=1;i<=n;++i)
		{
			son[i].clear();
			scanf("%d",&v);
			pos[v]=i;
		}
		scanf("%d",&rt);
		q.push(rt);
		for(int i=2;i<=n;++i)
		{
			scanf("%d",&v);
			while(q.top()!=rt&&pos[v]<=pos[q.top()]+1)q.pop();
			son[q.top()].push_back(v);
			q.push(v);
		}
		for(int i=1;i<=n;++i)
		{
			printf("%d:",i);
			len=son[i].size();
			for(int j=0;j<len;++j)
			printf(" %d",son[i][j]);
			puts("");
		}
	}
	return 0;
} 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
将下面这段代码改用python写出来: clear all; close all; fdir = '../dataset/iso/saii/'; %Reconstruction parameters depth_start = 710; depth_end = 720; depth_step = 1; pitch = 12; sensor_sizex = 24; focal_length = 8; lens_x = 4; lens_y = 4; %% import elemental image infile=[fdir '11.bmp']; outfile=[fdir, 'EIRC/']; mkdir(outfile); original_ei=uint8(imread(infile)); [v,h,d]=size(original_ei); %eny = v/lens_y; enx = h/lens_x; % Calculate real focal length %f_ratio=36/sensor_sizex; sensor_sizey = sensor_sizex * (v/h); %focal_length = focal_length*f_ratio; EI = zeros(v, h, d, lens_x * lens_y,'uint8'); for y = 1:lens_y for x = 1:lens_x temp=imread([fdir num2str(y),num2str(x),'.bmp']); EI(:, :, :, x + (y-1) * lens_y) = temp; end end %Reconstruction [EIy, EIx, Color] = size(EI(:,:,:,1)); %% EI_VCR time=[]; for Zr = depth_start:depth_step:depth_end tic; Shx = 8*round((EIx*pitch*focal_length)/(sensor_sizex*Zr)); Shy = 8*round((EIy*pitch*focal_length)/(sensor_sizey*Zr)); Img = (double(zeros(EIy+(lens_y-1)*Shy,EIx+(lens_x-1)*Shx, Color))); Intensity = (uint16(zeros(EIy+(lens_y-1)*Shy,EIx+(lens_x-1)*Shx, Color))); for y=1:lens_y for x=1:lens_x Img((y-1)*Shy+1:(y-1)*Shy+EIy,(x-1)*Shx+1:(x-1)*Shx+EIx,:) = Img((y-1)*Shy+1:(y-1)*Shy+EIy,(x-1)*Shx+1:(x-1)*Shx+EIx,:) + im2double(EI(:,:,:,x+(y-1)*lens_y)); Intensity((y-1)*Shy+1:(y-1)*Shy+EIy,(x-1)*Shx+1:(x-1)*Shx+EIx,:) = Intensity((y-1)*Shy+1:(y-1)*Shy+EIy,(x-1)*Shx+1:(x-1)*Shx+EIx,:) + uint16(ones(EIy,EIx,Color)); end end elapse=toc time=[time elapse]; display(['--------------- Z = ', num2str(Zr), ' is processed ---------------']); Fname = sprintf('EIRC/%dmm.png',Zr); imwrite(Img./double(Intensity), [fdir Fname]); end csvwrite([fdir 'EIRC/time.csv'],time);
07-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Code92007

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值