-
Notifications
You must be signed in to change notification settings - Fork 59
/
Max path sum Tree(array) using DFS.cpp
71 lines (56 loc) · 1.3 KB
/
Max path sum Tree(array) using DFS.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include<iostream>
#include<vector>
#include<cstdio>
#include<climits>
#include<cstring>
using namespace std;
//Magic-sum on HackerEarth
vector<int> list[515];
int visited[515],ans,sum;
int dfs(int i,int a[],int firstNode=0){
visited[i]=1;
//If it is a leaf node , then update ans in the case the path starts and ends at the same node
if(list[i].size()==1)
ans=max(ans,a[i]);
//If it is a leaf node and and not the starting leaf node.
if(list[i].size()==1&&!firstNode)
return a[i];
int sum=INT_MIN;
for(int j=0;j<list[i].size();j++){
if(!visited[list[i][j]])
sum=max(sum,dfs(list[i][j],a));
}
return a[i]+sum;
}
int main(){
int t,n,i;
cin>>t;
while(t--){
cin>>n;
int a[n];
assert(N>=1 && N<=511);
if(n==1)
{cin>>a[0];
cout<<a[0]<<endl;
continue;
}
for(i=0;i<n;i++)
list[i].clear();
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n/2;i++){
list[i].push_back(2*i+1);
list[i].push_back(2*i+2);
list[2*i+1].push_back(i);
list[2*i+2].push_back(i);
}
ans=INT_MIN;
for(i=n/2;i<n;i++){
//Apply DFS on all leaf nodes as the starting point.
memset(visited,0,sizeof visited);
ans = max(ans,dfs(i,a,1));
}
printf("%d\n",ans);
}
return 0;
}