# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
104276 | Kastanda | Beads and wires (APIO14_beads) | C++11 | 7 ms | 4992 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// Fallen
#include<bits/stdc++.h>
#define pb push_back
#define x first
#define y second
using namespace std;
const int N = 200005;
int n, dp[N][2];
vector < pair < int , int > > Adj[N];
void DFS(int v, int p, int w)
{
int sum = 0, mn[2] = {INT_MAX, INT_MAX};
for (auto &u : Adj[v])
if (u.x != p)
{
DFS(u.x, v, u.y);
sum += dp[u.x][0];
if (mn[1] > dp[u.x][0] - dp[u.x][1] - u.y)
mn[1] = dp[u.x][0] - dp[u.x][1] - u.y;
if (mn[0] > mn[1])
swap(mn[0], mn[1]);
}
dp[v][1] = max(1LL * sum, 1LL * sum - mn[0] - mn[1]);
dp[v][0] = max(1LL * dp[v][1], 1LL * sum - mn[0] + w);
}
int main()
{
scanf("%d", &n);
for (int i = 1; i < n; i++)
{
int v, u, w;
scanf("%d%d%d", &v, &u, &w);
Adj[v].pb({u, w});
Adj[u].pb({v, w});
}
DFS(1, 0, 0);
return !printf("%d", dp[1][1]);
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |