#include <bits/stdc++.h>
#ifdef DEBUG
#include <debug.hpp>
#else
#define debug(...) 42
#endif
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
using namespace std;
const ll INF = 1e18;
#define MAX_N 500005
vector<pair<int, ll>> gr[MAX_N];
int sz[MAX_N], used[MAX_N], act[MAX_N], cur[MAX_N];
ll cls[MAX_N];
int cent[MAX_N][25];
ll dist[MAX_N][25];
int now = 1;
int find_sz(int v, int p) {
sz[v] = 1;
for (auto [to, w] : gr[v]) {
if (used[to] || to == p) continue;
sz[v] += find_sz(to, v);
}
return sz[v];
}
int centr(int v, int p, int cn) {
bool flag = 1;
while (flag) {
flag = 0;
for (auto [to, w] : gr[v]) {
if (used[to] || to == p) continue;
if (sz[to] > cn / 2) {
p = v;
v = to;
flag = 1;
break;
}
}
}
return v;
}
void dfs(int v, int c, int p = -1, ll d = 0) {
cent[v][cur[v]] = c;
dist[v][cur[v]] = d;
++cur[v];
for (auto [to, w] : gr[v]) {
if (used[to] || to == p) continue;
dfs(to, c, v, d + w);
}
}
void Decomp(int v) {
int cn = find_sz(v, v);
int c = centr(v, v, cn);
used[c] = 1;
dfs(c, c);
for (auto [to, w] : gr[c]) {
if (used[to]) continue;
Decomp(to);
}
}
void Init(int N, int A[], int B[], int D[]) {
memset(used, 0, sizeof(used));
memset(act, 0, sizeof(act));
memset(cur, 0, sizeof(cur));
for (int i = 0; i < N; ++i) {
cls[i] = INF;
gr[i].clear();
}
now = 0;
for (int i = 0; i < N; ++i) {
gr[A[i]].emplace_back(B[i], D[i]);
gr[B[i]].emplace_back(A[i], D[i]);
}
Decomp(0);
}
void Update(int v) {
for (int j = 0; j < cur[v]; ++j) {
int c = cent[v][j];
if (act[c] == now) {
cls[c] = min(cls[c], dist[v][j]);
} else {
cls[c] = dist[v][j];
act[c] = now;
}
}
}
ll GetMin(int v) {
ll res = INF;
for (int j = 0; j < cur[v]; ++j) {
int c = cent[v][j];
if (act[c] == now) {
res = min(res, dist[v][j] + cls[c]);
}
}
return res;
}
ll Query(int S, int X[], int T, int Y[]) {
// go through all X[i] centroids and update cls
for (int i = 0; i < S; ++i) {
Update(X[i]);
}
// go through all Y[i] centroids and update min(res, dist[Y[i]][c] + cls[c])
ll res = INF;
for (int i = 0; i < T; ++i) {
res = min(res, GetMin(Y[i]));
}
// Update actuality
++now;
return res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
43608 KB |
Output is correct |
2 |
Correct |
209 ms |
48092 KB |
Output is correct |
3 |
Correct |
220 ms |
48092 KB |
Output is correct |
4 |
Correct |
213 ms |
48088 KB |
Output is correct |
5 |
Incorrect |
265 ms |
48608 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
43608 KB |
Output is correct |
2 |
Correct |
1839 ms |
219696 KB |
Output is correct |
3 |
Execution timed out |
8087 ms |
228324 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
43608 KB |
Output is correct |
2 |
Correct |
209 ms |
48092 KB |
Output is correct |
3 |
Correct |
220 ms |
48092 KB |
Output is correct |
4 |
Correct |
213 ms |
48088 KB |
Output is correct |
5 |
Incorrect |
265 ms |
48608 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |