记录编号 224928 评测结果 A
题目名称 [UVa 699] 下落的树叶 最终得分 100
用户昵称 Gravatarliu_runda 是否通过 通过
代码语言 C++ 运行时间 0.000 s
提交时间 2016-02-16 19:35:17 内存使用 0.30 MiB
显示代码纯文本
#include<cstdio>
#include<cstring> 
struct node{
	int data;int lch,rch,pos;
}tree[400];
int len=1;
int rebuild(){
	int tmp;
	scanf("%d",&tmp);
	if(tmp==-1)return 0;
	tree[len].data = tmp; 
	tmp=len;
	len++;
	tree[tmp].lch=rebuild();
	tree[tmp].rch=rebuild();
	return tmp;
}
int leaves[1000];
void pre(int x){
	leaves[tree[x].pos+100]+=tree[x].data;
	if(tree[x].lch){
		tree[tree[x].lch].pos=tree[x].pos-1;
		pre(tree[x].lch);
	}
	if(tree[x].rch){
		tree[tree[x].rch].pos=tree[x].pos+1;
		pre(tree[x].rch);
	}
}
int main(){
	freopen("leaves.in","r",stdin);
	freopen("leaves.out","w",stdout);
	int t=0;
	while(t++,rebuild()){
		memset(leaves,0,sizeof(leaves));
		len = 1; 
		pre(1);
		printf("Case %d:\n",t);
		for(int i = 0;i<900;++i){
			if(leaves[i])printf("%d ",leaves[i]);
		}
		printf("\n\n");
		memset(tree,0,sizeof(tree));
	} 
	fclose(stdin);fclose(stdout);
	return 0;
}