This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
typedef pair<int, int> P;
#define rep(i, n) for (int i=0; i<(n); i++)
#define all(x) x.begin(), x.end()
#define uniq(x) x.erase(unique(all(x)), x.end())
#define index(x, y) (int)(lower_bound(all(x), y) - x.begin())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
int N;
vector<P> G[200000];
int dp[200000][2];
//void update(int x, int p, int src) {
//}
void dfs(int x, int p) {
dp[x][0] = 0;
dp[x][1] = -INF;
for (P pp : G[x]) if (pp._1 != p) {
int t = pp._1, len = pp._2;
dfs(t, x);
int v = max(dp[t][0], dp[t][1]+len);
dp[x][0] += v;
dp[x][1] += v;
dp[x][1] = max(dp[x][1], dp[x][0]-v + dp[t][0]+len);
}
}
int main() {
cin >> N;
rep(i, N-1) {
int a, b, c;
cin >> a >> b >> c;
a--, b--;
G[a].pb(P(b, c));
G[b].pb(P(a, c));
}
//dfs(0, -1);
int m = 0;
rep(i, N) {
dfs(i, -1);
m = max(m, dp[i][0]);
}
cout << m << "\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |