이 제출은 이전 버전의 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 dfs0(int now, int pre)
{
for(pii nxt : v[now]) if(nxt.fir!=pre)
{
dfs0(nxt.fir, now);
adj[now].push_back(nxt);
par[nxt.fir]=nxt.sec;
}
}
void dfs1(int now)
{
priority_queue <ll, vector<ll>, cmp1> pq;
ll sum1=0;
for(pii nxt : adj[now])
{
dfs1(nxt.fir);
sum1+=dp1[nxt.fir];
pq.push(dp2[nxt.fir]+nxt.sec-dp1[nxt.fir]);
if(pq.size()>=3) pq.pop();
}
ll tmp1, tmp2;
if(pq.empty())
{
dp1[now]=dp2[now]=0;
return;
}
if(pq.size()==1)
{
dp2[now]=sum1;
dp1[now]=max(dp2[now], sum1+par[now]+pq.top());
return;
}
tmp1=pq.top();
pq.pop();
tmp2=pq.top();
pq.pop();
dp2[now]=sum1+max((ll)0, tmp1+tmp2);
dp1[now]=max(dp2[now], sum1+par[now]+tmp2);
}
int main()
{
gibon
cin >> N;
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});
}
dfs0(1, -1);
dfs1(1);
cout << dp2[1];
}
# | 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... |