#ifdef HOME
#else
#include "factories.h"
#endif
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
const int N = 5e5 + 7, LG = 20, INF = 1e18 + 7;
vector <ii> g[N];
int h[N], l[N], r[N], to[N][LG], ptr = 0;
void dfs(int u, int p) {
to[u][0] = p; for (int i = 1; i < LG; ++i) to[u][i] = to[to[u][i - 1]][i - 1];
l[u] = ++ptr;
for (auto e : g[u]) if (e.first != p) { h[e.first] = h[u] + e.second; dfs(e.first, u); }
r[u] = ptr;
}
void Init(signed N, signed A[], signed B[], signed D[]) {
for (int i = 0; i < N - 1; ++i) { g[A[i]].app({B[i], D[i]}); g[B[i]].app({A[i], D[i]}); }
dfs(1, 1);
}
inline bool anc(int u, int v) { return l[u] <= l[v] && r[v] <= r[u]; }
int lca(int u, int v) {
if (anc(u, v)) return u;
for (int i = LG - 1; i >= 0; --i) if (!anc(to[u][i], v)) u = to[u][i];
return to[u][0];
}
int type[N];
vector <int> g1[N];
int ans;
int bl[N][2];
void dfs1(int u) {
bl[u][0] = bl[u][1] = INF;
if (type[u]) bl[u][type[u] - 1] = 0;
for (int v : g1[u]) { dfs1(v); for (int i = 0; i < 2; ++i) bl[u][i] = min(bl[u][i], bl[v][i] + h[v] - h[u]); }
ans = min(ans, bl[u][0] + bl[u][1]);
}
bool comp(int u, int v) { return l[u] < l[v]; }
long long Query(signed S, signed X[], signed T, signed Y[]) {
vector <int> a;
for (int i = 0; i < S; ++i) { a.app(X[i]); type[X[i]] = 1; }
for (int i = 0; i < T; ++i) { a.app(Y[i]); type[Y[i]] = 2; }
sort(all(a), comp);
int sz = a.size();
for (int i = 0; i + 1 < sz; ++i) a.app(lca(a[i], a[i + 1]));
sort(all(a), comp); a.resize(unique(all(a)) - a.begin());
for (int e : a) g1[e].clear();
for (int e : a) if (!anc(a[0], e)) { cout << "LMAO\n"; exit(1); }
vector <int> v;
for (int e : a) {
while (v.size() && r[v.back()] < l[e]) v.pop_back();
if (v.size()) g1[v.back()].app(e);
v.app(e);
}
ans = INF; dfs1(a[0]);
for (int e : a) type[e] = 0;
return ans;
}
#ifdef HOME
signed main() {
freopen("input.txt", "r", stdin);
signed n, q; cin >> n >> q;
signed a[n - 1], b[n - 1], d[n - 1];
for (int i = 0; i < n - 1; ++i) cin >> a[i] >> b[i] >> d[i];
Init(n, a, b, d);
while (q--) {
signed s, t; cin >> s >> t;
signed a[s - 1], b[t - 1];
for (int i = 0; i < s; ++i) cin >> a[i];
for (int i = 0; i < t; ++i) cin >> b[i];
cout << Query(s, a, t, b) << '\n';
}
}
#endif
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
24440 KB |
Output is correct |
2 |
Correct |
1279 ms |
42992 KB |
Output is correct |
3 |
Correct |
1221 ms |
43016 KB |
Output is correct |
4 |
Correct |
1265 ms |
43172 KB |
Output is correct |
5 |
Correct |
1107 ms |
43080 KB |
Output is correct |
6 |
Correct |
979 ms |
43008 KB |
Output is correct |
7 |
Correct |
1210 ms |
43004 KB |
Output is correct |
8 |
Correct |
1270 ms |
43124 KB |
Output is correct |
9 |
Correct |
1101 ms |
43352 KB |
Output is correct |
10 |
Correct |
959 ms |
42948 KB |
Output is correct |
11 |
Correct |
1207 ms |
43120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
24184 KB |
Output is correct |
2 |
Correct |
1944 ms |
183032 KB |
Output is correct |
3 |
Correct |
3026 ms |
184204 KB |
Output is correct |
4 |
Correct |
1551 ms |
180188 KB |
Output is correct |
5 |
Correct |
2180 ms |
215944 KB |
Output is correct |
6 |
Correct |
3234 ms |
186824 KB |
Output is correct |
7 |
Correct |
2666 ms |
74744 KB |
Output is correct |
8 |
Correct |
1564 ms |
74640 KB |
Output is correct |
9 |
Correct |
1544 ms |
81720 KB |
Output is correct |
10 |
Correct |
2537 ms |
75964 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
24440 KB |
Output is correct |
2 |
Correct |
1279 ms |
42992 KB |
Output is correct |
3 |
Correct |
1221 ms |
43016 KB |
Output is correct |
4 |
Correct |
1265 ms |
43172 KB |
Output is correct |
5 |
Correct |
1107 ms |
43080 KB |
Output is correct |
6 |
Correct |
979 ms |
43008 KB |
Output is correct |
7 |
Correct |
1210 ms |
43004 KB |
Output is correct |
8 |
Correct |
1270 ms |
43124 KB |
Output is correct |
9 |
Correct |
1101 ms |
43352 KB |
Output is correct |
10 |
Correct |
959 ms |
42948 KB |
Output is correct |
11 |
Correct |
1207 ms |
43120 KB |
Output is correct |
12 |
Correct |
27 ms |
24184 KB |
Output is correct |
13 |
Correct |
1944 ms |
183032 KB |
Output is correct |
14 |
Correct |
3026 ms |
184204 KB |
Output is correct |
15 |
Correct |
1551 ms |
180188 KB |
Output is correct |
16 |
Correct |
2180 ms |
215944 KB |
Output is correct |
17 |
Correct |
3234 ms |
186824 KB |
Output is correct |
18 |
Correct |
2666 ms |
74744 KB |
Output is correct |
19 |
Correct |
1564 ms |
74640 KB |
Output is correct |
20 |
Correct |
1544 ms |
81720 KB |
Output is correct |
21 |
Correct |
2537 ms |
75964 KB |
Output is correct |
22 |
Correct |
4241 ms |
198572 KB |
Output is correct |
23 |
Correct |
3969 ms |
200464 KB |
Output is correct |
24 |
Correct |
4830 ms |
203700 KB |
Output is correct |
25 |
Correct |
4810 ms |
203596 KB |
Output is correct |
26 |
Correct |
5450 ms |
195460 KB |
Output is correct |
27 |
Correct |
4168 ms |
232624 KB |
Output is correct |
28 |
Correct |
2710 ms |
195152 KB |
Output is correct |
29 |
Correct |
5209 ms |
193820 KB |
Output is correct |
30 |
Correct |
4941 ms |
194224 KB |
Output is correct |
31 |
Correct |
5689 ms |
194408 KB |
Output is correct |
32 |
Correct |
2238 ms |
84172 KB |
Output is correct |
33 |
Correct |
1768 ms |
78328 KB |
Output is correct |
34 |
Correct |
2347 ms |
73500 KB |
Output is correct |
35 |
Correct |
2306 ms |
73416 KB |
Output is correct |
36 |
Correct |
2572 ms |
74160 KB |
Output is correct |
37 |
Correct |
2661 ms |
73976 KB |
Output is correct |