이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
#define int long long
int inf=1e9;
struct wi{
vector<int> Q;
long long dp[4]={0,0,0,0};///0->srodek i zost, 1->srodek i gora, 2->pocz, 3-> kon
long long do_ojca=0;
}*w;
void dfs(int x,int ojc)
{
if(w[x].Q.size()==2 && ojc!=0)
return;
long long a=-inf,b=-inf,c=-inf,d=-inf,e=-inf;
int gdzie1,gdzie2,skad1,skad2;
long long suma=0;
for(int i=0;i<w[x].Q.size();i+=2)
{
int pom=w[x].Q[i];
int koszt=w[x].Q[i+1];
if(pom==ojc)
continue;
w[pom].do_ojca=koszt;
dfs(pom,x);
long long teraz=max(w[pom].dp[0],w[pom].dp[2]);
suma+=teraz;
long long srodek=max(w[pom].dp[1],w[pom].dp[3])-teraz;
if(srodek>e)
e=srodek;
long long bez=w[pom].dp[1]+koszt-teraz;
if(bez>a)
{
gdzie2=gdzie1;
b=a;
gdzie1=pom;
a=bez;
}
else if(bez>b)
{
gdzie2=pom;
b=bez;
}
long long bez_sr=w[pom].dp[0]+koszt-teraz;
if(bez_sr>c)
{
skad2=skad1;
d=c;
skad1=pom;
c=bez_sr;
}
else if(bez_sr>d)
{
skad2=pom;
d=bez_sr;
}
}
w[x].dp[0]=suma;
w[x].dp[1]=suma+max((long long)0,e);
w[x].dp[1]=max(w[x].dp[1],suma+c+d);
if(gdzie1!=skad1)
w[x].dp[1]=max(w[x].dp[1],suma+a+c);
else{
w[x].dp[1]=max(w[x].dp[1],suma+a+d);
w[x].dp[1]=max(w[x].dp[1],suma+b+c);
}
w[x].dp[2]=suma+c+w[x].do_ojca;
w[x].dp[3]=suma+a+w[x].do_ojca;
}
int32_t main()
{
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(0);
int n,x,y,z;
cin>>n;
w=new wi[n+3];
for(int i=1;i<n;i++)
{
cin>>x>>y>>z;
w[x].Q.push_back(y);
w[x].Q.push_back(z);
w[y].Q.push_back(x);
w[y].Q.push_back(z);
}
dfs(1,0);
//for(int i=1;i<=n;i++)
//cout<<i<<": "<<w[i].dp[0]<<" "<<w[i].dp[1]<<" "<<w[i].dp[2]<<" "<<w[i].dp[3]<<"\n";
//cout<<w[1].dp[0]<<" "<<w[1].dp[1]<<" "<<w[1].dp[2]<<" "<<w[1].dp[3]<<"\n";
cout<<max(w[1].dp[0],w[1].dp[1]);
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
beads.cpp: In function 'void dfs(long long int, long long int)':
beads.cpp:19:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
19 | for(int i=0;i<w[x].Q.size();i+=2)
| ~^~~~~~~~~~~~~~
beads.cpp:17:16: warning: variable 'gdzie2' set but not used [-Wunused-but-set-variable]
17 | int gdzie1,gdzie2,skad1,skad2;
| ^~~~~~
beads.cpp:17:29: warning: variable 'skad2' set but not used [-Wunused-but-set-variable]
17 | int gdzie1,gdzie2,skad1,skad2;
| ^~~~~
beads.cpp:62:5: warning: 'skad1' may be used uninitialized in this function [-Wmaybe-uninitialized]
62 | if(gdzie1!=skad1)
| ^~
beads.cpp:62:5: warning: 'gdzie1' may be used uninitialized in this function [-Wmaybe-uninitialized]
# | 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... |