#include<bits/stdc++.h>
using namespace std;
#define Sz(x) (x).size()
const int N = 1e5 + 10;
long long dp[N][2];
vector<pair<int, int> > adj[N];
void DFS(int x, int p = 0, int cnt = 0) {
for(auto [j, t] : adj[x]) {
if(j == p) continue;
DFS(j, x, t);
}
if(Sz(adj[x]) == 1 && x != 1) {
return;
}
long long sum = 0;
for(auto [j, t] : adj[x]) {
if(j == p) continue;
sum += dp[j][1];
}
dp[x][0] = sum;
vector<int> vc;
for(auto [j, t] : adj[x]) {
if(j == p) continue;
vc.push_back(dp[j][0] + t - dp[j][1]);
}
sort(vc.begin(), vc.end(), greater<int>());
if(vc.size() > 1) {
dp[x][0] = max(dp[x][0], sum + vc[0] + vc[1]);
}
dp[x][1] = dp[x][0];
if(vc.size() > 0) {
dp[x][1] = max(dp[x][1], sum + vc[0] + cnt);
}
}
int main() {
int n; cin >>n;
for(int i = 0; i < n - 1; i++) {
int u, v, c; cin >>u >>v >>c;
adj[u].push_back({v, c}), adj[v].push_back({u, c});
}
DFS(1);
cout<<dp[1][0];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Incorrect |
2 ms |
2604 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Incorrect |
2 ms |
2604 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Incorrect |
2 ms |
2604 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Incorrect |
2 ms |
2604 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |