2022.1.28 练习 PAT甲 1090 Highest Price in Supply Chain (原题链接)
段错误代码:(不明白为什么段错误,呜呜,求大佬指正)
#include <bits/stdc++.h>
using namespace std;
const int MAX_SIZE=100010;
int n;
double p,r;
int hashtable[MAX_SIZE]={0};
struct node
{
vector<int> child;
double price;
int high;
}Node[MAX_SIZE];
void DFS(int index,int high)
{
hashtable[high]++;
Node[index].high=high;
Node[index].price=p*pow((1+r),high);
for(unsigned int i=0;i<Node[index].child.size();i++)
{
DFS(Node[index].child[i],high+1);
}
}
int main()
{
std::ios::sync_with_stdio(false);
cin>>n>>p>>r;
r=r/100;
int root=-1;
for(int i=0;i<n;i++)
{
int tmp;
cin>>tmp;
Node[tmp].child.push_back(i);
if(tmp==-1)
root=i;
}
DFS(root,0);
double max_p=-1;
int res=-1;
for(int i=0;i<n;i++)
{
if(Node[i].price>max_p)
{
max_p=Node[i].price;
res=hashtable[Node[i].high];
}
}
cout<<setiosflags(ios::fixed)<<setprecision(2)<<max_p<<" "<<res;
return 0;
}
AC代码:
#include <bits/stdc++.h>
using namespace std;
const int MAX_SIZE=100010;
int n;
double p,r;
vector<int> child[MAX_SIZE];
int max_high=0,num=0;
void DFS(int index,int high)
{
if(child[index].size()==0)
{
if(high>max_high)
{
max_high=high;
num=1;//重置最大深度的叶节点个数为1
}
else if(high==max_high)
{
num++;
}
return;
}
for(unsigned int i=0;i<child[index].size();i++)
{
DFS(child[index][i],high+1);
}
}
int main()
{
std::ios::sync_with_stdio(false);
cin>>n>>p>>r;
r=r/100;
int root=-1;
for(int i=0;i<n;i++)
{
int tmp;
cin>>tmp;
if(tmp!=-1)
child[tmp].push_back(i);
else
root=i;
}
DFS(root,0);
cout<<setiosflags(ios::fixed)<<setprecision(2)<<p*pow(1+r,max_high)<<" "<<num;
return 0;
}
我就不懂了,我感觉这两个代码一样的嘛,为啥,哎,心痛,好痛苦,追求AC的路上,我还是渣渣。