#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> ii;
const int N = 1e4 + 5;
int n, dp[N][2], ans;
vector<ii> a[N], b[N];
vector<int> l[N], r[N];
void dfs(int u, int p) {
dp[u][0] = dp[u][1] = 0;
int cnt = 0;
for(int i = 0; i < a[u].size(); ++i) {
int v = a[u][i].first;
if(v == p) continue;
dfs(v, u);
cnt++; b[u][cnt] = a[u][i];
}
l[u][0] = 0;
for(int i = 1; i <= cnt; ++i) {
int v = b[u][i].first, w = b[u][i].second;
l[u][i] = l[u][i - 1] + max(dp[v][1] + w, dp[v][0]);
}
r[u][cnt + 1] = 0;
for(int i = cnt; i >= 1; --i) {
int v = b[u][i].first, w = b[u][i].second;
r[u][i] = r[u][i + 1] + max(dp[v][1] + w, dp[v][0]);
}
dp[u][0] = l[u][cnt];
for(int i = 1; i <= cnt; ++i) {
int v = b[u][i].first, w = b[u][i].second;
dp[u][1] = max(dp[u][1], l[u][i - 1] + r[u][i + 1] + dp[v][0] + w);
}
if(a[u].size() == 1) dp[u][1] = -1e9;
}
int main() {
cin >> n;
for(int i = 1; i < n; ++i) {
int u, v, w;
cin >> u >> v >> w;
a[u].push_back(ii(v, w));
a[v].push_back(ii(u, w));
}
for(int i = 1; i <= n; ++i) l[i].assign(a[i].size() + 2, 0), r[i].assign(a[i].size() + 2, 0), b[i].assign(a[i].size() + 2, ii(0, 0));
for(int i = 1; i <= n; ++i) {
dfs(i, i);
ans = max(ans, dp[i][0]);
}
cout << ans;
}
Compilation message
beads.cpp: In function 'void dfs(int, int)':
beads.cpp:15:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < a[u].size(); ++i) {
~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1280 KB |
Output is correct |
2 |
Correct |
3 ms |
1280 KB |
Output is correct |
3 |
Correct |
3 ms |
1280 KB |
Output is correct |
4 |
Correct |
3 ms |
1280 KB |
Output is correct |
5 |
Correct |
3 ms |
1280 KB |
Output is correct |
6 |
Correct |
2 ms |
1280 KB |
Output is correct |
7 |
Correct |
3 ms |
1280 KB |
Output is correct |
8 |
Correct |
3 ms |
1280 KB |
Output is correct |
9 |
Correct |
3 ms |
1280 KB |
Output is correct |
10 |
Correct |
3 ms |
1280 KB |
Output is correct |
11 |
Correct |
3 ms |
1280 KB |
Output is correct |
12 |
Correct |
3 ms |
1280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1280 KB |
Output is correct |
2 |
Correct |
3 ms |
1280 KB |
Output is correct |
3 |
Correct |
3 ms |
1280 KB |
Output is correct |
4 |
Correct |
3 ms |
1280 KB |
Output is correct |
5 |
Correct |
3 ms |
1280 KB |
Output is correct |
6 |
Correct |
2 ms |
1280 KB |
Output is correct |
7 |
Correct |
3 ms |
1280 KB |
Output is correct |
8 |
Correct |
3 ms |
1280 KB |
Output is correct |
9 |
Correct |
3 ms |
1280 KB |
Output is correct |
10 |
Correct |
3 ms |
1280 KB |
Output is correct |
11 |
Correct |
3 ms |
1280 KB |
Output is correct |
12 |
Correct |
3 ms |
1280 KB |
Output is correct |
13 |
Correct |
3 ms |
1280 KB |
Output is correct |
14 |
Correct |
3 ms |
1280 KB |
Output is correct |
15 |
Correct |
3 ms |
1280 KB |
Output is correct |
16 |
Correct |
4 ms |
1280 KB |
Output is correct |
17 |
Correct |
4 ms |
1280 KB |
Output is correct |
18 |
Correct |
4 ms |
1280 KB |
Output is correct |
19 |
Correct |
3 ms |
1280 KB |
Output is correct |
20 |
Correct |
4 ms |
1280 KB |
Output is correct |
21 |
Correct |
4 ms |
1280 KB |
Output is correct |
22 |
Correct |
4 ms |
1280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1280 KB |
Output is correct |
2 |
Correct |
3 ms |
1280 KB |
Output is correct |
3 |
Correct |
3 ms |
1280 KB |
Output is correct |
4 |
Correct |
3 ms |
1280 KB |
Output is correct |
5 |
Correct |
3 ms |
1280 KB |
Output is correct |
6 |
Correct |
2 ms |
1280 KB |
Output is correct |
7 |
Correct |
3 ms |
1280 KB |
Output is correct |
8 |
Correct |
3 ms |
1280 KB |
Output is correct |
9 |
Correct |
3 ms |
1280 KB |
Output is correct |
10 |
Correct |
3 ms |
1280 KB |
Output is correct |
11 |
Correct |
3 ms |
1280 KB |
Output is correct |
12 |
Correct |
3 ms |
1280 KB |
Output is correct |
13 |
Correct |
3 ms |
1280 KB |
Output is correct |
14 |
Correct |
3 ms |
1280 KB |
Output is correct |
15 |
Correct |
3 ms |
1280 KB |
Output is correct |
16 |
Correct |
4 ms |
1280 KB |
Output is correct |
17 |
Correct |
4 ms |
1280 KB |
Output is correct |
18 |
Correct |
4 ms |
1280 KB |
Output is correct |
19 |
Correct |
3 ms |
1280 KB |
Output is correct |
20 |
Correct |
4 ms |
1280 KB |
Output is correct |
21 |
Correct |
4 ms |
1280 KB |
Output is correct |
22 |
Correct |
4 ms |
1280 KB |
Output is correct |
23 |
Execution timed out |
1069 ms |
2048 KB |
Time limit exceeded |
24 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1280 KB |
Output is correct |
2 |
Correct |
3 ms |
1280 KB |
Output is correct |
3 |
Correct |
3 ms |
1280 KB |
Output is correct |
4 |
Correct |
3 ms |
1280 KB |
Output is correct |
5 |
Correct |
3 ms |
1280 KB |
Output is correct |
6 |
Correct |
2 ms |
1280 KB |
Output is correct |
7 |
Correct |
3 ms |
1280 KB |
Output is correct |
8 |
Correct |
3 ms |
1280 KB |
Output is correct |
9 |
Correct |
3 ms |
1280 KB |
Output is correct |
10 |
Correct |
3 ms |
1280 KB |
Output is correct |
11 |
Correct |
3 ms |
1280 KB |
Output is correct |
12 |
Correct |
3 ms |
1280 KB |
Output is correct |
13 |
Correct |
3 ms |
1280 KB |
Output is correct |
14 |
Correct |
3 ms |
1280 KB |
Output is correct |
15 |
Correct |
3 ms |
1280 KB |
Output is correct |
16 |
Correct |
4 ms |
1280 KB |
Output is correct |
17 |
Correct |
4 ms |
1280 KB |
Output is correct |
18 |
Correct |
4 ms |
1280 KB |
Output is correct |
19 |
Correct |
3 ms |
1280 KB |
Output is correct |
20 |
Correct |
4 ms |
1280 KB |
Output is correct |
21 |
Correct |
4 ms |
1280 KB |
Output is correct |
22 |
Correct |
4 ms |
1280 KB |
Output is correct |
23 |
Execution timed out |
1069 ms |
2048 KB |
Time limit exceeded |
24 |
Halted |
0 ms |
0 KB |
- |