-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathCOCI 20-putovanje.cpp
113 lines (98 loc) · 2.47 KB
/
COCI 20-putovanje.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
* Also see: https://github.com/dolphingarlic/CompetitiveProgramming/blob/master/COI/COCI%2020-putovanje.cpp
*
* If we're going from A -> B, we can split this up into two paths: A -> lca(A, B) and lca(A, B) -> B.
*
* We can add one to node A and node B and subtract two from node lca(A, B). Now,
* the number of times an edge is traversed is simply the sum of all the nodes in
* its subtree.
*/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define ii pair<int, int>
#define f first
#define s second
#define mp make_pair
#define pb push_back
int n;
vector<pair<int, ii>> adj[200000];
int pa[200000][20];
int depth[200000];
int in[200000], out[200000], ctr = 0;
void dfs(int u, int p, int d) {
in[u] = ctr++;
pa[u][0] = p;
depth[u] = d;
for (auto v : adj[u]) {
if (v.f != p) dfs(v.f, u, d+1);
}
out[u] = ctr-1;
}
int lca(int a, int b) {
if (depth[a] < depth[b]) swap(a, b);
for (int i = 19; i >= 0; i--) {
if (depth[pa[a][i]] >= depth[b]) a = pa[a][i];
}
if (a == b) return a;
for (int i = 19; i >= 0; i--) {
if (pa[a][i] != pa[b][i]) {
a = pa[a][i], b = pa[b][i];
}
}
return pa[a][0];
}
int ft[200001];
void upd(int k, int v) {
k++;
while (k <= 200000) {
ft[k] += v;
k += k&-k;
}
}
int qry(int k) {
k++;
int ans = 0;
while(k) {
ans += ft[k];
k -= k&-k;
}
return ans;
}
ll ans = 0;
void dfs2(int u, int p) {
for (auto v : adj[u]) {
if (v.f == p) continue;
assert(depth[v.f] = depth[u]+1);
ll numTraversals = qry(out[v.f]) - qry(in[v.f]-1);
//cout << u << "->" << v.f << ": " << min(numTraversals*v.s.f, (ll)v.s.s) << endl;
ans += min(numTraversals*v.s.f, (ll)v.s.s);
dfs2(v.f, u);
}
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for (int i = 0; i < n-1; i++) {
int a, b, c, d; cin >> a >> b >> c >> d; --a; --b;
adj[a].push_back(mp(b, mp(c, d)));
adj[b].push_back(mp(a, mp(c, d)));
}
dfs(0, 0, 0);
for (int j = 1; j < 20; j++) {
for (int i = 0; i < n; i++) {
pa[i][j] = pa[pa[i][j-1]][j-1];
}
}
for (int i = 0; i < n-1; i++) {
int x = lca(i, i+1);
assert(depth[i] >= depth[x]);
assert(depth[i+1] >= depth[x]);
upd(in[i], 1);
upd(in[i+1], 1);
upd(in[x], -2);
}
dfs2(0, 0);
cout << ans << endl;
return 0;
}