#include <bits/stdc++.h>
#include "factories.h"
using namespace std;
using ll = long long;
using db = long double;
using str = string;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using pd = pair<db, db>;
using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<ll>;
using vd = vector<db>;
using vs = vector<str>;
using vpi = vector<pi>;
using vpl = vector<pl>;
using vpd = vector<pd>;
#define mp make_pair
#define f first
#define s second
#define bg(x) begin(x)
#define all(x) bg(x), end(x)
#define sor(x) sort(all(x))
#define ft front()
#define bk back()
#define pb push_back
#define pf push_front
#define lb lower_bound
#define ub upper_bound
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, a) FOR(i, 0, a)
#define ROF(i, a, b) for (int i = (b) - 1; i >= (a); i--)
#define R0F(i, a) ROF(i, 0, a)
#define EACH(a, x) for (auto& a : x)
const int MOD = 1e9 + 7;
const int MX = 5e5 + 10;
const ll INF = 1e18;
int N; vpi adj[MX]; int sub[MX], num[MX], par[MX]; ll dist[MX][20], ans[MX]; bool vis[MX];
void DFS1(int X, int P) {
sub[X] = 1; EACH(Y, adj[X]) if (Y.f != P && !vis[Y.f]) { DFS1(Y.f, X); sub[X] += sub[Y.f]; }
}
int centroid(int X, int P, int S) {
EACH(Y, adj[X]) if (Y.f != P && !vis[Y.f] && sub[Y.f] > S / 2) return centroid(Y.f, X, S); return X;
}
void DFS2(int X, int P, ll D) {
dist[X][num[X]++] = D; EACH(Y, adj[X]) if (Y.f != P && !vis[Y.f]) DFS2(Y.f, X, D + Y.s);
}
void decomp(int X, int P) {
DFS1(X, 0); int C = centroid(X, 0, sub[X]); vis[C] = true; par[C] = P;
DFS2(C, P, 0); EACH(Y, adj[C]) if (!vis[Y.f]) decomp(Y.f, C);
}
void update(int X, int T) {
int Y = X; int ind = num[X] - 1;
while (Y != 0) { if (T == 1) ans[Y] = min(ans[Y], dist[X][ind]); else ans[Y] = INF; Y = par[Y]; ind--; }
}
ll query(int X) {
int Y = X; int ind = num[X] - 1; ll res = INF;
while (Y != 0) { res = min(res, ans[Y] + dist[X][ind]); Y = par[Y]; ind--; } return res;
}
void Init(int _N, int A[], int B[], int D[]) {
N = _N; F0R(i, N - 1) { A[i]++, B[i]++; adj[A[i]].pb({B[i], D[i]}), adj[B[i]].pb({A[i], D[i]}); }
decomp(1, 0); FOR(i, 1, N + 1) ans[i] = INF;
}
ll Query(int S, int X[], int T, int Y[]) {
ll res = INF; F0R(i, S) update(++X[i], 1);
F0R(i, T) res = min(res, query(++Y[i])); F0R(i, S) update(X[i], 0); return res;
}
Compilation message
factories.cpp: In function 'int centroid(int, int, int)':
factories.cpp:42:20: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
42 | #define EACH(a, x) for (auto& a : x)
| ^~~
factories.cpp:54:5: note: in expansion of macro 'EACH'
54 | EACH(Y, adj[X]) if (Y.f != P && !vis[Y.f] && sub[Y.f] > S / 2) return centroid(Y.f, X, S); return X;
| ^~~~
factories.cpp:54:96: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
54 | EACH(Y, adj[X]) if (Y.f != P && !vis[Y.f] && sub[Y.f] > S / 2) return centroid(Y.f, X, S); return X;
| ^~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
12620 KB |
Output is correct |
2 |
Correct |
362 ms |
30664 KB |
Output is correct |
3 |
Correct |
385 ms |
30708 KB |
Output is correct |
4 |
Correct |
386 ms |
30712 KB |
Output is correct |
5 |
Correct |
404 ms |
30996 KB |
Output is correct |
6 |
Correct |
284 ms |
30792 KB |
Output is correct |
7 |
Correct |
385 ms |
30748 KB |
Output is correct |
8 |
Correct |
399 ms |
30816 KB |
Output is correct |
9 |
Correct |
401 ms |
30852 KB |
Output is correct |
10 |
Correct |
287 ms |
30776 KB |
Output is correct |
11 |
Correct |
407 ms |
30656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
12236 KB |
Output is correct |
2 |
Correct |
2186 ms |
132096 KB |
Output is correct |
3 |
Correct |
3426 ms |
151420 KB |
Output is correct |
4 |
Correct |
903 ms |
151024 KB |
Output is correct |
5 |
Correct |
4241 ms |
174832 KB |
Output is correct |
6 |
Correct |
3229 ms |
153680 KB |
Output is correct |
7 |
Correct |
993 ms |
58440 KB |
Output is correct |
8 |
Correct |
496 ms |
59124 KB |
Output is correct |
9 |
Correct |
1103 ms |
62072 KB |
Output is correct |
10 |
Correct |
967 ms |
59748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
12620 KB |
Output is correct |
2 |
Correct |
362 ms |
30664 KB |
Output is correct |
3 |
Correct |
385 ms |
30708 KB |
Output is correct |
4 |
Correct |
386 ms |
30712 KB |
Output is correct |
5 |
Correct |
404 ms |
30996 KB |
Output is correct |
6 |
Correct |
284 ms |
30792 KB |
Output is correct |
7 |
Correct |
385 ms |
30748 KB |
Output is correct |
8 |
Correct |
399 ms |
30816 KB |
Output is correct |
9 |
Correct |
401 ms |
30852 KB |
Output is correct |
10 |
Correct |
287 ms |
30776 KB |
Output is correct |
11 |
Correct |
407 ms |
30656 KB |
Output is correct |
12 |
Correct |
9 ms |
12236 KB |
Output is correct |
13 |
Correct |
2186 ms |
132096 KB |
Output is correct |
14 |
Correct |
3426 ms |
151420 KB |
Output is correct |
15 |
Correct |
903 ms |
151024 KB |
Output is correct |
16 |
Correct |
4241 ms |
174832 KB |
Output is correct |
17 |
Correct |
3229 ms |
153680 KB |
Output is correct |
18 |
Correct |
993 ms |
58440 KB |
Output is correct |
19 |
Correct |
496 ms |
59124 KB |
Output is correct |
20 |
Correct |
1103 ms |
62072 KB |
Output is correct |
21 |
Correct |
967 ms |
59748 KB |
Output is correct |
22 |
Correct |
2385 ms |
157908 KB |
Output is correct |
23 |
Correct |
2518 ms |
160544 KB |
Output is correct |
24 |
Correct |
3745 ms |
159812 KB |
Output is correct |
25 |
Correct |
3720 ms |
163536 KB |
Output is correct |
26 |
Correct |
3688 ms |
159892 KB |
Output is correct |
27 |
Correct |
4627 ms |
178972 KB |
Output is correct |
28 |
Correct |
1042 ms |
161496 KB |
Output is correct |
29 |
Correct |
3809 ms |
159880 KB |
Output is correct |
30 |
Correct |
3882 ms |
159176 KB |
Output is correct |
31 |
Correct |
3893 ms |
159612 KB |
Output is correct |
32 |
Correct |
1164 ms |
62936 KB |
Output is correct |
33 |
Correct |
485 ms |
59612 KB |
Output is correct |
34 |
Correct |
777 ms |
56260 KB |
Output is correct |
35 |
Correct |
779 ms |
56132 KB |
Output is correct |
36 |
Correct |
986 ms |
56660 KB |
Output is correct |
37 |
Correct |
971 ms |
56628 KB |
Output is correct |