#include "factories.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define ff first
#define ss second
const ll INF = 1e15;
int dfn[500005], dfnn, efn[500005], prt[19][500005];
int dep[500005];
ll D[500005];
vector<pii> edge[500005];
void dfs0(int x, int p) {
dfn[x] = ++dfnn;
for (auto i : edge[x]) {
if (i.ss == p) continue;
dep[dfnn + 1] = dep[dfn[x]] + 1;
D[dfnn + 1] = D[dfn[x]] + i.ff;
prt[0][dfnn + 1] = dfn[x];
dfs0(i.ss, x);
}
efn[dfn[x]] = dfnn;
}
int C[1000005], cnt, pv, XY[500005];
ll res;
pll dfs(int x) {
ll S = INF, T = INF;
if (XY[x] == 1) S = 0;
if (XY[x] == 2) T = 0;
XY[x] = 0;
while (pv < cnt && C[pv] <= efn[x]) {
int i = C[pv++];
auto r = dfs(i);
S = min(S, r.ff + D[i] - D[x]);
T = min(T, r.ss + D[i] - D[x]);
}
res = min(res, S+T);
return pll(S, T);
}
int lca(int s, int e) {
if (dep[s] < dep[e]) swap(s, e);
for (int i = 18; i >= 0; --i)
if ((dep[s] - dep[e] >> i) & 1) s = prt[i][s];
if (s == e) return s;
for (int i = 18; i >= 0; --i)
if (prt[i][s] != prt[i][e]) s = prt[i][s], e = prt[i][e];
return prt[0][s];
}
ll Query(int S, int X[], int T, int Y[]) {
cnt = 0;
for (int i = 0; i < S; ++i) C[cnt] = dfn[X[i]], XY[C[cnt++]] = 1;
for (int i = 0; i < T; ++i) C[cnt] = dfn[Y[i]], XY[C[cnt++]] = 2;
sort(C, C + cnt);
for (int i = cnt-1; i; --i) C[cnt++] = lca(C[i - 1], C[i]);
sort(C, C + cnt);
cnt = unique(C, C + cnt) - C;
res = INF, pv = 0;
dfs(1);
return res;
}
void Init(int N, int A[], int B[], int D[]) {
for (int i = 0; i < N - 1; ++i) {
edge[A[i]].emplace_back(D[i], B[i]);
edge[B[i]].emplace_back(D[i], A[i]);
}
dfs0(0, 0);
for (int i = 1; i < 19; ++i) for (int j = 1; j <= N; ++j) prt[i][j] = prt[i - 1][prt[i - 1][j]];
}
Compilation message
factories.cpp: In function 'int lca(int, int)':
factories.cpp:49:15: warning: suggest parentheses around '-' inside '>>' [-Wparentheses]
if ((dep[s] - dep[e] >> i) & 1) s = prt[i][s];
~~~~~~~^~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
12672 KB |
Output is correct |
2 |
Correct |
653 ms |
21112 KB |
Output is correct |
3 |
Correct |
686 ms |
30584 KB |
Output is correct |
4 |
Correct |
679 ms |
30712 KB |
Output is correct |
5 |
Correct |
613 ms |
30712 KB |
Output is correct |
6 |
Correct |
477 ms |
30712 KB |
Output is correct |
7 |
Correct |
681 ms |
30840 KB |
Output is correct |
8 |
Correct |
651 ms |
30712 KB |
Output is correct |
9 |
Correct |
627 ms |
30848 KB |
Output is correct |
10 |
Correct |
473 ms |
30584 KB |
Output is correct |
11 |
Correct |
675 ms |
30668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
12416 KB |
Output is correct |
2 |
Correct |
1945 ms |
92748 KB |
Output is correct |
3 |
Correct |
2481 ms |
93256 KB |
Output is correct |
4 |
Correct |
1514 ms |
93276 KB |
Output is correct |
5 |
Correct |
2495 ms |
110096 KB |
Output is correct |
6 |
Correct |
2710 ms |
94584 KB |
Output is correct |
7 |
Correct |
1776 ms |
50424 KB |
Output is correct |
8 |
Correct |
1075 ms |
51692 KB |
Output is correct |
9 |
Correct |
1839 ms |
53496 KB |
Output is correct |
10 |
Correct |
1817 ms |
51960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
12672 KB |
Output is correct |
2 |
Correct |
653 ms |
21112 KB |
Output is correct |
3 |
Correct |
686 ms |
30584 KB |
Output is correct |
4 |
Correct |
679 ms |
30712 KB |
Output is correct |
5 |
Correct |
613 ms |
30712 KB |
Output is correct |
6 |
Correct |
477 ms |
30712 KB |
Output is correct |
7 |
Correct |
681 ms |
30840 KB |
Output is correct |
8 |
Correct |
651 ms |
30712 KB |
Output is correct |
9 |
Correct |
627 ms |
30848 KB |
Output is correct |
10 |
Correct |
473 ms |
30584 KB |
Output is correct |
11 |
Correct |
675 ms |
30668 KB |
Output is correct |
12 |
Correct |
13 ms |
12416 KB |
Output is correct |
13 |
Correct |
1945 ms |
92748 KB |
Output is correct |
14 |
Correct |
2481 ms |
93256 KB |
Output is correct |
15 |
Correct |
1514 ms |
93276 KB |
Output is correct |
16 |
Correct |
2495 ms |
110096 KB |
Output is correct |
17 |
Correct |
2710 ms |
94584 KB |
Output is correct |
18 |
Correct |
1776 ms |
50424 KB |
Output is correct |
19 |
Correct |
1075 ms |
51692 KB |
Output is correct |
20 |
Correct |
1839 ms |
53496 KB |
Output is correct |
21 |
Correct |
1817 ms |
51960 KB |
Output is correct |
22 |
Correct |
2027 ms |
94872 KB |
Output is correct |
23 |
Correct |
2520 ms |
97192 KB |
Output is correct |
24 |
Correct |
1999 ms |
96248 KB |
Output is correct |
25 |
Correct |
2344 ms |
99556 KB |
Output is correct |
26 |
Correct |
3235 ms |
95628 KB |
Output is correct |
27 |
Correct |
2161 ms |
110336 KB |
Output is correct |
28 |
Correct |
1464 ms |
97988 KB |
Output is correct |
29 |
Correct |
3612 ms |
95148 KB |
Output is correct |
30 |
Correct |
3727 ms |
95068 KB |
Output is correct |
31 |
Correct |
3861 ms |
95524 KB |
Output is correct |
32 |
Correct |
1196 ms |
54840 KB |
Output is correct |
33 |
Correct |
833 ms |
52580 KB |
Output is correct |
34 |
Correct |
1024 ms |
48564 KB |
Output is correct |
35 |
Correct |
1042 ms |
48632 KB |
Output is correct |
36 |
Correct |
1157 ms |
49044 KB |
Output is correct |
37 |
Correct |
1302 ms |
49144 KB |
Output is correct |