#include "factories.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int nx=5e5+5, kx=19;
ll lvl[nx], pa[nx][kx], ds[nx];
vector<pair<ll, ll>> d[nx];
void dfs(int u, int p)
{
lvl[u]=lvl[p]+1;
pa[u][0]=p;
for (int i=1; i<kx; i++) pa[u][i]=pa[pa[u][i-1]][i-1];
for (auto [v, w]:d[u]) if (v!=p) ds[v]=ds[u]+w, dfs(v, u);
}
int lca(int u, int v)
{
if (lvl[u]>lvl[v]) swap(u, v);
for (int i=kx-1; i>=0; i--) if (lvl[pa[v][i]]>=lvl[u]) v=pa[v][i];
if (u==v) return v;
for (int i=kx-1; i>=0; i--) if (pa[u][i]!=pa[v][i]) u=pa[u][i], v=pa[v][i];
return pa[u][0];
}
void Init(int N, int A[], int B[], int D[]) {
for (int i=0; i<N-1; i++) d[A[i]].push_back({B[i], D[i]}), d[B[i]].push_back({A[i], D[i]});
dfs(0, 0);
}
long long Query(int S, int X[], int T, int Y[]) {
ll res=LLONG_MAX;
for (int i=0; i<S; i++) for (int j=0; j<T; j++) res=min(res, ds[X[i]]+ds[Y[j]]-2*ds[lca(X[i], Y[j])]);
return res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
186 ms |
35676 KB |
Output is correct |
2 |
Execution timed out |
8028 ms |
47116 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
33368 KB |
Output is correct |
2 |
Correct |
1605 ms |
161904 KB |
Output is correct |
3 |
Correct |
3255 ms |
165336 KB |
Output is correct |
4 |
Correct |
937 ms |
159180 KB |
Output is correct |
5 |
Correct |
3545 ms |
189624 KB |
Output is correct |
6 |
Correct |
2576 ms |
166148 KB |
Output is correct |
7 |
Correct |
5071 ms |
76816 KB |
Output is correct |
8 |
Correct |
1713 ms |
75892 KB |
Output is correct |
9 |
Correct |
4305 ms |
79988 KB |
Output is correct |
10 |
Correct |
3000 ms |
77416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
186 ms |
35676 KB |
Output is correct |
2 |
Execution timed out |
8028 ms |
47116 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |