1090 Highest Price in Supply Chain

#include<bits/stdc++.h>
using namespace std;
const int MAXN=100001;
struct Node{
	int h;
	vector<int> child;
}node[MAXN];
int maxnum=0,cntnum=0;
void dfs(int root,int depth){
	if(node[root].child.size()==0){
		if(maxnum<depth){
			maxnum=depth;
			cntnum=1;
		}else if(maxnum==depth){
			cntnum++;
		}
	}
	for(int i=0;i<node[root].child.size();i++){
		dfs(node[root].child[i],depth+1);
	}
}
int main()
{
	#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
	#endif
	int n;
	double p,r;
	scanf("%d %lf %lf",&n,&p,&r);
	int root;
	for(int i=0;i<n;i++){
		int temp;
		scanf("%d",&temp);
		if((temp)!=-1)
		node[temp].child.push_back(i);
		else root=i;
	}
	dfs(root,0);
	for(int i=0;i<maxnum;i++){
		p=p*(1+r*0.01);
	}
	printf("%.2lf %d",p,cntnum);
}

这种附带求有几个相同的树高或者depth的时候用深搜比较合适,我试着不用深搜,只用一个求树高的函数用两次只得到13分,很容易超时...而且还容易混掉,唉~

#include<bits/stdc++.h>
using namespace std;
const int MAXN=100001;
struct Node{
	int h;
	vector<int> child;
}node[MAXN];
struct tnode{
	int maxnum;
	int cntnum;
	tnode(){
		maxnum=0;
		cntnum=0;
	}
};
int rot;
int a=rot;
tnode* getheight1(int root){
	if(node[root].child.size()==0){
		tnode* ttt=new tnode;
		return ttt;
	}
	tnode* temp=new tnode;
	for(int i=0;i<node[root].child.size();i++){
		node[node[root].child[i]].h=node[root].h+1;
		tnode* tt=getheight1(node[root].child[i]);
		if(tt->maxnum>temp->maxnum){
			temp->maxnum=tt->maxnum;
		}
	}
	temp->maxnum=temp->maxnum+1;
	return temp;
}
int main(){
	#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
	#endif
	int n;
	double p,r;
	scanf("%d %lf %lf",&n,&p,&r);
	
	for(int i=0;i<n;i++){
		int temp;
		scanf("%d",&temp);
		if((temp)!=-1)
		node[temp].child.push_back(i);
		else rot=i;
	}
	node[rot].h=1;
	tnode* maxheight=getheight1(rot);
	for(int i=0;i<maxheight->maxnum;i++){
		p=p*(1+r*0.01);
	}
	int maxcnt=0;
	int cnt=0;
	for(int i=0;i<n;i++){
		if(node[i].child.size()==0){
			if(maxcnt<node[i].h){
				maxcnt=node[i].h;
				cnt=1;
			}else if(maxcnt==node[i].h){
				cnt++;
			}
		}
	}
	printf("%.2lf %d",p,cnt);
	return 0;
}

发现用getheight貌似也可以做啊,一开始还想这么麻烦(上面两段代码都AC了),唉太笨(~ ̄(OO) ̄)ブ

猜你喜欢

转载自blog.csdn.net/csg3140100993/article/details/81705002