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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
| #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define all(x) begin(x), end(x)
#ifdef local
#define safe cerr << __LINE__ << " line " << __LINE__ << " safe\n"
#define debug(a...) debug_(#a, a)
#define orange(a...) orange_(#a, a)
template <typename ...T>
void debug_(const char *s, T ...a) {
cerr << "\e[1;32m(" << s << ") = (";
int cnt = sizeof...(T);
(..., (cerr << a << (--cnt ? ", " : ")\e[0m\n")));
}
template <typename I>
void orange_(const char *s, I L, I R) {
cerr << "\e[1;32m[ " << s << " ] = [ ";
for (int f = 0; L != R; ++L)
cerr << (f++ ? ", " : "") << *L;
cerr << " ]\e[0m\n";
}
#else
#define safe ((void)0)
#define debug(...) safe
#define orange(...) safe
#endif
using lld = int64_t;
template <typename F, typename C> class MCMF {
static constexpr F INF_F = numeric_limits<F>::max();
static constexpr C INF_C = numeric_limits<C>::max();
struct E { int to, r; F f; C c; };
vector<vector<E>> g; vector<pair<int, int>> f;
vector<int> inq; vector<F> up; vector<C> d;
optional<pair<F, C>> step(int S, int T) {
queue<int> q;
for (q.push(S), d[S] = 0, up[S] = INF_F;
not q.empty(); q.pop()) {
int u = q.front(); inq[u] = false;
if (up[u] == 0) continue;
for (int i = 0; i < int(g[u].size()); ++i) {
auto e = g[u][i]; int v = e.to;
if (e.f <= 0 or d[v] <= d[u] + e.c) continue;
d[v] = d[u] + e.c; f[v] = {u, i};
up[v] = min(up[u], e.f);
if (not inq[v]) q.push(v);
inq[v] = true;
}
}
if (d[T] == INF_C) return nullopt;
for (int i = T; i != S; i = f[i].first) {
auto &eg = g[f[i].first][f[i].second];
eg.f -= up[T]; g[eg.to][eg.r].f += up[T];
}
return pair{up[T], d[T]};
}
public:
MCMF(int n) : g(n),f(n),inq(n),up(n),d(n,INF_C) {}
void add_edge(int s, int t, F c, C w) {
g[s].emplace_back(t, int(g[t].size()), c, w);
g[t].emplace_back(s, int(g[s].size()) - 1, 0, -w);
}
pair<F, C> solve(int a, int b) {
F c = 0; C w = 0;
while (auto r = step(a, b)) {
c += r->first, w += r->first * r->second;
ranges::fill(inq, false); ranges::fill(d, INF_C);
}
return {c, w};
}
};
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
int N, M;
cin >> N >> M;
vector<int> c(M);
vector<pair<int, int>> es;
vector<vector<pair<int,int>>> g(N);
for (int i = 0; i < M; i++) {
int a, b;
cin >> a >> b;
--a, --b;
if (i < N - 1) {
g[a].emplace_back(b, i);
g[b].emplace_back(a, i);
}
es.emplace_back(a, b);
cin >> c[i];
}
vector<int> pa(N), pa_edge(N), dep(N);
auto dfs = [&](auto &&self, int i, int p = -1) -> void {
for (auto [j, id] : g[i]) if (j != p) {
pa[j] = i;
pa_edge[j] = id;
dep[j] = dep[i] + 1;
self(self, j, i);
}
};
pa[0] = 0;
pa_edge[0] = -1;
dfs(dfs, 0);
constexpr int inf = 1e9;
int tot = M;
constexpr int LG = 15;
vector<int> sparse_table[LG];
for (int l = 0; l < LG; l++) {
sparse_table[l].resize(N);
for (int i = 0; i < N; i++) {
sparse_table[l][i] = tot++;
}
}
auto anc = [&](int v, int step) {
while (step--) {
v = pa[v];
if (v == pa[v]) break;
}
return v;
};
auto get_max = [&](int u, int v) {
int res = -inf;
while (u != v) {
res = max(res, c[pa_edge[u]]);
u = pa[u];
}
return res;
};
auto lca = [&](int a, int b) {
while (a != b) {
if (dep[a] < dep[b]) swap(a, b);
a = pa[a];
}
return a;
};
const int S = tot, T = tot + 1;
MCMF<int64_t, int64_t> flow(tot + 2);
for (int i = 0; i < N; i++)
if (pa_edge[i] != -1)
flow.add_edge(pa_edge[i], sparse_table[0][i], inf, 0);
for (int l = 0; l + 1 < LG; l++) {
for (int i = 0; i < N; i++) {
int j = anc(i, 1 << l);
flow.add_edge(sparse_table[l][i], sparse_table[l + 1][i], inf, 0);
flow.add_edge(sparse_table[l][j], sparse_table[l + 1][i], inf, 0);
}
}
for (int i = N - 1; i < M; i++) {
auto [a, b] = es[i];
int l = lca(a, b);
// int mx = max(get_max(a, l), get_max(b, l));
// if (mx <= c[i]) continue;
for (int u : {a, b}) {
if (u == l) continue;
const int d = dep[u] - dep[l];
const int lg = __lg(d);
const int v = anc(u, d - (1 << lg));
debug(u+1, v+1, lg);
// assert(anc(v, 1 << lg) == l && anc(v, (1 << lg) - 1) != l);
flow.add_edge(sparse_table[lg][u], i, inf, 0);
flow.add_edge(sparse_table[lg][v], i, inf, 0);
}
}
for (int i = 0; i < N - 1; i++) {
flow.add_edge(S, i, 1, -c[i]);
flow.add_edge(i, T, 1, c[i]); // not match anything
}
for (int i = N - 1; i < M; i++)
flow.add_edge(i, T, 1, c[i]);
auto [_, ans] = flow.solve(S, T);
cout << -ans << '\n';
// C_i - x_i <= C_j + y_j
// x_i >= 0
// y_j >= 0
// x_i + y_j >= C_i - C_j
// minimize \sum x_i + \sum y_j
// dual ->
// minimize (C_j - C_i) e_{ij}
// s.t. \sum _ j e_{ij} <= 1, \sum _ i e_{ij} <= 1
// e_{ij} >= 0
}
|