이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//雪花飄飄北風嘯嘯
//天地一片蒼茫
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define ll long long
#define ii pair<ll,ll>
#define iii pair<ii,ll>
#define fi first
#define se second
#define endl '\n'
#define debug(x) cout << #x << " is " << x << endl
#define rep(x,start,end) for(auto x=(start)-((start)>(end));x!=(end)-((start)>(end));((start)<(end)?x++:x--))
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define indexed_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
//change less to less_equal for non distinct pbds, but erase will bug
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
int n;
vector<ii> al[200005];
map<ii,ii> memo;
map<ii,int> w;
multiset<int,greater<int> > s[200005];
int pa[200005];
int tot[200005];
ii dp(int i,int p){
if (memo.count(ii(i,p))) return memo[ii(i,p)];
ii res;
if (!pa[i]){
s[i].insert(-1e9);
for (auto &it:al[i]){
if (it.fi==p) continue;
auto temp=dp(it.fi,i);
ll val=max(temp.fi,temp.se+it.se);
tot[i]+=val;
s[i].insert(temp.fi+it.se-val);
}
res=ii(tot[i],tot[i]+*s[i].begin());
pa[i]=p;
}
else{
if (pa[i]!=-1){
auto temp=dp(pa[i],i);
ll val=max(temp.fi,temp.se+w[ii(i,pa[i])]);
tot[i]+=val;
s[i].insert(temp.fi+w[ii(i,pa[i])]-val);
pa[i]=-1;
}
ii temp;
int ww;
ll val;
if (p!=-1){
temp=dp(p,i);
ww=w[ii(i,p)];
val=max(temp.fi,temp.se+w[ii(i,p)]);
tot[i]-=val;
s[i].erase(s[i].find(temp.fi+ww-val));
}
res=ii(tot[i],tot[i]+*s[i].begin());
if (p!=-1){
val=max(temp.fi,temp.se+ww);
tot[i]+=val;
s[i].insert(temp.fi+ww-val);
}
}
return memo[ii(i,p)]=res;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin.exceptions(ios::badbit | ios::failbit);
cin>>n;
int a,b,c;
rep(x,1,n){
cin>>a>>b>>c;
al[a].push_back(ii(b,c));
al[b].push_back(ii(a,c));
w[ii(a,b)]=c;
w[ii(b,a)]=c;
}
ll ans=0;
rep(x,1,n+1) ans=max(ans,dp(x,-1).fi);
cout<<ans<<endl;
}
# | 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... |