#include <bits/stdc++.h>
using namespace std;
int n;
vector <int> g[200010];
vector <int> c[200010];
long long dp[200010][2][2];
const long long inf = 1e16;
void dfs(int x, int par, int parCost) {
for(size_t i = 0; i < g[x].size(); i++) {
int y = g[x][i];
int w = c[x][i];
if(y == par) continue;
dfs(y, x, w);
}
long long sum;
vector <long long> best;
// solves 10 case when node x is a middle node
sum = 0;
vector < pair <long long, int> > t1, t2;
for(size_t i = 0; i < g[x].size(); i++) {
int y = g[x][i];
int cost = c[x][i];
if(y == par) continue;
long long add = max(dp[y][0][1], dp[y][0][0]);
sum += add;
t1.emplace_back(cost + dp[y][1][0] - add, y);
t2.emplace_back(cost + dp[y][0][0] - add, y);
}
/*
for(size_t i = 0; i < g[x].size(); i++) {
for(size_t j = 0; j < g[x].size(); j++) {
if(i == j) continue;
int p = g[x][i];
int q = g[x][j];
int pcost = c[x][i];
int qcost = c[x][j];
if(p == par || q == par) continue;
long long opt = pcost + qcost + max(dp[p][1][0] + dp[q][0][0], dp[p][0][0] + dp[q][1][0]);
long long del = max(dp[p][0][1], dp[p][0][0]) + max(dp[q][0][1], dp[q][0][0]);
dp[x][1][0] = max(dp[x][1][0], opt + sum - del);
}
}
*/
sort(t1.begin(), t1.begin());
sort(t2.begin(), t2.end());
reverse(t1.begin(), t1.end());
reverse(t2.begin(), t2.end());
if(t1.size() > 1) {
for(int i = 0; i <= 1; i++) {
for(int j = 0; j <= 1; j++) {
if(t1[i].second != t2[j].second) dp[x][1][0] = max(dp[x][1][0], sum + t1[i].first + t2[j].first);
}
}
}
// end case
// solves 10 case when node x is not a middle node
sum = 0;
for(size_t i = 0; i < g[x].size(); i++) {
int y = g[x][i];
int cost = c[x][i];
if(y == par) continue;
long long add = max(dp[y][0][1], dp[y][0][0]);
sum += add;
best.push_back(max(dp[y][1][0], dp[y][1][1]) - add);
}
sort(best.begin(), best.end());
reverse(best.begin(), best.end());
if(best.size() > 0) dp[x][1][0] = max(dp[x][1][0], sum + best[0]);
best.clear();
// end case
// solves 11 case
sum = 0;
for(size_t i = 0; i < g[x].size(); i++) {
int y = g[x][i];
int cost = c[x][i];
if(y == par) continue;
long long add = max(dp[y][0][1], dp[y][0][0]);
sum += add;
best.push_back(dp[y][1][0] - add + cost);
}
sort(best.begin(), best.end());
reverse(best.begin(), best.end());
if(best.size() > 0) {
dp[x][1][1] = sum + best[0] + parCost;
} else {
dp[x][1][1] = -inf;
}
best.clear();
// end case
// solves 01 case
sum = 0;
for(size_t i = 0; i < g[x].size(); i++) {
int y = g[x][i];
int cost = c[x][i];
if(y == par) continue;
long long add = max(dp[y][0][0], dp[y][0][1]);
sum += add;
best.push_back(dp[y][0][0] - add + cost);
}
sort(best.begin(), best.end());
reverse(best.begin(), best.end());
if(best.size() > 0) {
dp[x][0][1] = sum + best[0] + parCost;
} else {
dp[x][0][1] = -inf;
}
best.clear();
// end case
// solves 00 case
sum = 0;
for(size_t i = 0; i < g[x].size(); i++) {
int y = g[x][i];
int cost = c[x][i];
if(y == par) continue;
long long add = max(dp[y][0][0], dp[y][0][1]);
sum += add;
}
dp[x][0][0] = sum;
// end case
}
int main() {
scanf("%d", &n);
for(int i = 1; i < n; i++) {
int p, q, r;
scanf("%d %d %d", &p, &q, &r);
g[p].push_back(q);
g[q].push_back(p);
c[p].push_back(r);
c[q].push_back(r);
}
dfs(1, 0, 0);
long long ans = dp[1][1][0];
printf("%lld\n", ans);
// cout << dp[6][1][0] << endl;
// cout << dp[3][1][0] << endl;
// cout << dp[2][0][1] << endl;
// cout << dp[8][1][0] << endl;
return 0;
}
Compilation message
beads.cpp: In function 'void dfs(int, int, int)':
beads.cpp:66:7: warning: unused variable 'cost' [-Wunused-variable]
int cost = c[x][i];
^~~~
beads.cpp:124:7: warning: unused variable 'cost' [-Wunused-variable]
int cost = c[x][i];
^~~~
beads.cpp: In function 'int main()':
beads.cpp:135:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
beads.cpp:138:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d", &p, &q, &r);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9728 KB |
Output is correct |
2 |
Incorrect |
10 ms |
9728 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9728 KB |
Output is correct |
2 |
Incorrect |
10 ms |
9728 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9728 KB |
Output is correct |
2 |
Incorrect |
10 ms |
9728 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9728 KB |
Output is correct |
2 |
Incorrect |
10 ms |
9728 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |