# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
570487 | groshi | Beads and wires (APIO14_beads) | C++17 | 183 ms | 32608 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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;
}
Compilation message (stderr)
# | 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... |