# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
731172 | ReLice | Beads and wires (APIO14_beads) | C++14 | 1 ms | 212 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define pb push_back
#define endl "\n"
#define fr first
#define sc second
#define sz size()
#define bc back()
using namespace std;
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<ll, null_type,less_equal<ll>, rb_tree_tag,tree_order_statistics_node_update>
void fre(string s){freopen((s+".in").c_str(),"r",stdin);freopen((s+".out").c_str(),"w",stdout);}*/
void start(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
const ll inf=1e9+7;
const ll mod=1e9+7;
const ll N=1e3+7;
vector <vector <ll>> g(N);
ll d[N];
map<ll,ll> mp;
map<pair<ll,ll>,ll> w;
void dfs(ll v,ll pr){
for(auto i : g[v]){
if(i==pr) continue;
d[i]=d[v]+1;
dfs(i,v);
}
}
void solve(){
ll i,j,q,sum=0,ans=inf,a,b,mx=-1,m,t;
ll n,k;
cin>>n;
vector<pair<ll,pair<ll,ll>>> v;
for(i=1;i<n;i++){
ll c;
cin>>a>>b>>c;
sum+=c;
g[a].pb(b);
g[b].pb(a);
w[{a,b}]=w[{b,a}]=c;
v.pb({c,{a,b}});
}
for(i=1;i<=n;i++){
if(g[i].sz==1) mp[i]++;
}
sort(v.begin(),v.end());
for(i=0;i<n-1;i++){
ll c=0,c2=0,mn=inf,mn2=inf;
for(auto j : g[v[i].sc.fr]){
if(j==v[i].sc.sc) continue;
if(mp[j]>0){
mn=min(mn,w[{v[i].sc.fr,j}]);
c++;
}
}
for(auto j : g[v[i].sc.sc]){
if(j==v[i].sc.fr) continue;
if(mp[j]>0){
mn2=min(mn2,w[{v[i].sc.sc,j}]);
c2++;
}
}
if(c%2==0 && c2%2==0){
ans=min(ans,v[i].fr);
}
else if(c%2==0){
ans=min(ans,v[i].fr+mn2);
}
else if(c2%2==0){
ans=min(ans,v[i].fr+mn);
}
else{
ans=min(ans,v[i].fr+mn+mn2);
}
}
cout<<sum-ans<<endl;
}
signed main(){
//start();
//fre("");
ll t=1;
//cin>>t;
while(t--) solve();
}
Compilation message (stderr)
# | 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... |