This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
ID: tmhazem1
LANG: C++14
TASK: pprime
*/
#include <bits/stdc++.h>
using namespace std;
#define S second
#define F first
#define LL long long
const int N = 2e5 + 10;
LL LINF = 100000000000000000;
LL INF = 1000000000;
LL par[N][30],pr[N],depth[N];
vector<pair<int,pair<LL,LL>>>adj[N];
void dfs(int i,int pre){
par[i][0] = pre;
for(int j=1;j<=20;j++)
par[i][j] = par[par[i][j-1]][j-1];
for(auto x:adj[i])
if(x.F==pre)continue;
else
depth[x.F] = depth[i]+1,dfs(x.F,i),pr[i] += pr[x.F];
}
int lca(int u,int v){
if(depth[u]<depth[v])swap(u,v);
for(int i=20;i>=0;i--)
if(depth[u]-(1<<i)>=depth[v])
u = par[u][i];
if(u==v)return u;
for(int i=20;i>=0;i--)
if(par[u][i]!=par[v][i])
u = par[u][i],v = par[v][i];
return par[u][0];
}
LL dfs2(int i,int pre){
LL ret = 0;
for(auto x:adj[i])
if(x.F==pre)continue;
else ret += min(x.S.F*pr[x.F],x.S.S)+dfs2(x.F,i);
return ret;
}
int main()
{
//freopen("out.txt","w",stdout);
int n;
scanf("%d",&n);
for(int i=1;i<n;i++){
int u,v,c,c1;
scanf("%d%d%d%d",&u,&v,&c,&c1);
adj[u].push_back({v,{c,c1}});
adj[v].push_back({u,{c,c1}});
}
dfs(1,1);
for(int i=1;i<n;i++){
int l = lca(i,i+1);
pr[l] -= 2;
pr[i]++;pr[i+1]++;
}
dfs(1,1);
printf("%lld\n",dfs2(1,1));
}
Compilation message (stderr)
putovanje.cpp: In function 'int main()':
putovanje.cpp:66:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
66 | scanf("%d",&n);
| ~~~~~^~~~~~~~~
putovanje.cpp:70:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
70 | scanf("%d%d%d%d",&u,&v,&c,&c1);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |