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 mp make_pair
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(),x.end()
#define MAXN 200005
typedef long long ll;
using namespace std;
ll n,res,res2;
vector<pair<ll,pair<ll,ll> > > niz[MAXN];
ll dubina[MAXN];
ll predak[MAXN][21];
ll dodaj[MAXN],oduzmi[MAXN];
void lca(ll a,ll b){
if(dubina[b]>dubina[a])swap(a,b);
ll pa=a,pb=b;
for(ll i=20;i>=0;i--){
if(dubina[predak[a][i]]>=dubina[b]){
a=predak[a][i];
}
}
if(a==b){
a=pa;
for(ll i=20;i>=0;i--){
if(dubina[predak[a][i]]>dubina[b]){
a=predak[a][i];
}
}
dodaj[a]++;
oduzmi[pa]++;
return;
}
//printf(" %lld %lld %lld %lld\n",a,pa,b,pb);
for(ll i=20;i>=0;i--){
if(predak[a][i]!=predak[b][i]){
a=predak[a][i];
b=predak[b][i];
}
}
dodaj[a]++;
dodaj[b]++;
oduzmi[pa]++;
oduzmi[pb]++;
}
void dfsI(ll t,ll o,ll h){
dubina[t]=h;
predak[t][0]=o;
for(auto x:niz[t]){
if(x.F==o)continue;
dfsI(x.F,t,h+1);
}
}
ll tren=0;
int dfs(ll t,ll o ,ll c1,ll cv){
int xyz=oduzmi[t];
//printf(" %lld %lld %lld\n",t,tren*c1,cv);
for(auto x:niz[t])
if(x.F!=o)
xyz+=dfs(x.F,t,x.S.F,x.S.S);
res+=min(xyz*c1,cv);
xyz-=dodaj[t];
return xyz;
}
int main()
{
scanf("%lld", &n);
for(ll i=1;i<n;i++){
ll t1,t2,c1,c2;
scanf("%lld %lld %lld %lld", &t1, &t2, &c1, &c2);
niz[t1].pb(mp(t2,mp(c1,c2)));
niz[t2].pb(mp(t1,mp(c1,c2)));
}
dfsI(1,0,1);
for(ll j=1;j<=20;j++){
for(ll i=1;i<=n;i++){
predak[i][j]=predak[predak[i][j-1]][j-1];
}
}
for(ll i=1;i<n;i++){
lca(i,i+1);
}
dfs(1,0,0,0);
printf("%lld",res);
return 0;
}
Compilation message (stderr)
putovanje.cpp: In function 'int main()':
putovanje.cpp:77:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld", &n);
~~~~~^~~~~~~~~~~~
putovanje.cpp:80:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld %lld %lld %lld", &t1, &t2, &c1, &c2);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |