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 el cout<<"\n"
#define f0(i,n) for(int i=0;i<n;++i)
#define f1(i,n) for(int i=1;i<=n;++i)
#define maxn 200005
using namespace std;
int n, lg;
int p[20][maxn], d[maxn], f[maxn];
struct edge
{
int v, c1, c2;
};
vector <edge> a[maxn];
void dfs(int u, int par)
{
for (auto e:a[u])
{
int v = e.v;
if (v!=par)
{
p[0][v] = u;
d[v] = d[u]+1;
dfs(v, u);
}
}
}
void rmq()
{
for (int i=1; i<=lg; i++)
for (int j=1; j<=n; j++)
p[i][j] = p[i-1][p[i-1][j]];
}
int lca(int u, int v)
{
if (d[u] < d[v]) swap(u, v);
int h=d[u]-d[v];
for (int i=lg; ~i; i--)
if ((h>>i)&1) u = p[i][u];
if (u==v) return u;
for (int i=lg; ~i; i--)
if (p[i][u]!=p[i][v]) u = p[i][u], v = p[i][v];
return p[0][u];
}
long long ans;
void dfs2(int u, int par)
{
for (auto e:a[u])
{
int v = e.v;
if (v!=par)
{
dfs2(v, u);
ans += min(1ll*e.c1*f[v],1ll*e.c2);
f[u] += f[v];
}
}
}
main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n;
lg = log2(n);
f1 (i, n-1)
{
int u, v, c1, c2;
cin >> u >> v >> c1 >> c2;
a[u].push_back({v, c1, c2});
a[v].push_back({u, c1, c2});
}
d[1] = 1;
dfs(1, 0);
rmq();
f1 (i, n-1)
{
int p = lca(i, i+1);
f[p]-=2;
f[i]++;
f[i+1]++;
}
dfs2(1, 0);
cout << ans;
}
Compilation message (stderr)
putovanje.cpp:58:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
58 | main()
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |