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>
using namespace std;
typedef long long lo;
typedef pair< lo,lo > PII;
#define fi first
#define se second
#define mp make_pair
#define endl "\n"
#define pb push_back
#define int long long
#define fio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define FOR for(int i=1;i<=n;i++)
#define mid ((start+end)/2)
#define ort ((bas+son)/2)
const lo inf = 1000000000000000000;
const lo KOK = 100000;
const lo LOG = 30;
const lo li = 500005;
const lo mod = 1000000007;
int n,m,b[li],a[li],k,flag,t,c1[li],c2[li],fa[li][LOG],dep[li],vis[li],vis1[li],sum[li];
int cev;
string s;
vector<PII> v[li];
inline void build_lca(){
for(int j=1;j<=27;j++){
FOR fa[i][j]=fa[fa[i][j-1]][j-1];
}
}
inline int ask_lca(int x,int y){
if(dep[x]<dep[y])swap(x,y);
int fark=dep[x]-dep[y];
for(int i=27;i>=0;i--){
if((1<<i)<=fark){
fark-=(1<<i);
x=fa[x][i];
}
}
if(x==y){return x;}
for(int i=27;i>=0;i--){
if(fa[x][i]==fa[y][i])continue;
x=fa[x][i];
y=fa[y][i];
}
return fa[x][0];
}
inline void dfs(int node,int ata,int der){
dep[node]=der;
fa[node][0]=ata;
for(int i=0;i<(int)v[node].size();i++){
int go=v[node][i].fi;
if(go==ata)continue;
dfs(go,node,der+1);
}
}
inline void solve(int node,int ata){
for(int i=0;i<(int)v[node].size();i++){
int go=v[node][i].fi;
int ind=v[node][i].se;
if(go==ata)continue;
//~ int sum=0;
//~ a[ind]=sum;
solve(go,node);
sum[node]+=sum[go];
a[ind]=sum[go];
}
sum[node]+=vis[node];
}
main(void){
scanf("%lld",&n);
for(int i=1;i<n;i++){
int x,y;
scanf("%lld %lld %lld %lld",&x,&y,&c1[i],&c2[i]);
v[x].pb({y,i});
v[y].pb({x,i});
//~ cout<<x<<": : : "<<y<<endl;
}
dfs(1,0,0);
build_lca();
for(int i=1;i<n;i++){
int x=i;
int y=i+1;
flag=-1;
vis[x]++;
vis[y]++;
int at=ask_lca(x,y);
vis[at]-=2;
}
//~ FOR{
//~ printf("%d %d\n",vis[i],vis1[i]);
//~ }
solve(1,0);
int ans=0;
for(int i=1;i<n;i++){
ans+=min(c1[i]*a[i],c2[i]);
//~ cout<<a[i]<<endl;
}
printf("%lld\n",ans);
return 0;
}
Compilation message (stderr)
putovanje.cpp:78:10: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main(void){
^
putovanje.cpp: In function 'int main()':
putovanje.cpp:79:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld",&n);
~~~~~^~~~~~~~~~~
putovanje.cpp:82:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld %lld %lld %lld",&x,&y,&c1[i],&c2[i]);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |