이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 2e5+1;
const ll INF = 1e12+1;
int n;
ll dp[N][2][2];
vector<pair<int,int> > g[N];
void dfs(int u, int par) {
for(auto p: g[u]) if(p.first != par) dfs(p.first, u);
vector<int> v, w, vals;
for(auto p: g[u]) if(p.first != par) {
v.push_back(p.first);
w.push_back(p.second);
vals.push_back(max(dp[p.first][1][0] + p.second, dp[p.first][0][0]));
}
//init
dp[u][0][1] = dp[u][1][0] = dp[u][1][1] = -INF;
// 00
for(int i = 0; i < v.size(); i++) dp[u][0][0] += vals[i];
//01
ll mx = -INF;
for(int i = 0; i < v.size(); i++) mx = max(max(dp[v[i]][0][1], dp[v[i]][1][1] + w[i]) - vals[i], mx);
dp[u][0][1] = dp[u][0][0] + mx;
mx = -INF;
for(int i = 0; i < v.size(); i++) {
dp[u][0][1] = max(dp[u][0][0] + dp[v[i]][0][0] + w[i] - vals[i] + mx, dp[u][0][1]);
mx = max(max(dp[v[i]][0][0], dp[v[i]][0][1]) + w[i] - vals[i], mx);
}
mx = -INF;
for(int i = v.size()-1; i >= 0; i--) {
dp[u][0][1] = max(dp[u][0][0] + dp[v[i]][0][0] + w[i] - vals[i] + mx, dp[u][0][1]);
mx = max(max(dp[v[i]][0][0], dp[v[i]][0][1]) + w[i] - vals[i], mx);
}
//10
mx = -INF;
for(int i = 0; i < v.size(); i++) mx = max(dp[v[i]][0][0] + w[i] - vals[i], mx);
dp[u][1][0] = dp[u][0][0] + mx;
//11
mx = -INF;
for(int i = 0; i < v.size(); i++) mx = max(dp[v[i]][0][1] + w[i] - vals[i], mx);
dp[u][1][1] = dp[u][0][0] + mx;
mx = -INF;
for(int i = 0; i < v.size(); i++) {
dp[u][1][1] = max(dp[u][0][0] + dp[v[i]][1][0] + w[i] - vals[i] + mx, dp[u][1][1]);
mx = max(dp[v[i]][0][1] - vals[i], mx);
}
// cout << u << ' ' << dp[u][0][0] << ' ' << dp[u][0][1] << ' ' << dp[u][1][0] << ' ' << dp[u][1][1] << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for(int i = 1, u, v, w; i < n; i++) {
cin >> u >> v >> w;
g[u].emplace_back(v, w);
g[v].emplace_back(u, w);
}
dfs(1, 0);
cout << max(dp[1][0][0], dp[1][0][1]);
}
컴파일 시 표준 에러 (stderr) 메시지
beads.cpp: In function 'void dfs(int, int)':
beads.cpp:23:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < v.size(); i++) dp[u][0][0] += vals[i];
~~^~~~~~~~~~
beads.cpp:27:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < v.size(); i++) mx = max(max(dp[v[i]][0][1], dp[v[i]][1][1] + w[i]) - vals[i], mx);
~~^~~~~~~~~~
beads.cpp:30:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < v.size(); i++) {
~~^~~~~~~~~~
beads.cpp:42:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < v.size(); i++) mx = max(dp[v[i]][0][0] + w[i] - vals[i], mx);
~~^~~~~~~~~~
beads.cpp:47:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < v.size(); i++) mx = max(dp[v[i]][0][1] + w[i] - vals[i], mx);
~~^~~~~~~~~~
beads.cpp:50:19: 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... |