#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long
#define mp make_pair
const int l = 22;
const ll inf = 2e15;
const int MAX_N = 1e6 + 15;
int n, m, sz[MAX_N], tin[MAX_N], tout[MAX_N], last, loglog[MAX_N];
pair <int, int> st[MAX_N][l];
vector <pair <ll, int>> cur;
ll minn[MAX_N];
vector <int> g[MAX_N], d[MAX_N];
int pCentroid[MAX_N];
ll dist[MAX_N], dd[MAX_N];
bool deleted[MAX_N];
void start(int v, int p) {
cur.pb(mp(dd[v], v));
for(int i = 0; i < g[v].size(); ++i)
if(g[v][i] != p) {
dist[g[v][i]] = dist[v] + d[v][i];
dd[g[v][i]] = dd[v] + 1;
start(g[v][i], v);
cur.pb(mp(dd[v], v));
}
}
void buildlca() {
for(int i = 0; i < cur.size(); ++i)
if(!tin[cur[i].second])
tin[cur[i].second] = i;
for(int i = 2; i < MAX_N; ++i)
loglog[i] = loglog[i >> 1] + 1;
for(int i = 0; i < cur.size(); ++i)
st[i][0] = cur[i];
for(int j = 1; (1 << j) < cur.size(); ++j)
for(int i = 0; i + (1 << (j - 1)) < cur.size(); ++i)
st[i][j] = min(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
}
int lca(int a, int b) {
int l = min(tin[a], tin[b]), r = max(tin[a], tin[b]);
int lg = loglog[r - l + 1];
return min(st[l][lg], st[r - (1 << lg) + 1][lg]).second;
}
void build(int v, int p = -1) {
sz[v] = 1;
for(auto to : g[v])
if(to != p && !deleted[to]) {
build(to, v);
sz[v] += sz[to];
}
}
int centroid(int v, int p, int n) {
for(auto to : g[v])
if(!deleted[to] && to != p && sz[to] > n / 2)
return centroid(to, v, n);
return v;
}
void process(int node, int p = -1) {
build(node, -1);
int center = centroid(node, node, sz[node]);
deleted[center] = true;
if(p == -1)
p = center;
pCentroid[center] = p;
for(int to : g[center])
if(!deleted[to])
process(to, center);
}
void Init(int N, int A[], int B[], int D[]) {
n = N;
for(int i = 0; i < n; ++i) {
++A[i];
++B[i];
g[A[i]].pb(B[i]);
d[A[i]].pb(D[i]);
g[B[i]].pb(A[i]);
d[B[i]].pb(D[i]);
}
fill(minn, minn + MAX_N, inf);
start(1, 1);
buildlca();
process(1);
}
long long Query(int S, int X[], int T, int Y[]) {
ll ans = inf;
for(int i = 0; i < S; ++i)
++X[i];
for(int i = 0; i < T; ++i)
++Y[i];
for(int i = 0; i < S; ++i) {
int v = X[i];
while(1) {
minn[v] = min(minn[v], dist[X[i]] + dist[v] - 2 * dist[lca(X[i], v)]);
if(v == pCentroid[v])
break;
v = pCentroid[v];
}
}
for(int i = 0; i < T; ++i) {
int v = Y[i];
while(1) {
ans = min(ans, dist[Y[i]] + dist[v] - 2 * dist[lca(Y[i], v)] + minn[v]);
if(v == pCentroid[v])
break;
v = pCentroid[v];
}
}
for(int i = 0; i < S; ++i) {
int v = X[i];
while(1) {
minn[v] = inf;
if(v == pCentroid[v])
break;
v = pCentroid[v];
}
}
return ans;
}
Compilation message
factories.cpp: In function 'void start(int, int)':
factories.cpp:23:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < g[v].size(); ++i)
~~^~~~~~~~~~~~~
factories.cpp: In function 'void buildlca()':
factories.cpp:33:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < cur.size(); ++i)
~~^~~~~~~~~~~~
factories.cpp:38:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < cur.size(); ++i)
~~^~~~~~~~~~~~
factories.cpp:40:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j = 1; (1 << j) < cur.size(); ++j)
~~~~~~~~~^~~~~~~~~~~~
factories.cpp:41:37: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i + (1 << (j - 1)) < cur.size(); ++i)
~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
68 ms |
59896 KB |
Output is correct |
2 |
Correct |
690 ms |
70780 KB |
Output is correct |
3 |
Correct |
867 ms |
70744 KB |
Output is correct |
4 |
Correct |
858 ms |
70780 KB |
Output is correct |
5 |
Correct |
837 ms |
71156 KB |
Output is correct |
6 |
Correct |
427 ms |
70648 KB |
Output is correct |
7 |
Correct |
854 ms |
70792 KB |
Output is correct |
8 |
Correct |
875 ms |
70820 KB |
Output is correct |
9 |
Correct |
855 ms |
71216 KB |
Output is correct |
10 |
Correct |
424 ms |
70764 KB |
Output is correct |
11 |
Correct |
857 ms |
70848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
58 ms |
59512 KB |
Output is correct |
2 |
Correct |
3794 ms |
306900 KB |
Output is correct |
3 |
Correct |
4889 ms |
310096 KB |
Output is correct |
4 |
Correct |
1895 ms |
309832 KB |
Output is correct |
5 |
Correct |
5351 ms |
346600 KB |
Output is correct |
6 |
Correct |
4838 ms |
310996 KB |
Output is correct |
7 |
Correct |
2500 ms |
120288 KB |
Output is correct |
8 |
Correct |
854 ms |
120992 KB |
Output is correct |
9 |
Correct |
2338 ms |
125540 KB |
Output is correct |
10 |
Correct |
2640 ms |
121300 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
68 ms |
59896 KB |
Output is correct |
2 |
Correct |
690 ms |
70780 KB |
Output is correct |
3 |
Correct |
867 ms |
70744 KB |
Output is correct |
4 |
Correct |
858 ms |
70780 KB |
Output is correct |
5 |
Correct |
837 ms |
71156 KB |
Output is correct |
6 |
Correct |
427 ms |
70648 KB |
Output is correct |
7 |
Correct |
854 ms |
70792 KB |
Output is correct |
8 |
Correct |
875 ms |
70820 KB |
Output is correct |
9 |
Correct |
855 ms |
71216 KB |
Output is correct |
10 |
Correct |
424 ms |
70764 KB |
Output is correct |
11 |
Correct |
857 ms |
70848 KB |
Output is correct |
12 |
Correct |
58 ms |
59512 KB |
Output is correct |
13 |
Correct |
3794 ms |
306900 KB |
Output is correct |
14 |
Correct |
4889 ms |
310096 KB |
Output is correct |
15 |
Correct |
1895 ms |
309832 KB |
Output is correct |
16 |
Correct |
5351 ms |
346600 KB |
Output is correct |
17 |
Correct |
4838 ms |
310996 KB |
Output is correct |
18 |
Correct |
2500 ms |
120288 KB |
Output is correct |
19 |
Correct |
854 ms |
120992 KB |
Output is correct |
20 |
Correct |
2338 ms |
125540 KB |
Output is correct |
21 |
Correct |
2640 ms |
121300 KB |
Output is correct |
22 |
Correct |
4740 ms |
308152 KB |
Output is correct |
23 |
Correct |
5685 ms |
310500 KB |
Output is correct |
24 |
Correct |
6252 ms |
311228 KB |
Output is correct |
25 |
Correct |
6510 ms |
314808 KB |
Output is correct |
26 |
Correct |
6439 ms |
311612 KB |
Output is correct |
27 |
Correct |
7001 ms |
366020 KB |
Output is correct |
28 |
Correct |
2286 ms |
338012 KB |
Output is correct |
29 |
Correct |
6778 ms |
335072 KB |
Output is correct |
30 |
Correct |
6969 ms |
334332 KB |
Output is correct |
31 |
Correct |
6431 ms |
335300 KB |
Output is correct |
32 |
Correct |
2523 ms |
137820 KB |
Output is correct |
33 |
Correct |
904 ms |
132660 KB |
Output is correct |
34 |
Correct |
2008 ms |
128800 KB |
Output is correct |
35 |
Correct |
1943 ms |
128988 KB |
Output is correct |
36 |
Correct |
2581 ms |
129632 KB |
Output is correct |
37 |
Correct |
2449 ms |
129696 KB |
Output is correct |