이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
#include <algorithm>
#include <stdio.h>
#define MAX_N 200000
#define INF 2000000000000000LL
using namespace std;
typedef long long ll;
int N;
int ans;
struct S{
S (int idx, ll data) : idx(idx), data(data) {}
int idx;ll data;
};
vector<S> gp[MAX_N+1];
vector<S> p;
ll dp[MAX_N+1][4];
ll sum = 0;
ll mx1 = 0, mx2 = 0;
bool tf;
void dfs(int x){
for(int i=0; i<gp[x].size(); i++){
if(gp[x][i].idx!=p[x].idx){
p[gp[x][i].idx].idx = x;
p[gp[x][i].idx].data = gp[x][i].data;
dfs(gp[x][i].idx);
}
}
sum = 0;
for(int i=0; i<gp[x].size(); i++){
if(gp[x][i].idx==p[x].idx) continue;
int now = gp[x][i].idx;
dp[x][0] += max(dp[now][0], max(dp[now][1], dp[now][2]));
//dp[x][1] += max(dp[now][0], max(dp[now][1], dp[now][3]));
sum += max(dp[gp[x][i].idx][0], max(dp[gp[x][i].idx][1], dp[gp[x][i].idx][3]));
}
dp[x][1] = dp[x][2] = dp[x][3] = -INF;
tf = true;
for(int i=0; i<gp[x].size(); i++){
if(gp[x][i].idx==p[x].idx) continue;
if(tf){
tf = false;
mx2 = dp[gp[x][i].idx][0] + gp[x][i].data - max(dp[gp[x][i].idx][0], max(dp[gp[x][i].idx][1], dp[gp[x][i].idx][3]));
mx1 = dp[gp[x][i].idx][1] + gp[x][i].data - max(dp[gp[x][i].idx][0], max(dp[gp[x][i].idx][1], dp[gp[x][i].idx][3]));
mx1 = max(mx1, mx2);
}else{
dp[x][1] = max(dp[x][1], sum + mx1 + gp[x][i].data + dp[gp[x][i].idx][0] - max(dp[gp[x][i].idx][0], max(dp[gp[x][i].idx][1], dp[gp[x][i].idx][3])));
dp[x][1] = max(dp[x][1], sum + mx2 + gp[x][i].data + dp[gp[x][i].idx][1] - max(dp[gp[x][i].idx][0], max(dp[gp[x][i].idx][1], dp[gp[x][i].idx][3])));
mx2 = max(mx2, dp[gp[x][i].idx][0] + gp[x][i].data - max(dp[gp[x][i].idx][0], max(dp[gp[x][i].idx][1], dp[gp[x][i].idx][3])));
mx1 = max(mx1, dp[gp[x][i].idx][1] + gp[x][i].data - max(dp[gp[x][i].idx][0], max(dp[gp[x][i].idx][1], dp[gp[x][i].idx][3])));
mx1 = max(mx2, mx1);
}
dp[x][2] = max(dp[x][2], dp[gp[x][i].idx][1] + sum - max(dp[gp[x][i].idx][0], max(dp[gp[x][i].idx][1], dp[gp[x][i].idx][3])) + gp[x][i].data + p[x].data);
dp[x][3] = max(dp[x][3], dp[gp[x][i].idx][0] + sum - max(dp[gp[x][i].idx][0], max(dp[gp[x][i].idx][1], dp[gp[x][i].idx][3])) + gp[x][i].data + p[x].data);
dp[x][2] = max(dp[x][2], dp[x][3]);
}
//cout<<x<<' '<<dp[x][0]<<' '<<dp[x][1]<<' '<<dp[x][2]<<' '<<dp[x][3]<<endl;
return;
}
int main(){
scanf("%d", &N);
for(int i=0; i<=N; i++) p.push_back({0, 0});
for(int i=0; i<N-1; i++){
int a, b;ll c;
scanf("%d%d%lld", &a, &b, &c);
gp[a].push_back({b, c}); gp[b].push_back({a, c});
}
dfs(1);
printf("%lld", max(dp[1][0], dp[1][1]));
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
beads.cpp: In function 'void dfs(int)':
beads.cpp:25:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<gp[x].size(); i++){
~^~~~~~~~~~~~~
beads.cpp:33:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<gp[x].size(); i++){
~^~~~~~~~~~~~~
beads.cpp:42:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<gp[x].size(); i++){
~^~~~~~~~~~~~~
beads.cpp: In function 'int main()':
beads.cpp:66:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &N);
~~~~~^~~~~~~~~~
beads.cpp:70:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%lld", &a, &b, &c);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |