이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
#include <algorithm>
#include <assert.h>
using namespace std;
#define int long long
#define pb push_back
#define ii pair<int, int>
#define f1 first
#define s2 second
const int MAX = 2e5 + 7;
const int inf = 1e9 + 7;
//dp[x][0]: do not use node x as center
//dp[x][1]: used the edge to the parent of node x
//dp[x][2]: used two edges to the child of node x (all graph)
//dp[x][3]: used two edges to the child of node x, and used the edge to the parent of node x
int dp[MAX][4];
vector<ii> adj[MAX];
int getLeaf(int node, int par)
{
for (ii to : adj[node])
if (to.f1 != par)
return getLeaf(to.f1, node);
return node;
}
void dfs(int node, int par, int par_val)
{
vector<int> v[4];
vector<int> edge;
for (ii to : adj[node])
{
if (to.f1 != par)
{
dfs(to.f1, node, to.s2);
for (int i = 0; i < 4; ++i)
v[i].pb(dp[to.f1][i]);
edge.pb(to.s2);
}
}
if (v[0].empty())
return;
//not using node as center
{
for (int i = 0; i < v[0].size(); ++i)
dp[node][0] += max(v[0][i], v[1][i]);
}
//using the edge to the parent
{
int ctr = 0;
int mx = -inf;
for (int i = 0; i < v[0].size(); ++i)
{
ctr += max(v[0][i], v[1][i]);
if (v[0][i] <= v[1][i])
mx = max(mx, edge[i] + v[0][i] - v[1][i]);
}
if (mx != -inf)
dp[node][1] = ctr + mx + par_val;
}
//using a split
{
int ctr = 0;
vector<int> val;
for (int i = 0; i < v[0].size(); ++i)
{
dp[node][2] += max(max(v[0][i], v[1][i]), max(v[2][i], v[3][i]));
ctr += max(v[0][i], v[1][i]);
if (v[0][i] <= v[1][i])
val.pb(edge[i] + v[0][i] - v[1][i]);
}
sort(val.begin(), val.end(), greater<int>());
if (val.size() >= 2)
dp[node][2] = max(dp[node][2], ctr + val[0] + val[1]);
}
//there has been a split, now uses the edge to the parent
{
int ctr = 0;
int mx = -inf;
for (int i = 0; i < v[0].size(); ++i)
{
ctr += max(v[2][i], v[3][i]);
if (v[2][i] <= v[3][i])
mx = max(mx, edge[i] + v[2][i] - v[3][i]);
}
if (mx != -inf)
dp[node][3] = ctr + mx + par_val;
}
//cerr << node << "==> ";
//for (int i = 0; i < 4; ++i)
// cerr << dp[node][i] << (i == 3 ? '\n' : ' ');
}
int32_t 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});
}
int ROOT = getLeaf(1, -1);
dfs(ROOT, -1, -1);
cout << max(dp[ROOT][0], dp[ROOT][2]) << '\n';
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
beads.cpp: In function 'void dfs(long long int, long long int, long long int)':
beads.cpp:55:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
55 | for (int i = 0; i < v[0].size(); ++i)
| ~~^~~~~~~~~~~~~
beads.cpp:63:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | for (int i = 0; i < v[0].size(); ++i)
| ~~^~~~~~~~~~~~~
beads.cpp:79:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
79 | for (int i = 0; i < v[0].size(); ++i)
| ~~^~~~~~~~~~~~~
beads.cpp:97:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
97 | for (int i = 0; i < v[0].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... |