이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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 + 69;
int n;
vector<ii> adj[MAX];
//DP[X][0]: vertex X, not using the edge from parent
//DP[X][1]: vertex X, using the edge from parent
int dp[MAX][2];
int cost[MAX];
void dfs(int node, int par, int parw)
{
int ctr = 0; //point for using 0 edge (X as centre)
vector<int> v; //bonus point for using the edge (must free the child's parent edge)
for (auto [to, w] : adj[node])
{
if (to != par)
{
dfs(to, node, w);
ctr += max(dp[to][0], dp[to][1]);
if (dp[to][0] >= dp[to][1])
v.pb(w);
else v.pb(dp[to][0] - dp[to][1] + w);
}
}
if (v.empty()) // a leaf
return;
sort(v.begin(), v.end(), greater<int>());
dp[node][0] = ctr;
dp[node][1] = ctr + v[0] + parw;
if (v.size() >= 2)
dp[node][0] = max(dp[node][0], ctr + v[0] + v[1]);
}
int main()
{
ios :: sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
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;
}
# | 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... |