이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
#include <algorithm>
#include <assert.h>
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
bool vst[MAX];
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;
vst[node] = true;
for (ii to : adj[node])
{
if (to.f1 != par)
{
if (vst[to.f1])
assert(0);
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;
}
컴파일 시 표준 에러 (stderr) 메시지
beads.cpp: In function 'void dfs(int, int, int)':
beads.cpp:45:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | for (int i = 0; i < v0.size(); ++i)
| ~~^~~~~~~~~~~
beads.cpp:50:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
50 | for (int i = 0; i < v0.size(); ++i)
| ~~^~~~~~~~~~~
# | 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... |