#include <bits/stdc++.h>
#include "factories.h"
#define task "J"
#define all(v) (v).begin(), (v).end()
#define rep(i, l, r) for (int i = (l); i <= (r); ++i)
#define Rep(i, r, l) for (int i = (r); i >= (l); --i)
#define DB(X) { cerr << #X << " = " << (X) << '\n'; }
#define DB1(A, _) { cerr << #A << "[" << _ << "] = " << (A[_]) << '\n'; }
#define DB2(A, _, __) { cerr << #A << "[" << _ << "][" << __ << "] = " << (A[_][__]) << '\n'; }
#define DB3(A, _, __, ___) { cerr << #A << "[" << _ << "][" << __ << "][" << ___ << "] = " << (A[_][__][___]) << '\n'; }
#define PR(A, l, r) { cerr << '\n'; rep(_, l, r) DB1(A, _); cerr << '\n';}
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back
#define pf push_front
#define F first
#define S second
#define by(x) [](const auto& a, const auto& b) { return a.x < b.x; } // sort(arr, arr + N, by(a));
#define next ___next
#define prev ___prev
#define y1 ___y1
#define left ___left
#define right ___right
#define y0 ___y0
#define div ___div
#define j0 ___j0
#define jn ___jn
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using namespace std;
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ll> vl;
const int N = 5e5 + 3;
vii g[N];
bool in[N];
int sz[N], dep[N], mn[2 * N][20], par[N], st[N], eul[2 * N], neu;
ll d[N], mnd[N];
void dfs(int u, int p)
{
st[u] = ++neu;
eul[neu] = u;
dep[u] = dep[p] + 1;
for (ii v : g[u]) if (v.F != p)
{
d[v.F] = d[u] + v.S;
dfs(v.F, u);
eul[++neu] = u;
}
}
int getsz(int u, int p)
{
sz[u] = 1;
for (ii v : g[u]) if (v.F != p && !in[v.F]) sz[u] += getsz(v.F, u);
return sz[u];
}
int fcen(int u, int p, int nn)
{
for (ii v : g[u]) if (v.F != p && !in[v.F] && sz[v.F] > nn / 2) return fcen(v.F, u, nn);
return u;
}
int cd(int u)
{
int nn = getsz(u, 0);
u = fcen(u, 0, nn);
in[u] = 1;
for (ii v : g[u]) if (!in[v.F]) par[cd(v.F)] = u;
return u;
}
void buildLca()
{
rep(i, 1, neu) mn[i][0] = eul[i];
rep(i, 1, 19) for (int j = 1; j + (1 << i) - 1 <= neu; j++)
if (dep[mn[j][i - 1]] < dep[mn[j + (1 << (i - 1))][i - 1]]) mn[j][i] = mn[j][i - 1];
else mn[j][i] = mn[j + (1 << (i - 1))][i - 1];
}
int lca(int x, int y)
{
x = st[x], y = st[y];
if (x > y) swap(x, y);
int k = 31 - __builtin_clz(y - x + 1);
if (dep[mn[x][k]] < dep[mn[y - (1 << k) + 1][k]]) return mn[x][k];
return mn[y - (1 << k) + 1][k];
}
void Init(int n, int A[], int B[], int D[])
{
rep(i, 0, n - 2) g[A[i] + 1].eb(B[i] + 1, D[i]), g[B[i] + 1].eb(A[i] + 1, D[i]);
dfs(1, 0);
cd(1);
buildLca();
dep[0] = 1e9;
fill(mnd + 1, mnd + 1 + n, 1e18);
}
ll Query(int S, int X[], int T, int Y[])
{
ll ans = 1e18;
rep(i, 0, S - 1)
{
int u = X[i] + 1;
while (u)
{
mnd[u] = min(mnd[u], d[X[i] + 1] + d[u] - 2 * d[lca(X[i] + 1, u)]);
u = par[u];
}
}
rep(i, 0, T - 1)
{
int u = Y[i] + 1;
while (u)
{
ans = min(ans, d[Y[i] + 1] + d[u] - 2 * d[lca(Y[i] + 1, u)] + mnd[u]);
u = par[u];
}
}
rep(i, 0, S - 1)
{
int u = X[i] + 1;
while (u)
{
mnd[u] = 1e18;
u = par[u];
}
}
return ans;
}
//int n, q, A[100], B[100], D[100], S, T, X[100], Y[100];
//int main()
//{
//#ifdef HynDuf
// freopen(task".in", "r", stdin);
// //freopen(task".out", "w", stdout);
//#else
// ios_base::sync_with_stdio(false); cin.tie(nullptr);
//#endif
// cin >> n >> q;
// rep(i, 0, n - 2) cin >> A[i] >> B[i] >> D[i];
// Init(n, A, B, D);
// while (q--)
// {
// cin >> S >> T;
// rep(i, 0, S - 1) cin >> X[i];
// rep(i, 0, T - 1) cin >> Y[i];
// cout << Query(S, X, T, Y) << '\n';
// }
// return 0;
//}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
12544 KB |
Output is correct |
2 |
Correct |
547 ms |
21368 KB |
Output is correct |
3 |
Correct |
721 ms |
21496 KB |
Output is correct |
4 |
Correct |
736 ms |
21496 KB |
Output is correct |
5 |
Correct |
785 ms |
21624 KB |
Output is correct |
6 |
Correct |
363 ms |
21496 KB |
Output is correct |
7 |
Correct |
701 ms |
21368 KB |
Output is correct |
8 |
Correct |
726 ms |
21368 KB |
Output is correct |
9 |
Correct |
800 ms |
21624 KB |
Output is correct |
10 |
Correct |
358 ms |
21496 KB |
Output is correct |
11 |
Correct |
712 ms |
21368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
12416 KB |
Output is correct |
2 |
Correct |
2755 ms |
141952 KB |
Output is correct |
3 |
Correct |
3781 ms |
142596 KB |
Output is correct |
4 |
Correct |
1199 ms |
160988 KB |
Output is correct |
5 |
Correct |
5083 ms |
178284 KB |
Output is correct |
6 |
Correct |
3911 ms |
162936 KB |
Output is correct |
7 |
Correct |
2132 ms |
60212 KB |
Output is correct |
8 |
Correct |
734 ms |
61160 KB |
Output is correct |
9 |
Correct |
2392 ms |
63480 KB |
Output is correct |
10 |
Correct |
2136 ms |
61780 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
12544 KB |
Output is correct |
2 |
Correct |
547 ms |
21368 KB |
Output is correct |
3 |
Correct |
721 ms |
21496 KB |
Output is correct |
4 |
Correct |
736 ms |
21496 KB |
Output is correct |
5 |
Correct |
785 ms |
21624 KB |
Output is correct |
6 |
Correct |
363 ms |
21496 KB |
Output is correct |
7 |
Correct |
701 ms |
21368 KB |
Output is correct |
8 |
Correct |
726 ms |
21368 KB |
Output is correct |
9 |
Correct |
800 ms |
21624 KB |
Output is correct |
10 |
Correct |
358 ms |
21496 KB |
Output is correct |
11 |
Correct |
712 ms |
21368 KB |
Output is correct |
12 |
Correct |
10 ms |
12416 KB |
Output is correct |
13 |
Correct |
2755 ms |
141952 KB |
Output is correct |
14 |
Correct |
3781 ms |
142596 KB |
Output is correct |
15 |
Correct |
1199 ms |
160988 KB |
Output is correct |
16 |
Correct |
5083 ms |
178284 KB |
Output is correct |
17 |
Correct |
3911 ms |
162936 KB |
Output is correct |
18 |
Correct |
2132 ms |
60212 KB |
Output is correct |
19 |
Correct |
734 ms |
61160 KB |
Output is correct |
20 |
Correct |
2392 ms |
63480 KB |
Output is correct |
21 |
Correct |
2136 ms |
61780 KB |
Output is correct |
22 |
Correct |
3913 ms |
167896 KB |
Output is correct |
23 |
Correct |
3933 ms |
170616 KB |
Output is correct |
24 |
Correct |
5991 ms |
169028 KB |
Output is correct |
25 |
Correct |
5825 ms |
172936 KB |
Output is correct |
26 |
Correct |
5987 ms |
169308 KB |
Output is correct |
27 |
Correct |
7033 ms |
183824 KB |
Output is correct |
28 |
Correct |
1563 ms |
171356 KB |
Output is correct |
29 |
Correct |
5572 ms |
168916 KB |
Output is correct |
30 |
Correct |
5683 ms |
168528 KB |
Output is correct |
31 |
Correct |
5655 ms |
169000 KB |
Output is correct |
32 |
Correct |
2521 ms |
64280 KB |
Output is correct |
33 |
Correct |
764 ms |
61680 KB |
Output is correct |
34 |
Correct |
1626 ms |
58360 KB |
Output is correct |
35 |
Correct |
1596 ms |
58232 KB |
Output is correct |
36 |
Correct |
2053 ms |
58724 KB |
Output is correct |
37 |
Correct |
2064 ms |
58612 KB |
Output is correct |