이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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... |