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;
const int N = 2e5 + 5, Log = 20;
using ll = long long;
int n, a[N], b[N], c1[N], c2[N];
vector<tuple<int, int, int>> e[N];
ll ans;
int d[N];
int anc[N][Log];
int wc1[N], wc2[N];
int dep[N];
void dfs1(int u, int fa) {
anc[u][0] = fa;
dep[u] = dep[fa] + 1;
for (int i = 1; i < Log; ++i) anc[u][i] = anc[anc[u][i - 1]][i - 1];
for (auto ver: e[u]) {
int v, C1, C2;
tie(v, C1, C2) = ver;
if (v == fa) continue;
wc1[v] = C1;
wc2[v] = C2;
dfs1(v, u);
}
}
int lca(int u, int v) {
if (dep[u] < dep[v])
swap(u, v);
for (int i = Log - 1; i >= 0; --i)
if (dep[anc[u][i]] >= dep[v])
u = anc[u][i];
if (u == v) return u;
for (int i = Log - 1; i >= 0; --i)
if (anc[u][i] != anc[v][i])
u = anc[u][i], v = anc[v][i];
return anc[u][0];
}
void dfs2(int u, int fa) {
for (auto ver: e[u]) {
int v = get<0>(ver);
if (v != fa)
dfs2(v, u), d[u] += d[v];
}
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n - 1; ++i) {
scanf("%d%d%d%d", a+i, b+i, c1+i, c2+i);
e[a[i]].emplace_back(b[i], c1[i], c2[i]);
e[b[i]].emplace_back(a[i], c1[i], c2[i]);
}
dfs1(1, 0);
for (int i = 1; i <= n - 1; ++i) {
const int u = i, v = i + 1;
d[u] += 1, d[v] += 1, d[lca(u, v)] -= 2;
}
dfs2(1, 0);
for (int i = 2; i <= n; ++i)
ans += min((ll) wc2[i], 1ll * wc1[i] * d[i]);
cout << ans;
return 0;
}
Compilation message (stderr)
putovanje.cpp: In function 'int main()':
putovanje.cpp:45:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
45 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
putovanje.cpp:47:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
47 | scanf("%d%d%d%d", a+i, b+i, 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... |