#include <bits/stdc++.h>
#include "factories.h"
//#include "grader.cpp"
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define st first
#define nd second
#define pb push_back
#define INF 1e18
using namespace std;
const int maxN = 500050;
const int L = log2(maxN) + 10;
vector<pll> adj[maxN];
set<ll> s;
bool mark[maxN];
ll sz[maxN], mn[maxN], up[maxN], dist[maxN][L], lvl[maxN], P[maxN][L], updist[maxN][L], vs[maxN], now;
void dfs2(int a, int prt, ll cur) {
lvl[a] = lvl[prt]+1;
P[a][0] = prt;
updist[a][0] = cur;
for (int i = 1; i < L; ++i) {
P[a][i] = P[P[a][i-1]][i-1];
updist[a][i] = updist[P[a][i-1]][i-1] + updist[a][i-1];
}
for (auto [b, w] : adj[a]) if (b != prt) dfs2(b, a, w);
}
long long lca(int a, int b) {
if (lvl[a] < lvl[b]) swap(a, b);
int j = lvl[a] - lvl[b];
ll dis = 0;
for (int i = 0; i < L; ++i) {
if (j & (1<<i)) {
dis += updist[a][i];
a = P[a][i];
}
}
if (a==b) return dis;
for (int i = L-1; i >= 0; --i) {
if (P[a][i] != P[b][i]) {
dis += (updist[a][i] + updist[b][i]);
a = P[a][i]; b = P[b][i];
}
}
return dis + updist[a][0] + updist[b][0];
}
void computedist(int n) {
for (int i = 0; i < n; ++i) {
int cnt = 0;
for (int j = i; j != -1; j = up[j]) {
dist[i][cnt++] = lca(i, j);
}
}
}
int dfs(int i, int prt=-1) {
sz[i] = 1;
for (auto [j, w] : adj[i]) {
if (j == prt || mark[j]) continue;
sz[i] += dfs(j, i);
}
return sz[i];
}
int find_centroid(int i, int m, int prt=-1) {
for (auto [j, w] : adj[i]) {
if (j == prt || mark[j]) continue;
if (sz[j] > m/2) return find_centroid(j, m, i);
}
return i;
}
int decom(int x=0) {
int cen = find_centroid(x, dfs(x));
mark[cen] = 1;
for (auto [j, w] : adj[cen]) {
if (mark[j]) continue;
int c = decom(j);
up[c] = cen;
}
return cen;
}
void update(int v) {
int cur = v, cnt = 0;
while (cur != -1) {
if (vs[cur] != now) {
vs[cur] = now;
mn[cur] = INF;
}
mn[cur] = min(mn[cur], dist[v][cnt]);
cur = up[cur];
cnt++;
}
}
ll query(int v) {
ll ans = INF, cur = v, cnt = 0;
while (cur != -1) {
if (vs[cur] != now) {
vs[cur] = now;
mn[cur] = INF;
}
ans = min(ans, mn[cur] + dist[v][cnt]);
cur = up[cur];
cnt++;
}
return ans;
}
void Init(int N, int A[], int B[], int D[]) {
for (int i = 0; i < N-1; ++i) {
adj[A[i]].pb({B[i], D[i]});
adj[B[i]].pb({A[i], D[i]});
}
for (int i = 0; i < N; ++i) mn[i] = INF;
memset(up, -1, sizeof(up));
decom();
dfs2(0, -1, 0);
computedist(N);
}
ll Query(int S, int X[], int T, int Y[]) {
s.clear();
for (int i = 0; i < S; ++i) update(X[i]);
ll ans = INF;
for (int i = 0; i < T; ++i) ans = min(ans, query(Y[i]));
now++;
return ans;
}
Compilation message
factories.cpp: In function 'void dfs2(int, int, long long int)':
factories.cpp:29:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
29 | for (auto [b, w] : adj[a]) if (b != prt) dfs2(b, a, w);
| ^
factories.cpp: In function 'int dfs(int, int)':
factories.cpp:63:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
63 | for (auto [j, w] : adj[i]) {
| ^
factories.cpp: In function 'int find_centroid(int, int, int)':
factories.cpp:71:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
71 | for (auto [j, w] : adj[i]) {
| ^
factories.cpp: In function 'int decom(int)':
factories.cpp:82:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
82 | for (auto [j, w] : adj[cen]) {
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
16596 KB |
Output is correct |
2 |
Correct |
238 ms |
27772 KB |
Output is correct |
3 |
Correct |
262 ms |
27856 KB |
Output is correct |
4 |
Correct |
267 ms |
27780 KB |
Output is correct |
5 |
Correct |
274 ms |
28128 KB |
Output is correct |
6 |
Correct |
186 ms |
27788 KB |
Output is correct |
7 |
Correct |
267 ms |
27852 KB |
Output is correct |
8 |
Correct |
270 ms |
27708 KB |
Output is correct |
9 |
Correct |
273 ms |
28024 KB |
Output is correct |
10 |
Correct |
182 ms |
27852 KB |
Output is correct |
11 |
Correct |
264 ms |
27700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
16468 KB |
Output is correct |
2 |
Correct |
2523 ms |
399380 KB |
Output is correct |
3 |
Correct |
4900 ms |
402304 KB |
Output is correct |
4 |
Correct |
856 ms |
397004 KB |
Output is correct |
5 |
Correct |
7259 ms |
427020 KB |
Output is correct |
6 |
Correct |
5212 ms |
422328 KB |
Output is correct |
7 |
Correct |
1004 ms |
115168 KB |
Output is correct |
8 |
Correct |
366 ms |
115156 KB |
Output is correct |
9 |
Correct |
1279 ms |
119304 KB |
Output is correct |
10 |
Correct |
990 ms |
116540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
16596 KB |
Output is correct |
2 |
Correct |
238 ms |
27772 KB |
Output is correct |
3 |
Correct |
262 ms |
27856 KB |
Output is correct |
4 |
Correct |
267 ms |
27780 KB |
Output is correct |
5 |
Correct |
274 ms |
28128 KB |
Output is correct |
6 |
Correct |
186 ms |
27788 KB |
Output is correct |
7 |
Correct |
267 ms |
27852 KB |
Output is correct |
8 |
Correct |
270 ms |
27708 KB |
Output is correct |
9 |
Correct |
273 ms |
28024 KB |
Output is correct |
10 |
Correct |
182 ms |
27852 KB |
Output is correct |
11 |
Correct |
264 ms |
27700 KB |
Output is correct |
12 |
Correct |
10 ms |
16468 KB |
Output is correct |
13 |
Correct |
2523 ms |
399380 KB |
Output is correct |
14 |
Correct |
4900 ms |
402304 KB |
Output is correct |
15 |
Correct |
856 ms |
397004 KB |
Output is correct |
16 |
Correct |
7259 ms |
427020 KB |
Output is correct |
17 |
Correct |
5212 ms |
422328 KB |
Output is correct |
18 |
Correct |
1004 ms |
115168 KB |
Output is correct |
19 |
Correct |
366 ms |
115156 KB |
Output is correct |
20 |
Correct |
1279 ms |
119304 KB |
Output is correct |
21 |
Correct |
990 ms |
116540 KB |
Output is correct |
22 |
Correct |
3058 ms |
425216 KB |
Output is correct |
23 |
Correct |
2880 ms |
427728 KB |
Output is correct |
24 |
Correct |
5617 ms |
428444 KB |
Output is correct |
25 |
Correct |
5574 ms |
432424 KB |
Output is correct |
26 |
Correct |
5567 ms |
428592 KB |
Output is correct |
27 |
Execution timed out |
8054 ms |
449684 KB |
Time limit exceeded |
28 |
Halted |
0 ms |
0 KB |
- |