이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int NMAX = 200000 + 5;
const int INF = 2E9 + 15;
int N;
vector <pair <int, int> > graph[NMAX];
int fatherEdgeCost[NMAX];
int dp[NMAX][2];
void dfs1(int node, int father) {
dp[node][0] = 0;
dp[node][1] = -INF;
for (auto it: graph[node])
if (it.first != father) {
dfs1(it.first, node);
int val = max(dp[it.first][0], dp[it.first][1] + it.second);
dp[node][0] += val;
dp[node][1] = max(dp[node][1], it.second + dp[it.first][0] - val);
}
dp[node][1] = dp[node][0] + dp[node][1];
}
int ans;
int getBestNotNode(const vector <pair <int, int> > &v, int node) {
for (int i = 0; i < v.size(); ++ i)
if (v[i].second != node)
return v[i].first;
return -INF;
}
int up[NMAX][2];
void dfs2(int node, int father, int edge) {
int cost = dp[node][0] + max(up[node][0], up[node][1] + edge);
ans = max(ans, cost);
vector <pair <int, int> > v;
if (father)
v.push_back(make_pair(up[node][0] + edge - max(up[node][0], up[node][1] + edge), father));
for (auto it: graph[node])
if (it.first != father) {
int val = max(dp[it.first][0], dp[it.first][1] + it.second);
up[it.first][0] = dp[node][0] - val + max(up[node][0], up[node][1] + edge);
v.push_back(make_pair(it.second + dp[it.first][0] - val, it.first));
}
sort(v.begin(), v.end(), greater <pair <int, int> >());
if (v.size() > 2)
v.resize(2);
for (auto it: graph[node])
if (it.first != father) {
int val = max(dp[it.first][0], dp[it.first][1] + it.second);
up[it.first][1] = cost + getBestNotNode(v, it.first) - val;
}
for (auto it: graph[node])
if (it.first != father)
dfs2(it.first, node, it.second);
}
int main()
{
//freopen("data.in", "r", stdin);
cin >> N;
for (int i = 1; i < N; ++ i) {
int a, b, c;
cin >> a >> b >> c;
graph[a].push_back({b, c});
graph[b].push_back({a, c});
}
dfs1(1, 0);
dfs2(1, 0, 0);
cout << ans << '\n';
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
beads.cpp: In function 'int getBestNotNode(const std::vector<std::pair<int, int> >&, int)':
beads.cpp:28:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < v.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... |