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;
#define file "COCI20_putovanje"
#define mp make_pair
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define getbit(x, i) (((x) >> (i)) & 1)
#define bit(x) (1LL << (x))
#define popcount __builtin_popcountll
mt19937_64 rd(chrono::steady_clock::now().time_since_epoch().count());
int rand(int l, int r) {
return l + rd() % (r - l + 1);
}
const int N = 2e5 + 5;
const int mod = (int)1e9 + 7; // 998244353;
const int lg = 25; // lg + 1
const int oo = 1e9;
const long long ooo = 1e18;
template<class X, class Y> bool mini(X &a, Y b) {
return a > b ? (a = b, true) : false;
}
template<class X, class Y> bool maxi(X &a, Y b) {
return a < b ? (a = b, true) : false;
}
void add(int &a, int b) {
a += b;
if (a >= mod) a -= mod;
if (a < 0) a += mod;
}
int n;
vector<pair<int, int> > adj[N];
int h[N], par[N][lg + 1];
int d[N];
int single[N], multi[N];
int cnt[N];
void dfs(int u, int p) {
par[u][0] = p;
for (int i = 1; i < 20; ++i) par[u][i] = par[par[u][i - 1]][i - 1];
for (auto [v, id] : adj[u]) if (v != p) {
h[v] = h[u] + 1;
dfs(v, u);
}
}
int lca(int u, int v) {
if (h[u] < h[v]) swap(u, v);
for (int i = 19; i >= 0; --i) if (h[par[u][i]] >= h[v]) u = par[u][i];
if (u == v) return u;
for (int i = 19; i >= 0; --i) if (par[u][i] != par[v][i]) {
u = par[u][i];
v = par[v][i];
}
return par[u][0];
}
void rdfs(int u, int p, int id) {
for (auto [v, id] : adj[u]) if (v != p) {
rdfs(v, u, id);
d[u] += d[v];
}
cnt[id] = d[u];
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
// freopen(file".inp", "r", stdin);
// freopen(file".out", "w", stdout);
cin >> n;
for (int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v >> single[i] >> multi[i];
adj[u].emplace_back(v, i);
adj[v].emplace_back(u, i);
}
h[0] = -1;
dfs(1, 0);
for (int i = 1; i < n; ++i) {
d[i]++;
d[i + 1]++;
d[lca(i, i + 1)] -= 2;
}
rdfs(1, 0, 0);
long long res = 0;
for (int i = 1; i <= n; ++i) {
res += min(1LL * cnt[i] * single[i], 1LL * multi[i]);
}
cout << res;
return 0;
}
/*
5
1 2 2 3
1 3 2 3
1 4 2 3
1 5 2 3
11
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |