#include "factories.h"
#include "bits/stdc++.h"
using namespace std;
const char nl = '\n';
using ll = long long;
/* #warning checkconstanst as per subtask */
const int mxN = 5e5L + 10;
const int logN = 20;
vector<pair<int, ll>> adj[mxN];
int n;
int par[mxN][logN + 1], dep[mxN];
ll dist[mxN][logN + 1];
/* ll check[mxN][mxN]; */
void dfs(int x, int p) {
for(auto U : adj[x]) {
int y = U.first; ll w = U.second;
if(y == p) continue;
dep[y] = dep[x] + 1;
dist[y][0] = w;
par[y][0] = x;
dfs(y, x);
}
}
/* void Init(int N, int A[], int B[], int D[]) { */
void Init(signed N, signed A[], signed B[], signed D[]) {
n = N;
for(int i = 0; i < N - 1; ++i) {
adj[A[i]].emplace_back(B[i], ll(D[i]));
adj[B[i]].emplace_back(A[i], ll(D[i]));
}
dep[0] = 0;
dist[0][0] = -1;
par[0][0] = -1;
dfs(0, -1);
for(int j = 1; j < logN; ++j) {
for(int i = 0; i < N; ++i) {
if(dep[i] < (1ll << j)) {
par[i][j] = dist[i][j] = -1;
}
else {
par[i][j] = par[par[i][j - 1]][j - 1];
dist[i][j] = dist[i][j - 1] + dist[par[i][j - 1]][j - 1];
}
}
}
}
ll distance(int x, int y) {
ll res = 0;
if(dep[x] < dep[y]) swap(x, y);
ll diff = dep[x] - dep[y];
for(int j = logN - 1; j >= 0; --j) {
if(diff < (1ll << j)) continue;
diff -= (1ll << j);
res += dist[x][j];
x = par[x][j];
}
if(x == y) return res;
for(int j = logN - 1; j >= 0; --j) {
if(par[x][j] == par[y][j]) continue;
res += dist[x][j];
x = par[x][j];
res += dist[y][j];
y = par[y][j];
}
res += dist[x][0];
x = par[x][0];
res += dist[y][0];
y = par[y][0];
assert(x == y);
return res;
}
long long Query(signed S, signed X[], signed T, signed Y[]) {
ll res = 1e18;
for(int i = 0; i < S; ++i) {
int x = X[i];
for(int j = 0; j < T; ++j) {
int y = Y[j];
res = min(res, distance(x, y));
}
}
return res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
89 ms |
37468 KB |
Output is correct |
2 |
Execution timed out |
8096 ms |
43856 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
35416 KB |
Output is correct |
2 |
Correct |
1330 ms |
186416 KB |
Output is correct |
3 |
Correct |
3061 ms |
190476 KB |
Output is correct |
4 |
Correct |
800 ms |
183980 KB |
Output is correct |
5 |
Correct |
2805 ms |
214016 KB |
Output is correct |
6 |
Correct |
2230 ms |
190564 KB |
Output is correct |
7 |
Correct |
3626 ms |
70728 KB |
Output is correct |
8 |
Correct |
885 ms |
69684 KB |
Output is correct |
9 |
Correct |
4174 ms |
73700 KB |
Output is correct |
10 |
Correct |
2227 ms |
71124 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
89 ms |
37468 KB |
Output is correct |
2 |
Execution timed out |
8096 ms |
43856 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |