This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*huypheu
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
*/
#include <bits/stdc++.h>
#define int long long
#define oo (LLONG_MAX/2)
using namespace std;
int dp[200007][2];
vector <pair<int,int> > ch[200007],v[200007],vn;
int par[200007];
bool vst[200007];
void dfs(int u)
{
vst[u]=1;
for(int i=0;i<v[u].size();i++)
{
int k=v[u][i].first;
if(!vst[k])
{
ch[u].push_back(v[u][i]);
par[k]=u;
dfs(k);
}
}
return ;
}
void get_ans(int u)
{
for(int i=0;i<ch[u].size();i++)
{
int k=ch[u][i].first;
get_ans(k);
}
dp[u][1]=-oo;
int tot=0;
vector <int> jo;
for(int i=0;i<ch[u].size();i++)
{
int k=ch[u][i].first;
tot+=max(dp[k][0],dp[k][1]+ch[u][i].second);
jo.push_back(max(dp[k][0],dp[k][1]+ch[u][i].second));
}
for(int i=0;i<jo.size();i++)
{
int k=ch[u][i].first;
dp[u][1]=max(dp[u][1],tot-jo[i]+dp[k][0]+ch[u][i].second);
}
dp[u][0]=tot;
return ;
}
signed main()
{
int n;
cin >> n;
for(int i=1;i<n;i++)
{
int a,b,c;
cin >> a >> b >> c;
v[a].push_back(make_pair(b,c));
v[b].push_back(make_pair(a,c));
}
int totans=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++) vst[j]=0;
for(int j=1;j<=n;j++) ch[j]=vn;
for(int j=1;j<=n;j++) par[j]=0,dp[j][0]=0,dp[j][1]=0;
dfs(i);
get_ans(i);
// for(int j=1;j<=n;j++) cout << par[j] << " ";
// cout << endl;
// cout << dp[i][0] << endl;
totans=max(totans,dp[i][0]);
}
// for(int i=1;i<=n;i++) cout << dp[i][0] << " " << dp[i][1] << endl;
// cout << "concak" << endl;
cout << totans << endl;
return 0;
}
Compilation message (stderr)
beads.cpp: In function 'void dfs(long long int)':
beads.cpp:27:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<v[u].size();i++)
~^~~~~~~~~~~~
beads.cpp: In function 'void get_ans(long long int)':
beads.cpp:42:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<ch[u].size();i++)
~^~~~~~~~~~~~~
beads.cpp:50:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<ch[u].size();i++)
~^~~~~~~~~~~~~
beads.cpp:56:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<jo.size();i++)
~^~~~~~~~~~
# | 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... |