# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1110840 | simona1230 | Beads and wires (APIO14_beads) | C++17 | 1080 ms | 1104 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 <bits/stdc++.h>
using namespace std;
int n;
vector<pair<int,int> > v[10001];
void read()
{
cin>>n;
for(int i=1;i<n;i++)
{
int x,y,z;
cin>>x>>y>>z;
v[x].push_back({y,z});
v[y].push_back({x,z});
}
}
int dp[10001][2];
void dfs(int i,int p)
{
dp[i][0]=0;
int minn=1e9;
for(int j=0;j<v[i].size();j++)
{
int nb=v[i][j].first;
int dist=v[i][j].second;
if(nb==p)continue;
dfs(nb,i);
int h=max(dp[nb][0],dp[nb][1]+dist);
dp[i][0]+=h;
minn=min(minn,h-dp[nb][0]-dist);
}
dp[i][1]=dp[i][0]-minn;
if(v[i].size()==1)dp[i][1]=-1e9;
//cout<<i<<" "<<dp[i][0]<<" "<<dp[i][1]<<endl;
}
void solve()
{
int ans=0;
for(int i=1;i<=n;i++)
{
//cout<<"! "<<i<<endl;
dfs(i,-1);
ans=max(ans,dp[i][0]);
}
cout<<ans<<'\n';
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
read();
solve();
return 0;
}
/*
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
*/
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... |