-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2467. Most Profitable Path in a Tree.cpp
More file actions
40 lines (38 loc) · 1.21 KB
/
Copy path2467. Most Profitable Path in a Tree.cpp
File metadata and controls
40 lines (38 loc) · 1.21 KB
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
class Solution {
private:
void solve(int node,int pa,vector<vector<int>> &v, vector<int> &depth, vector<int> &par) {
par[node] = pa;
if(pa != -1) depth[node] = depth[pa] + 1;
for(int ch : v[node]) {
if(ch != pa) solve(ch,node,v,depth,par);
}
}
void dfs(int node,int pa, vector<vector<int>> &v, vector<int> &amount) {
if(pa != -1) amount[node] += amount[pa];
for(int ch : v[node]) {
if(ch != pa) dfs(ch,node,v,amount);
}
}
public:
int mostProfitablePath(vector<vector<int>>& edges, int bob, vector<int>& amount) {
int n = amount.size();
vector<int> depth(n,0),par(n,-1);
vector<vector<int>> tree(n);
for(auto it : edges) {tree[it[0]].push_back(it[1]);tree[it[1]].push_back(it[0]);}
solve(0,-1,tree,depth,par);
int p = depth[bob]+1;
int dis = p/2;
while(dis) {
amount[bob] = 0;
bob = par[bob];
dis--;
}
if(p%2) amount[bob] /= 2;
dfs(0,-1,tree,amount);
int ans = -1e9;
for(int i = 1; i < n; i++) {
if(tree[i].size() == 1) ans = max(ans,amount[i]);
}
return ans;
}
};