# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1041850 | MMihalev | Beads and wires (APIO14_beads) | C++14 | 1075 ms | 1116 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<algorithm>
#include<iomanip>
#include<cmath>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<tuple>
#include<set>
#include<map>
#include<random>
#include<chrono>
#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
using namespace std;
const int MAX_N=1e4+1;
const int INF=2e9+1e8;
int dp[MAX_N][2];
int n;
vector<pair<int,int>>g[MAX_N];
int j;
void reset()
{
for(j=1;j<=n;++j)
{
dp[j][0]=0;
dp[j][1]=-INF;
}
}
void dfs(int u,int par)
{
int sum=0,cur;
for(auto [v,edge]:g[u])
{
if(v==par)continue;
dfs(v,u);
sum+=max(edge+dp[v][1],dp[v][0]);
}
for(auto [v,edge]:g[u])
{
if(v==par)continue;
cur=dp[v][0];
if(edge+dp[v][1]>cur)cur=edge+dp[v][1];
dp[u][1]=max(dp[u][1],sum-cur+edge+dp[v][0]);
}
dp[u][0]=sum;
}
signed main ()
{
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
cin>>n;
for(int i=1;i<n;i++)
{
int u,v,c;
cin>>u>>v>>c;
g[u].push_back({v,c});
g[v].push_back({u,c});
}
int ans=0;
int r;
for(r=1;r<=n;++r)
{
reset();
dfs(r,0);
ans=max(ans,dp[r][0]);
}
cout<<ans<<"\n";
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... |