1090. Highest Price in Supply Chain (25)
A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone involved in moving a product from supplier to customer.
Starting from one root supplier, everyone on the chain buys products from one's supplier in a price P and sell or distribute them in a price that is r% higher than P. It is assumed that each member in the supply chain has exactly one supplier except the root supplier, and there is no supply cycle.
Now given a supply chain, you are supposed to tell the highest price we can expect from some retailers.
Input Specification:
Each input file contains one test case. For each case, The first line contains three positive numbers: N (<=105), the total number of the members in the supply chain (and hence they are numbered from 0 to N-1); P, the price given by the root supplier; and r, the percentage rate of price increment for each distributor or retailer. Then the next line contains N numbers, each number Si is the index of the supplier for the i-th member. Sroot for the root supplier is defined to be -1. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print in one line the highest price we can expect from some retailers, accurate up to 2 decimal places, and the number of retailers that sell at the highest price. There must be one space between the two numbers. It is guaranteed that the price will not exceed 1010.
Sample Input:9 1.80 1.00 1 5 4 4 -1 4 5 3 6Sample Output:
1.85 2
题意:大致题意就是说一个进货的图,卖个下一个人就提高进价%r,问你最后最大的价格是多少?
1 5 4 4 -1 4 5 3 6 这些数表示他的上一个进货的人,编号为0- n-1
思路1:这道题比较难得地方在于建立一个树,这个父节点为-1的结点一定是这个树的根节点,然后从根节点往下深搜找到树的深度最大的叶节点,就可以求出结果了;
我是第一次接触自己建立树的搜索,以前做的都是直接给你的图;建立树一般采用二维数组,我采用c++里vector容器来存树;
ac代码:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
vector<int >a[100017];
int maxdep;
int cont;
int n;
void dfs(int tem,int dep)
{ if(a[tem].size()==0)
{ if(dep>maxdep)
{ cont=1;
maxdep=dep;
}
else if(maxdep==dep)
cont++;
}
for(int i=0;i<a[tem].size();i++)
dfs(a[tem][i],dep+1);
}
int main()
{ double pri,r;
while(scanf("%d %lf %lf",&n,&pri,&r)!=EOF)
{
int x,i;
int temp;
maxdep=0;
cont=0;
for(i=0;i<n;i++)
{ scanf("%d",&x);
if(x==-1)
{ temp=i;
continue;
}
a[x].push_back(i);
}
dfs(temp,0);
double ans=pri;
for(i=0;i<maxdep;i++)
{ ans=ans*(100+r)/100;
}
printf("%.2lf %d\n",ans,cont);
}
return 0;
}
思路2:思路一我是通过根节点去找叶节点,这里我的第二种思路是通过叶节点去找到根节点,然后判断最大深度即可,因为从叶节点到根节点用一维数组就可以存啦。。。。。。但是超时了。。。。。还望哪位大神留言指教一下啊、、、、
#include<stdio.h>
#include<string.h>
#include<math.h>
#define N 111111
int pre[N],deep[N];
int n;
double price ,r;
void dfs(int x)
{ if(pre[x]==-1)
{
return ;
}
else
{
dfs(pre[x]);
deep[x]=deep[pre[x]]+1;
}
return ;
}
int main()
{
int i;
scanf("%d %lf %lf",&n,&price,&r);
memset(deep,0,sizeof(deep));
for(i=0;i<n;i++)
{ scanf("%d",&pre[i]);
}
for(i=0;i<n;i++)
{ if(deep[i]==0)
dfs(i);
}
int count=0,max;
for(i=0;i<n;i++)
{ if(deep[i]>max)
{
max=deep[i];
count=1;}
else if(deep[i]==max)
count++;
}
for(i=0;i<max;i++)
price*=1+r/100;
printf("%.2lf %d",price,count);
return 0;
}