This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <numeric>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<long> cumulativeNeighbourPigs;
void init(vector<long>& pigs, vector<vector<int>>& adj)
{
cumulativeNeighbourPigs.resize(pigs.size());
for(int i = 0; i<pigs.size(); ++i)
{
for(auto child : adj[i])
{
cumulativeNeighbourPigs[i] += pigs[child];
}
}
}
long DFS(int startNode, vector<vector<int>>& adj, vector<long>& pig, int depth, const int v, long currP)
{
if(depth == v)
{
return currP;
}
else
{
if(depth > v)
return -1;
else
{
long currMax = 0;
for(auto child : adj[startNode])
{
// cout<<"trying child: "<<child<<endl;
long childPigeons = cumulativeNeighbourPigs[child] - pig[startNode];
childPigeons = DFS(child, adj, pig, depth+1, v, currP+ childPigeons);
if(currMax < childPigeons)
currMax = childPigeons;
// cout<<"new max "<<currMax<<endl;
}
return max(currP, currMax);
}
}
}
long maxPigeonsPath(vector<vector<int>>& adj, vector<long>& pig, const int v)
{
long maxP = 0;
for(int i =0 ;i<adj.size(); ++i)
{
long pigs = DFS(i, adj, pig, 1, v, cumulativeNeighbourPigs[i]);
if(pigs > maxP)
maxP = pigs;
}
return maxP;
}
int main()
{
int n; cin>>n;
int v; cin>>v;
vector<long> pigeons(n);
for(int i =0; i<n; ++i)
cin>>pigeons[i];
vector<vector<int>> adjList(n);
for(int i = 0; i<n-1; ++i)
{
int a, b;
cin>>a>>b;
adjList[a-1].push_back(b-1);
adjList[b-1].push_back(a-1);
}
init(pigeons, adjList);
cout<<maxPigeonsPath(adjList, pigeons, v)<<endl;
return 0;
}
Compilation message (stderr)
chase.cpp: In function 'void init(std::vector<long int>&, std::vector<std::vector<int> >&)':
chase.cpp:13:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
13 | for(int i = 0; i<pigs.size(); ++i)
| ~^~~~~~~~~~~~
chase.cpp: In function 'long int maxPigeonsPath(std::vector<std::vector<int> >&, std::vector<long int>&, int)':
chase.cpp:52:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | for(int i =0 ;i<adj.size(); ++i)
| ~^~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |