#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define pb push_back
#define ii pair<int, int>
#define f1 first
#define s2 second
const int MAX = 2e5 + 7;
//dp[x][0]: do not use the edge to the parent of node x
//dp[x][1]: used the edge to the parent of node x
int dp[MAX][2];
vector<ii> adj[MAX];
void dfs(int node, int par, int par_val)
{
vector<int> v0;
vector<int> v1;
vector<int> edge;
for (ii to : adj[node])
{
if (to.f1 != par)
{
dfs(to.f1, node, to.s2);
v0.pb(dp[to.f1][0]);
v1.pb(dp[to.f1][1]);
edge.pb(to.s2);
}
}
//not using node as center
for (int i = 0; i < v0.size(); ++i)
dp[node][0] += max(v0[i], v1[i]);
//using two edges
vector<int> val;
for (int i = 0; i < v0.size(); ++i)
{
if (v0[i] >= v1[i]) val.pb(edge[i]);
else val.pb(edge[i] + v0[i] - v1[i]);
}
sort(val.begin(), val.end(), greater<int>());
if (val.size() >= 1)
dp[node][1] = dp[node][0] + val[0] + par_val;
if (val.size() >= 2)
dp[node][0] = max(dp[node][0], dp[node][0] + val[0] + val[1]);
}
int main()
{
ios :: sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for (int i = 0; i < n-1; ++i)
{
int u, v, w;
cin >> u >> v >> w;
adj[u].pb({v, w});
adj[v].pb({u, w});
}
dfs(1, -1, -1);
cout << dp[1][0] << '\n';
return 0;
}
Compilation message
beads.cpp: In function 'void dfs(int, int, int)':
beads.cpp:36:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
36 | for (int i = 0; i < v0.size(); ++i)
| ~~^~~~~~~~~~~
beads.cpp:41:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | for (int i = 0; i < v0.size(); ++i)
| ~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
3 |
Correct |
5 ms |
5100 KB |
Output is correct |
4 |
Correct |
4 ms |
5248 KB |
Output is correct |
5 |
Correct |
4 ms |
5100 KB |
Output is correct |
6 |
Incorrect |
4 ms |
5100 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
3 |
Correct |
5 ms |
5100 KB |
Output is correct |
4 |
Correct |
4 ms |
5248 KB |
Output is correct |
5 |
Correct |
4 ms |
5100 KB |
Output is correct |
6 |
Incorrect |
4 ms |
5100 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
3 |
Correct |
5 ms |
5100 KB |
Output is correct |
4 |
Correct |
4 ms |
5248 KB |
Output is correct |
5 |
Correct |
4 ms |
5100 KB |
Output is correct |
6 |
Incorrect |
4 ms |
5100 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5100 KB |
Output is correct |
2 |
Correct |
4 ms |
5100 KB |
Output is correct |
3 |
Correct |
5 ms |
5100 KB |
Output is correct |
4 |
Correct |
4 ms |
5248 KB |
Output is correct |
5 |
Correct |
4 ms |
5100 KB |
Output is correct |
6 |
Incorrect |
4 ms |
5100 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |