#include "factories.h"
#include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
using namespace std;
const ll INF = 1e18;
#define MAX_N 500000
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][20];
ll dist[MAX_N][20];
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[]) {
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
37712 KB |
Output is correct |
2 |
Correct |
200 ms |
48880 KB |
Output is correct |
3 |
Correct |
215 ms |
48728 KB |
Output is correct |
4 |
Correct |
227 ms |
48916 KB |
Output is correct |
5 |
Incorrect |
264 ms |
49156 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
37468 KB |
Output is correct |
2 |
Correct |
1619 ms |
197532 KB |
Output is correct |
3 |
Execution timed out |
8022 ms |
196848 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
37712 KB |
Output is correct |
2 |
Correct |
200 ms |
48880 KB |
Output is correct |
3 |
Correct |
215 ms |
48728 KB |
Output is correct |
4 |
Correct |
227 ms |
48916 KB |
Output is correct |
5 |
Incorrect |
264 ms |
49156 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |