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 f first
#define s second
using namespace std;
vector<pair<ll,ll>> g[100050];
ll p[100050][22],h[100050],n,u,v,c1[100050],c2[100050],dem[100050],d[100050],ans;
ll getbit(ll x,ll k)
{
return (x>>k)&1;
}
void dfs(int u)
{
for(int v=0; v<g[u].size(); v++)
{
if(g[u][v].f!=p[u][0])
{
h[g[u][v].f]=h[u]+1;
p[g[u][v].f][0]=u;
for(int i=1; i<=18; i++)
{
p[g[u][v].f][i]=p[p[g[u][v].f][i-1]][i-1];
}
dfs(g[u][v].f);
}
}
}
ll lca(int u,int v)
{
if(h[u]<h[v]) swap(u,v);
int delta=h[u]-h[v];
for(int i=0; i<=18; i++)
{
if(getbit(delta,i)==1)
{
u=p[u][i];
}
}
if(u==v) return u;
for(int i=18; i>=0; i--)
{
if(p[u][i]!=p[v][i])
{
u=p[u][i];
v=p[v][i];
}
}
return p[u][0];
}
void dfs2(int u,int id)
{
for(int v=0; v<g[u].size(); v++)
{
if(g[u][v].f!=p[u][0])
{
dfs2(g[u][v].f,g[u][v].s);
d[u]+=d[g[u][v].f];
}
}
dem[id]=d[u];
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// freopen("friend.inp", "r", stdin);
// freopen("friend.out", "w", stdout);
cin>>n;
for(int i=1; i<=n-1; i++)
{
cin>>u>>v>>c1[i]>>c2[i];
g[u].push_back({v,i});
g[v].push_back({u,i});
}
dfs(1);
for(int i=1; i<=n-1; i++)
{
d[i]++;
d[i+1]++;
d[lca(i,i+1)]-=2;
}
dfs2(1,0);
for(int i=1; i<=n; i++)
{
ans+=min(dem[i]*c1[i],c2[i]);
}
cout<<ans;
return 0;
}
Compilation message (stderr)
putovanje.cpp: In function 'void dfs(int)':
putovanje.cpp:14:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
14 | for(int v=0; v<g[u].size(); v++)
| ~^~~~~~~~~~~~
putovanje.cpp: In function 'void dfs2(int, int)':
putovanje.cpp:53:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | for(int v=0; v<g[u].size(); v++)
| ~^~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |