이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstring>
using namespace std;
int alln;
vector <pair<int,int> > E[200005];
int dp[200005][2][2][2];
int recur(int u,int ins,int wp,int can10,int pre){
if(dp[u][ins][wp][can10] >= 0)
return dp[u][ins][wp][can10];
//printf("--> dp infor %d %d %d %d %d\n",u,ins,wp,can10,pre);
if(E[u].size() <= 1)
return dp[u][ins][wp][can10] = 0;
if(ins == 1){
if(wp == 0 && E[u].size() <= 2 )
return dp[u][ins][wp][can10] = 0;
else if(wp == 1 && E[u].size() <= 1)
return dp[u][ins][wp][can10] = 0;
}
int sum = 0;
for(int i=0;i<E[u].size();++i){
int v = E[u][i].first;
if(v == pre)
continue;
int w = E[u][i].second;
sum += max( recur(v,0,0,0,u) , recur(v,1,1,0,u)+w*(E[v].size() > 1) ) ;
}
pair<int,int> t101,t102,f101,f102;
t101 = t102 = f101 = f102 = {0,0};
for(int i=0;i<E[u].size();++i){
int v = E[u][i].first;
if(v == pre)
continue;
int w = E[u][i].second;
if(can10 == true){
if(ins == 0){
int del = max( recur(v,0,0,0,u) , recur(v,1,1,0,u)+w*(E[v].size() > 1) );
int add = max( recur(v,0,0,can10,u) , max( recur(v,1,0,can10,u) , recur(v,1,1,can10,u)+w*(E[v].size() > 1) ) );
pair<int,int> tmp = { sum-del+add , v };
if(t101.first < tmp.first)
t102 = t101 , t101 = tmp;
else if(t102.first < tmp.first)
t102 = tmp;
}
else{
int del = max( recur(v,0,0,0,u) , recur(v,1,1,0,u)+w*(E[v].size() > 1) );
int add = max( recur(v,0,0,can10,u) , recur(v,1,0,can10,u) )+w;
//printf("-->1 uv %d %d val %d\n",u,v,sum-del+add);
pair<int,int> tmp = { sum-del+add , v };
if(t101.first < tmp.first)
t102 = t101 , t101 = tmp;
else if(t102.first < tmp.first)
t102 = tmp;
//printf("--> %d %d\n",t101.first , t102.first);
}
}
if(ins == 0){
int del = max( recur(v,0,0,0,u) , recur(v,1,1,0,u)+w*(E[v].size() > 1) );
int add = max( recur(v,0,0,0,u) , recur(v,1,1,0,u)+w*(E[v].size() > 1) );
pair<int,int> tmp = { sum-del+add , v };
if(f101.first < tmp.first)
f102 = t101 , f101 = tmp;
else if(f102.first < tmp.first)
f102 = tmp;
}
else{
int del = max( recur(v,0,0,0,u) , recur(v,1,1,0,u)+w*(E[v].size() > 1) );
int add = recur(v,0,0,0,u) + w;
//printf("-->0 uv %d %d val %d\n\n",u,v,sum-del+add);
pair<int,int> tmp = { sum-del+add , v };
if(f101.first < tmp.first)
f102 = f101 , f101 = tmp;
else if(f102.first < tmp.first)
f102 = tmp;
//printf("--> %d %d\n",f101.first , f102.first);
}
}
if(ins == 0)
return dp[u][ins][wp][can10] = max( t101.first , f101.first );
if(can10){
if(wp)
return dp[u][ins][wp][can10] = t101.first;
if(t101.second != f101.second)
return dp[u][ins][wp][can10] = t101.first + f101.first -sum;
//printf("*** %d %d\n",t101.first+f102.first , t102.first+f101.first);
return dp[u][ins][wp][can10] = max(t101.first+f102.first , t102.first+f101.first)-sum;
}
else{
if(wp)
return dp[u][ins][wp][can10] = f101.first;
return dp[u][ins][wp][can10] = f101.first+f102.first - sum;
}
}
int main()
{
memset(dp,-1,sizeof(dp));
scanf("%d",&alln);
for(int i=1;i<alln;++i){
int u,v;
int w;
scanf("%d%d%d",&u,&v,&w);
E[u].push_back({v,w});
E[v].push_back({u,w});
}
int root = 1;
E[root].push_back({0,0});
printf("%d\n",max( recur(root,0,0,1,0) , recur(root,1,0,1,0) ) );
//printf("--> %d\n",recur(root,1,0,1,0));
return 0;
}
/**
5
1 2 10
1 3 40
1 4 15
1 5 20
10
4 10 2
1 2 21
1 3 13
6 7 1
7 9 5
2 4 3
2 5 8
1 6 55
6 8 34
6
1 2 15
2 3 35
3 4 20
4 5 30
5 6 25
*/
컴파일 시 표준 에러 (stderr) 메시지
beads.cpp: In function 'int recur(int, int, int, int, int)':
beads.cpp:24:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<E[u].size();++i){
~^~~~~~~~~~~~
beads.cpp:35:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<E[u].size();++i){
~^~~~~~~~~~~~
beads.cpp: In function 'int main()':
beads.cpp:105:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&alln);
~~~~~^~~~~~~~~~~~
beads.cpp:109:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d",&u,&v,&w);
~~~~~^~~~~~~~~~~~~~~~~~~
# | 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... |