이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//Smaug never desolated!!
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const int MAXN = (int)1e5 + 3;
const int MAXS = (int)203;
const int MOD = (int)1e9 + 7;
const int infint = (int)1e9;
const ll inf = (ll)1e15;
int n, k, dp1[MAXN], dp2[MAXN];
vector<pair<int, int> > G[MAXN];
void dfs(int u, int p)
{
int childs = 0;
for (auto v : G[u])
if(v.first != p)
dfs(v.first, u), childs++;
if(childs == 0)
{
dp1[u] = 0, dp2[u] = -infint;
return;
}
//dp1
for (auto v : G[u])
if(v.first != p)
dp1[u] += max(dp1[v.first], dp2[v.first] + v.second);
int mx1 = -infint, mx2 = -infint, ans1 = 0, ans2 = 0;
for (auto v : G[u])
if(v.first != p)
if(dp1[v.first] + v.second - max(dp2[v.first] + v.second, dp1[v.first]) > mx1)
mx2 = mx1, ans2 = ans1, mx1 = dp1[v.first] + v.second - max(dp2[v.first] + v.second, dp1[v.first]), ans1 = v.first;
else
if(dp1[v.first] + v.second - max(dp2[v.first] + v.second, dp1[v.first]) > mx2)
mx2 = dp1[v.first] + v.second - max(dp2[v.first] + v.second, dp1[v.first]), ans2 = v.first;
dp1[u] += max(0, mx1 + mx2);
//dp2
int sum = 0;
for (auto v : G[u])
if(v.first != p)
sum += max(dp2[v.first] + v.second, dp1[v.first]);
for (auto v : G[u])
if(v.first != p)
dp2[u] = max(dp2[u], sum + dp1[v.first] + v.second - max(dp2[v.first] + v.second, dp1[v.first]));
return;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n;
for (int i = 0; i < n - 1; i++)
{
int u, v, w;
cin >> u >> v >> w;
G[u].push_back({v, w});
G[v].push_back({u, w});
}
dfs(1, -1);
cout << dp1[1];
}
컴파일 시 표준 에러 (stderr) 메시지
beads.cpp: In function 'void dfs(int, int)':
beads.cpp:30:5: warning: suggest explicit braces to avoid ambiguous 'else' [-Wdangling-else]
if(v.first != p)
^
beads.cpp:28:46: warning: variable 'ans2' set but not used [-Wunused-but-set-variable]
int mx1 = -infint, mx2 = -infint, ans1 = 0, ans2 = 0;
^~~~
# | 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... |