-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2467_mostProfitablePathInTree.cpp
51 lines (50 loc) · 1.57 KB
/
2467_mostProfitablePathInTree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class Solution {
private:
bool findBobPath(vector<int> &bobPath, vector<vector<int>> &adj, int curr, int parent){
if(curr==0){
return true;
}
for(auto &it: adj[curr]){
if(it != parent){
bobPath.push_back(it);
if(findBobPath(bobPath, adj, it, curr) == true) return true;
bobPath.pop_back();
}
}
return false;
}
void aliceReward(vector<vector<int>> &adj, vector<int> &amount, int curr, int parent, int &ans, int &maxi){
ans += amount[curr];
if(adj[curr].size()==1 && curr!=0){
maxi = max(maxi,ans);
}
for(auto &it: adj[curr]){
if(it != parent){
aliceReward(adj, amount, it, curr, ans, maxi);
}
}
ans -= amount[curr];
}
public:
int mostProfitablePath(vector<vector<int>>& edges, int bob, vector<int>& amount) {
int n = amount.size();
vector<vector<int>> adj(n);
for(auto &it: edges){
adj[it[0]].push_back(it[1]);
adj[it[1]].push_back(it[0]);
}
vector<int> bobPath; bobPath.push_back(bob);
findBobPath(bobPath, adj, bob, -1);
int sz = bobPath.size();
if(sz&1){
for(int i=0; i<sz/2; i++) amount[bobPath[i]] = 0;
amount[bobPath[sz/2]] /= 2;
}
else{
for(int i=0; i<sz/2; i++) amount[bobPath[i]] = 0;
}
int ans=0, maxi=INT_MIN;
aliceReward(adj, amount, 0, -1, ans, maxi);
return maxi;
}
};