"Test Instructions" find the highest retail price in a supply and marketing network and the corresponding number of retailers
Ideas
is to ask for the maximum depth from the root node to the leaf node, to record the number of the person supplied by each supplier, DFS can
#include <iostream>
#include <vector>
#include <cmath>
#include <cstdio>
using namespace std;
int maxDepth = 0,cnt = 0;
vector<vector<int>> net;
void Dfs (int index, int depth) {
depth++;
if (Net[index].size () ==0) {
if (depth>maxdepth) {
maxDepth = depth;
CNT = 1;
}
else if (depth==maxdepth) {
cnt++;
}
}
else{for
(int i=0; i<net[index].size (); i++) {
dfs (net[index][i],depth);}}
}
int main (int argc, char const *argv[])
{
int n,rootid;
Double p,r;
CIN >> N >> P >> r;
Net.resize (n);
for (int i=0; i<n; i++) {
int num;
CIN >> num;
if (num==-1) {
rootid = i;
Continue;
}
Net[num].push_back (i);
}
DFS (rootid,-1);
printf ("%.2lf%d", P*pow (1+0.01*r,maxdepth), CNT);
System ("pause");
return 0;
}