Skip to content

Instantly share code, notes, and snippets.

@luizribeiro
Created March 1, 2011 00:43
Show Gist options
  • Save luizribeiro/848380 to your computer and use it in GitHub Desktop.
Save luizribeiro/848380 to your computer and use it in GitHub Desktop.
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
#define NN 200024
int n, root;
vector< pair<int, int> > adj[NN];
int dp[NN];
int ans;
int dfs(int u) {
dp[u] = 0;
for(int i = 0; i < (int)adj[u].size(); i++) {
int j = adj[u][i].first;
if(dp[j] == -1)
dp[u] += max(0, adj[u][i].second + dfs(j));
}
ans = max(ans, dp[u]);
return dp[u];
}
int main(void) {
while(scanf("%d", &n) && n) {
for(int i = 0; i < NN; i++) adj[i].clear();
for(int i = 0; i < n; i++) {
int a, b, p;
scanf("%d %d %d", &a, &b, &p), a--, b--;
root = a;
adj[a].push_back(make_pair(b,p));
adj[b].push_back(make_pair(a,p));
root = a;
}
ans = 0;
memset(dp, -1, sizeof(dp));
dfs(root);
printf("%d\n", ans);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment