Comments
On darkshadows → DP on Trees Tutorial, 6 years ago
0

What about the complexity if this code for the 1st problem(subset coins one)

Your code here...
/*
// Sample code to perform I/O:
cin >> name;                            // Reading input from STDIN
cout << "Hi, " << name << ".\n";        // Writing output to STDOUT

// Warning: Printing unwanted or ill-formatted data to output will cause the test cases to fail
*/
// Write your code here
//Given a tree with N nodes and N-1 edges, calculate the maximum sum of the node values from root to any of the leaves without re-visiting any node
#include<bits/stdc++.h>
#define fr(i,a,b) for(i=a;i<=b;i++)
#define rf(i,a,b) for(i=a;i<=b;i--)
#define ll long long int
#define pb push_back
const int inf = 1e9;
using namespace std;
int dp[100005][2];
ll mod = 1e9+7;
/*typedef struct node{
	int v;
	int adj;
}node;*/
int vals[100005];
vector<int> g[100005];
int dfs(bool t, int u,int parent){
	// first dont take the nodes1
	int ans=0;
	if(dp[u][t]!=-1){
		return dp[u][t];
	}
	if(t){
		// /int ans=0;
		ans+=vals[u];
		for(int v:g[u]){
			if(v!=parent)
				ans += dfs(!t,v,u);
		}
		//return ans;
	}else{
		for(int v:g[u]){
			if(v!=parent)
					ans += dfs(!t,v,u);
		}
	}
	return dp[u][t]=ans;
}
void solve(){

	int n;
	clock_t start, end;
	cin>>n;
	int u,v;
	for(int i=0;i<n;i++){
		dp[i][0]=-1;
		dp[i][1]=-1;
		cin >> vals[i];
	}
	for(int i=0;i<(n-1);i++){
		cin >> u >> v;
		g[u].pb(v);
		g[v].pb(u);
	}
	// taking root as zero
	bool t=0;
	start=clock();
	cout << max(dfs(t,0,-1),dfs(!t,0,-1)) << "\n";
	end=clock();

	//double timetaken = double(end-start)/double(CLOCKS_PER_SEC);
	//cout << "time : " << fixed << setprecision(50) <<  timetaken << "\n"; 
	return;
}
int main(){
    
    int t;
    cin >> t;
    while(t--){
        solve();
    }
    return 0;
}

Here's the code submission here

Actually , bro there was a base case missing when index > n ...for that I added (return 0) in the same code and it got accepted :).....btw thanks for checking it.

Thanks man that worked :) ...got AC but not pretty much clear why function should return zero for ind > n

What should it be..?

How to do that bro, since it is a 2-d array

Still not working out...even after initializing the res value

@actium Bro I made this change now it isn't showing that error , but it is returning wrong answer for the square root calculation...and again the same is working totally fine in my compiler.... Still confused