// #pragma GCC optimize("Ofast,no-stack-protector")
// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx,avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
#define lowbit(x) ((x) & -(x))
#define ml(a, b) ((1ll * (a) * (b)) % M)
#define tml(a, b) (a) = ((1ll * (a) * (b)) % M)
#define ad(a, b) ((0ll + (a) + (b)) % M)
#define tad(a, b) (a) = ((0ll + (a) + (b)) % M)
#define mi(a, b) ((0ll + M + (a) - (b)) % M)
#define tmi(a, b) (a) = ((0ll + M + (a) - (b)) % M)
#define tmin(a, b) (a) = min((a), (b))
#define tmax(a, b) (a) = max((a), (b))
#define iter(a) (a).begin(), (a).end()
#define riter(a) (a).rbegin(), (a).rend()
#define init(a, b) memset((a), (b), sizeof(a))
#define cpy(a, b) memcpy((a), (b), sizeof(a))
#define uni(a) a.resize(unique(iter(a)) - a.begin())
#define size(x) (int)x.size()
#define pb emplace_back
#define mpr make_pair
#define ls(i) ((i) << 1)
#define rs(i) ((i) << 1 | 1)
#define INF 0x3f3f3f3f
#define NIF 0xc0c0c0c0
#define eps 1e-9
#define F first
#define S second
#define AC cin.tie(0)->sync_with_stdio(0)
using namespace std;
typedef long long llt;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<llt, llt> pll;
typedef complex<double> cd;
// const int M = 998244353;
// random_device rm;
// mt19937 rg(rm());
// default_random_engine rg(rm());
// uniform_int_distribution<int> rd(INT_MIN, INT_MAX);
// uniform_real_distribution<double> rd(0, M_PI);
void db() { cerr << "\n"; }
template <class T, class... U>
void db(T a, U... b) { cerr << a << " ", db(b...); }
inline char gc()
{
const static int SZ = 1 << 16;
static char buf[SZ], *p1, *p2;
if (p1 == p2 && (p2 = buf + fread(p1 = buf, 1, SZ, stdin), p1 == p2))
return -1;
return *p1++;
}
void rd() {}
template <typename T, typename... U>
void rd(T &x, U &...y)
{
x = 0;
bool f = 0;
char c = gc();
while (!isdigit(c))
f ^= !(c ^ 45), c = gc();
while (isdigit(c))
x = (x << 1) + (x << 3) + (c ^ 48), c = gc();
f && (x = -x), rd(y...);
}
template <typename T>
void prt(T x)
{
if (x < 0)
putchar('-'), x = -x;
if (x > 9)
prt(x / 10);
putchar((x % 10) ^ 48);
}
const int N = 5e5 + 5, M = 2e6 + 6;
vector<int> eg[N];
int xr[M], yr[M], wr[M], a1[M];
int in[N], ou[N], ct, ac[20][N];
int mx[N], sc[N], dp[N], ans, bs;
vector<int> q1[N], q2[N];
void dfs(int u, int fa)
{
in[u] = ++ct;
for (int v : eg[u])
{
if (v == fa)
continue;
ac[0][v] = u, dp[v] = dp[u] + 1;
for (int i = 1; i < 20; i++)
ac[i][v] = ac[i - 1][ac[i - 1][v]];
dfs(v, u);
tmax(sc[u], mx[v] + 1);
if (sc[u] > mx[u])
swap(mx[u], sc[u]);
}
tmax(ans, mx[u] + sc[u]), ou[u] = ct;
}
inline bool isa(int a, int p)
{
return in[a] <= in[p] && ou[p] <= ou[a];
}
inline int lca(int u, int v)
{
if (isa(u, v))
return u;
for (int i = 19; i >= 0; i--)
if (ac[i][u] && !isa(ac[i][u], v))
u = ac[i][u];
return ac[0][u];
}
struct P
{
int vp, vn, ans;
P() { vp = vn = ans = -INF; }
inline void ini() { vp = vn = ans = -INF; }
P operator+(const P &rh)
{
P res;
res.ans = max({ans, vp + rh.vn, rh.ans});
res.vp = max(vp, rh.vp);
res.vn = max(vn, rh.vn);
return res;
}
} tr[N << 2], vv;
void cg(int l, int r, int id, int p, int v)
{
if (l == r)
{
tr[id].vp = v + p;
tr[id].vn = v - p;
tr[id].ans = -INF;
return;
}
int m = (l + r) >> 1;
if (p <= m)
cg(l, m, ls(id), p, v);
else
cg(m + 1, r, rs(id), p, v);
tr[id] = tr[ls(id)] + tr[rs(id)];
}
void qy(int l, int r, int id, int ql, int qr)
{
if (ql <= l && r <= qr)
{
vv = vv + tr[id];
return;
}
int m = (l + r) >> 1;
if (ql <= m)
qy(l, m, ls(id), ql, qr);
if (qr > m)
qy(m + 1, r, rs(id), ql, qr);
}
void slv(int u, int fa)
{
for (int v : eg[u])
{
if (v == fa)
continue;
cg(1, ct, 1, dp[u], mx[v] + 1 == mx[u] ? sc[u] : mx[u]);
slv(v, u);
}
cg(1, ct, 1, dp[u], mx[u]);
for (int i : q1[u])
{
vv.ini(), qy(1, ct, 1, dp[xr[i]], dp[u]);
tmin(ans, bs + wr[i] - (vv.ans + dp[u] - dp[xr[i]]));
vv.ini(), qy(1, ct, 1, dp[xr[i]] + 1, dp[u]);
int tp = vv.vn + dp[u] + dp[xr[i]];
vv.ini(), qy(1, ct, 1, 1, dp[xr[i]]);
tmin(ans, bs + wr[i] - (tp + vv.vn));
}
for (int i : q2[u])
{
int p = lca(xr[i], yr[i]);
vv.ini(), qy(1, ct, 1, dp[p], dp[u]);
// tmin(ans, bs + wr[i] -
// (vv.ans + dp[xr[i]] + dp[yr[i]] - (dp[p] << 1)));
vv.ini(), qy(1, ct, 1, dp[p] + 1, dp[u]);
if (xr[i] == u)
a1[i] = vv.vn;
else
tmin(ans, bs + wr[i] - (vv.vn + a1[i] + dp[xr[i]] + dp[yr[i]]));
}
}
signed main()
{
int n, m;
rd(n, m), bs = (n - 2) << 1;
for (int i = 0; i < m; i++)
{
rd(xr[i], yr[i], wr[i]), xr[i]++, yr[i]++;
if (wr[i] == 1)
eg[xr[i]].pb(yr[i]), eg[yr[i]].pb(xr[i]);
}
dp[1] = 1, dfs(1, -1), ans = ((n - 1) << 1) - ans;
for (int i = 0; i < m; i++)
{
assert(xr[i] != yr[i]);
if (wr[i] == 1)
continue;
if (in[xr[i]] > in[yr[i]])
swap(xr[i], yr[i]);
if (isa(xr[i], yr[i]))
q1[yr[i]].pb(i);
else
q2[xr[i]].pb(i), q2[yr[i]].pb(i);
}
slv(1, -1);
prt(ans), putchar('\n');
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
108368 KB |
Output is correct |
2 |
Correct |
19 ms |
110424 KB |
Output is correct |
3 |
Correct |
22 ms |
110684 KB |
Output is correct |
4 |
Correct |
19 ms |
110596 KB |
Output is correct |
5 |
Correct |
20 ms |
110684 KB |
Output is correct |
6 |
Correct |
18 ms |
110684 KB |
Output is correct |
7 |
Correct |
19 ms |
110424 KB |
Output is correct |
8 |
Correct |
18 ms |
108380 KB |
Output is correct |
9 |
Correct |
19 ms |
110620 KB |
Output is correct |
10 |
Correct |
18 ms |
108380 KB |
Output is correct |
11 |
Incorrect |
21 ms |
110544 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
108368 KB |
Output is correct |
2 |
Correct |
19 ms |
110424 KB |
Output is correct |
3 |
Correct |
22 ms |
110684 KB |
Output is correct |
4 |
Correct |
19 ms |
110596 KB |
Output is correct |
5 |
Correct |
20 ms |
110684 KB |
Output is correct |
6 |
Correct |
18 ms |
110684 KB |
Output is correct |
7 |
Correct |
19 ms |
110424 KB |
Output is correct |
8 |
Correct |
18 ms |
108380 KB |
Output is correct |
9 |
Correct |
19 ms |
110620 KB |
Output is correct |
10 |
Correct |
18 ms |
108380 KB |
Output is correct |
11 |
Incorrect |
21 ms |
110544 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
111196 KB |
Output is correct |
2 |
Correct |
25 ms |
111320 KB |
Output is correct |
3 |
Correct |
26 ms |
111196 KB |
Output is correct |
4 |
Correct |
25 ms |
111064 KB |
Output is correct |
5 |
Correct |
29 ms |
111036 KB |
Output is correct |
6 |
Correct |
23 ms |
111028 KB |
Output is correct |
7 |
Correct |
24 ms |
111432 KB |
Output is correct |
8 |
Correct |
27 ms |
111196 KB |
Output is correct |
9 |
Correct |
26 ms |
111196 KB |
Output is correct |
10 |
Correct |
29 ms |
111196 KB |
Output is correct |
11 |
Correct |
24 ms |
111196 KB |
Output is correct |
12 |
Correct |
24 ms |
110940 KB |
Output is correct |
13 |
Correct |
28 ms |
111272 KB |
Output is correct |
14 |
Correct |
24 ms |
111192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
108368 KB |
Output is correct |
2 |
Correct |
19 ms |
110424 KB |
Output is correct |
3 |
Correct |
22 ms |
110684 KB |
Output is correct |
4 |
Correct |
19 ms |
110596 KB |
Output is correct |
5 |
Correct |
20 ms |
110684 KB |
Output is correct |
6 |
Correct |
18 ms |
110684 KB |
Output is correct |
7 |
Correct |
19 ms |
110424 KB |
Output is correct |
8 |
Correct |
18 ms |
108380 KB |
Output is correct |
9 |
Correct |
19 ms |
110620 KB |
Output is correct |
10 |
Correct |
18 ms |
108380 KB |
Output is correct |
11 |
Incorrect |
21 ms |
110544 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
108368 KB |
Output is correct |
2 |
Correct |
19 ms |
110424 KB |
Output is correct |
3 |
Correct |
22 ms |
110684 KB |
Output is correct |
4 |
Correct |
19 ms |
110596 KB |
Output is correct |
5 |
Correct |
20 ms |
110684 KB |
Output is correct |
6 |
Correct |
18 ms |
110684 KB |
Output is correct |
7 |
Correct |
19 ms |
110424 KB |
Output is correct |
8 |
Correct |
18 ms |
108380 KB |
Output is correct |
9 |
Correct |
19 ms |
110620 KB |
Output is correct |
10 |
Correct |
18 ms |
108380 KB |
Output is correct |
11 |
Incorrect |
21 ms |
110544 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
108368 KB |
Output is correct |
2 |
Correct |
19 ms |
110424 KB |
Output is correct |
3 |
Correct |
22 ms |
110684 KB |
Output is correct |
4 |
Correct |
19 ms |
110596 KB |
Output is correct |
5 |
Correct |
20 ms |
110684 KB |
Output is correct |
6 |
Correct |
18 ms |
110684 KB |
Output is correct |
7 |
Correct |
19 ms |
110424 KB |
Output is correct |
8 |
Correct |
18 ms |
108380 KB |
Output is correct |
9 |
Correct |
19 ms |
110620 KB |
Output is correct |
10 |
Correct |
18 ms |
108380 KB |
Output is correct |
11 |
Incorrect |
21 ms |
110544 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
108368 KB |
Output is correct |
2 |
Correct |
19 ms |
110424 KB |
Output is correct |
3 |
Correct |
22 ms |
110684 KB |
Output is correct |
4 |
Correct |
19 ms |
110596 KB |
Output is correct |
5 |
Correct |
20 ms |
110684 KB |
Output is correct |
6 |
Correct |
18 ms |
110684 KB |
Output is correct |
7 |
Correct |
19 ms |
110424 KB |
Output is correct |
8 |
Correct |
18 ms |
108380 KB |
Output is correct |
9 |
Correct |
19 ms |
110620 KB |
Output is correct |
10 |
Correct |
18 ms |
108380 KB |
Output is correct |
11 |
Incorrect |
21 ms |
110544 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
108368 KB |
Output is correct |
2 |
Correct |
19 ms |
110424 KB |
Output is correct |
3 |
Correct |
22 ms |
110684 KB |
Output is correct |
4 |
Correct |
19 ms |
110596 KB |
Output is correct |
5 |
Correct |
20 ms |
110684 KB |
Output is correct |
6 |
Correct |
18 ms |
110684 KB |
Output is correct |
7 |
Correct |
19 ms |
110424 KB |
Output is correct |
8 |
Correct |
18 ms |
108380 KB |
Output is correct |
9 |
Correct |
19 ms |
110620 KB |
Output is correct |
10 |
Correct |
18 ms |
108380 KB |
Output is correct |
11 |
Incorrect |
21 ms |
110544 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |