이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//雪花飄飄北風嘯嘯
//天地一片蒼茫
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#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());
struct custom_hash {
typedef unsigned long long ull;
const ull FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
static ull splitmix64(ull x) {
// http://xorshift.di.unimi.it/splitmix64.c
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(ll x) const {
return splitmix64(x + FIXED_RANDOM);
}
size_t operator()(const pair<int,int> &i)const{
return splitmix64((((ll)i.first)^(((ll)i.second)<<32))+FIXED_RANDOM);
}
};
int n;
vector<ii> al[200005];
unordered_map<ii,ii,custom_hash> memo;
unordered_map<ii,ll,custom_hash> w;
ii s[200005];
int pa[200005];
ll tot[200005];
ii dp(int i,int p){
if (memo.count(ii(i,p))) return memo[ii(i,p)];
ii res;
if (!pa[i]){
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;
ll vv=temp.fi+it.se-val;
if (s[i].fi<vv){
s[i].se=s[i].fi;
s[i].fi=vv;
}
else if (s[i].se<vv){
s[i].se=vv;
}
}
res=ii(tot[i],tot[i]+s[i].fi);
pa[i]=p;
}
else{
if (pa[i]!=-1){
auto temp=dp(pa[i],i);
ll ww=w[ii(i,pa[i])];
ll val=max(temp.fi,temp.se+ww);
tot[i]+=val;
ll vv=temp.fi+ww-val;
if (s[i].fi<vv){
s[i].se=s[i].fi;
s[i].fi=vv;
}
else if (s[i].se<vv){
s[i].se=vv;
}
pa[i]=-1;
}
ii temp;
ll ww;
ll val;
ll vv;
if (p!=-1){
temp=dp(p,i);
ww=w[ii(i,p)];
val=max(temp.fi,temp.se+ww);
tot[i]-=val;
vv=temp.fi+ww-val;
}
res=ii(tot[i],tot[i]+(s[i].fi==vv?s[i].se:s[i].fi));
if (p!=-1){
tot[i]+=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);
memo.reserve(4096);
memo.max_load_factor(0.25);
w.reserve(4096);
w.max_load_factor(0.25);
rep(x,0,200005) s[x]=ii(-1e9,-1e9);
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;
}
컴파일 시 표준 에러 (stderr) 메시지
beads.cpp: In function 'std::pair<long long int, long long int> dp(int, int)':
beads.cpp:118:36: warning: 'vv' may be used uninitialized in this function [-Wmaybe-uninitialized]
118 | res=ii(tot[i],tot[i]+(s[i].fi==vv?s[i].se:s[i].fi));
| ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
# | 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... |