Submission #298754

#TimeUsernameProblemLanguageResultExecution timeMemory
298754errorgornBeads and wires (APIO14_beads)C++14
57 / 100
1065 ms121164 KiB
//雪花飄飄北風嘯嘯 //天地一片蒼茫 #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,int,custom_hash> 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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...