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 <bits/stdc++.h>
using namespace std;
const int NMAX = 200000 + 5;
const int INF = 2E9 + 15;
int N;
vector <pair <int, int> > graph[NMAX];
int dp[NMAX][2];
void dfs(int node, int father) {
dp[node][0] = 0;
dp[node][1] = -INF;
for (auto it: graph[node])
if (it.first != father) {
dfs(it.first, node);
int val = max(dp[it.first][0], dp[it.first][1] + it.second);
dp[node][0] += val;
dp[node][1] = max(dp[node][1], it.second + dp[it.first][0] - val);
}
dp[node][1] = dp[node][0] + dp[node][1];
}
int main()
{
//freopen("data.in", "r", stdin);
cin >> N;
for (int i = 1; i < N; ++ i) {
int a, b, c;
cin >> a >> b >> c;
graph[a].push_back({b, c});
graph[b].push_back({a, c});
}
int ans = 0;
for (int i = 1; i <= N; ++ i) {
dfs(i, 0);
ans = max(ans, dp[i][0]);
}
cout << ans << '\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... |