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>
#define gibon ios::sync_with_stdio(false); cin.tie(0);
#define bp __builtin_popcount
#define fir first
#define sec second
#define pii pair<int, int>
#define pll pair<ll, ll>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
typedef long long ll;
using namespace std;
int dx[4]={0, 1, 0, -1}, dy[4]={1, 0, -1 , 0};
const int mxN=200010;
const int mxM=7300000;
const int mxK=100001;
const int MOD=1000000007;
const ll INF=9223372036854775807;
int N;
vector <pii> v[mxN];
vector <pii> adj[mxN];
ll par[mxN];
ll dp1[mxN], dp2[mxN];
typedef struct cmp1{
bool operator()(ll a, ll b)
{
return a>b;
}
}cmp1;
void dfs(int now, int pre)
{
priority_queue <ll> pq;
ll sum1=0;
for(pii nxt : v[now]) if(nxt.fir!=pre)
{
par[nxt.fir]=nxt.sec;
dfs(nxt.fir, now);
sum1+=dp1[nxt.fir];
pq.push(dp2[nxt.fir]+nxt.sec-dp1[nxt.fir]);
}
ll tmp1, tmp2;
if(pq.empty())
{
dp2[now]=dp1[now]=0;
return;
}
if(pq.size()==1)
{
dp2[now]=sum1;
dp1[now]=max(dp2[now], sum1+par[now]+pq.top());
return;
}
//assert(pq.size()==2);
tmp1=pq.top();
pq.pop();
tmp2=pq.top();
pq.pop();
//printf("tmp1=%lld, tmp2=%lld\n", tmp1, tmp2);
dp2[now]=sum1+tmp1+tmp2;
dp1[now]=max(dp2[now], sum1+par[now]+tmp1);
}
int main()
{
gibon
cin >> N;
if(N<=2)
{
cout << 0;
return 0;
}
for(int i=1;i<N;i++)
{
int a, b, c;
cin >> a >> b >> c;
v[a].push_back({b, c});
v[b].push_back({a, c});
}
dfs(3, -1);
cout << dp2[3];
}
# | 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... |