이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
// why am I so weak
int n;
struct edge {int to; long long cost;};
vector<edge> g[200055];
inline void add_edge(int from, int to, int cost) {
g[from].push_back((edge){to, cost});
}
long long dp[200055][2];
void dfs(int v, int par = -1) {
dp[v][0] = 0;
dp[v][1] = -(long long)1e13;
// one dp[e.to][0] + e.cost
long long sum = 0ll;
for (auto e : g[v]) if (e.to != par) {
dfs(e.to, v);
dp[v][0] += max(dp[e.to][0], dp[e.to][1] + e.cost);
sum += max(dp[e.to][0], dp[e.to][1] + e.cost);
}
for (auto e : g[v]) if (e.to != par) {
dp[v][1] = max(dp[v][1], sum - max(dp[e.to][0], dp[e.to][1] + e.cost) + dp[e.to][0] + e.cost);
}
}
struct dat {long long dat[2];};
long long rdfs(int v, int par, dat ac, long long cost) {
long long res = dp[v][0] + max(ac.dat[0], ac.dat[1] + cost);
dat cool;
cool.dat[0] = 0;
cool.dat[1] = -(long long)1e13;
multiset<long long> s;
long long sum = 0;
for (auto e : g[v]) if (e.to != par) {
cool.dat[0] += max(dp[e.to][0], dp[e.to][1] + e.cost);
sum += max(dp[e.to][0], dp[e.to][1] + e.cost);
s.insert(-max(dp[e.to][0], dp[e.to][1] + e.cost) + dp[e.to][0] + e.cost);
}
cool.dat[0] += max(ac.dat[0], ac.dat[1] + cost);
sum += max(ac.dat[0], ac.dat[1] + cost);
s.insert(-max(ac.dat[0], ac.dat[1] + cost) + ac.dat[0] + cost);
for (auto e : g[v]) if (e.to != par) {
s.erase(s.find(-max(dp[e.to][0], dp[e.to][1] + e.cost) + dp[e.to][0] + e.cost));
cool.dat[0] -= max(dp[e.to][0], dp[e.to][1] + e.cost);
cool.dat[1] = sum + *s.rbegin();
res = max(res, rdfs(e.to, v, cool, e.cost));
cool.dat[0] += max(dp[e.to][0], dp[e.to][1] + e.cost);
s.insert(-max(dp[e.to][0], dp[e.to][1] + e.cost) + dp[e.to][0] + e.cost);
}
return res;
}
int main() {
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int x, y, z;
scanf("%d %d %d", &x, &y, &z);
x--, y--;
add_edge(x, y, z);
add_edge(y, x, z);
}
dfs(0);
dat a;
a.dat[0] = 0;
a.dat[1] = -(long long)1e13;
long long res = 0ll;
for (int i = 0; i < n; i++) {
dfs(i);
res = max(res, dp[i][0]);
}
printf("%lld\n", res);
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
beads.cpp: In function 'int main()':
beads.cpp:91:6: warning: variable 'a' set but not used [-Wunused-but-set-variable]
dat a;
^
beads.cpp:78:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
beads.cpp:82:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d", &x, &y, &z);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |