#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 7;
const int INF = 1e9 + 7;
struct Edge{
int v, c;
Edge(int _v, int _c){
v = _v;
c = _c;
}
};
int n, down[N][2], up[N][2];
vector<Edge> adj[N];
void Dfs(int u, int p){
down[u][0] = 0;
down[u][1] = -INF;
for(Edge e : adj[u]){
int v = e.v;
int c = e.c;
if (v == p)
continue;
Dfs(v, u);
down[u][1] = max(down[u][1] + max(down[v][0], down[v][1] + c), down[u][0] + down[v][0] + c);
down[u][0] += max(down[v][0], down[v][1] + c);
}
}
void Dfs2(int u, int p){
if (p == -1){
up[u][0] = 0;
up[u][1] = -INF;
}
int sum = up[u][0];
multiset<int> s;
s.insert(up[u][1] - up[u][0]);
for(Edge e : adj[u]){
int v = e.v;
int c = e.c;
if (v == p)
continue;
sum += max(down[v][0], down[v][1] + c);
s.insert(down[v][0] + c - max(down[v][0], down[v][1] + c));
}
for(Edge e : adj[u]){
int v = e.v;
int c = e.c;
if (v == p)
continue;
sum -= max(down[v][0], down[v][1] + c);
s.erase(s.find(down[v][0] + c - max(down[v][0], down[v][1] + c)));
up[v][0] = sum + max(0, *(--s.end()) + c);
up[v][1] = sum + c;
s.insert(down[v][0] + c - max(down[v][0], down[v][1] + c));
sum += max(down[v][0], down[v][1] + c);
}
for(Edge e : adj[u]){
int v = e.v;
if (v == p)
continue;
Dfs2(v, u);
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for(int i = 1; i < n; i++){
int u, v, c;
cin >> u >> v >> c;
adj[u].push_back(Edge(v, c));
adj[v].push_back(Edge(u, c));
}
Dfs(1, -1);
Dfs2(1, -1);
int res = 0;
for(int i = 1; i <= n; i++)
res = max(res, up[i][0] + down[i][0]);
cout << res;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
4 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
4 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
3 ms |
4940 KB |
Output is correct |
22 |
Correct |
3 ms |
4940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
4 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
3 ms |
4940 KB |
Output is correct |
22 |
Correct |
3 ms |
4940 KB |
Output is correct |
23 |
Correct |
7 ms |
5196 KB |
Output is correct |
24 |
Correct |
7 ms |
5300 KB |
Output is correct |
25 |
Correct |
7 ms |
5196 KB |
Output is correct |
26 |
Correct |
10 ms |
5452 KB |
Output is correct |
27 |
Correct |
11 ms |
5712 KB |
Output is correct |
28 |
Correct |
13 ms |
6068 KB |
Output is correct |
29 |
Correct |
13 ms |
6112 KB |
Output is correct |
30 |
Correct |
12 ms |
6020 KB |
Output is correct |
31 |
Correct |
12 ms |
6956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4944 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
4 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
3 ms |
4940 KB |
Output is correct |
22 |
Correct |
3 ms |
4940 KB |
Output is correct |
23 |
Correct |
7 ms |
5196 KB |
Output is correct |
24 |
Correct |
7 ms |
5300 KB |
Output is correct |
25 |
Correct |
7 ms |
5196 KB |
Output is correct |
26 |
Correct |
10 ms |
5452 KB |
Output is correct |
27 |
Correct |
11 ms |
5712 KB |
Output is correct |
28 |
Correct |
13 ms |
6068 KB |
Output is correct |
29 |
Correct |
13 ms |
6112 KB |
Output is correct |
30 |
Correct |
12 ms |
6020 KB |
Output is correct |
31 |
Correct |
12 ms |
6956 KB |
Output is correct |
32 |
Correct |
45 ms |
8432 KB |
Output is correct |
33 |
Correct |
43 ms |
8436 KB |
Output is correct |
34 |
Correct |
46 ms |
8544 KB |
Output is correct |
35 |
Correct |
259 ms |
19012 KB |
Output is correct |
36 |
Correct |
272 ms |
19132 KB |
Output is correct |
37 |
Correct |
298 ms |
19176 KB |
Output is correct |
38 |
Correct |
324 ms |
27956 KB |
Output is correct |
39 |
Correct |
308 ms |
28812 KB |
Output is correct |
40 |
Correct |
296 ms |
27820 KB |
Output is correct |
41 |
Correct |
327 ms |
35484 KB |
Output is correct |