#include "factories.h"
//#include "grader.cpp"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = (int)5e5 + 7;
int tiktak;
int tin[maxn], tout[maxn];
int lc[20][maxn];
ll h[maxn];
vector<pair<int, int>> gr[maxn];
int numlog[maxn + maxn], pos[maxn], deep[maxn];
vector <int> order;
struct sparse {
pair<int, int> table[20][maxn + maxn];
sparse() {}
void build() {
for (int i = 2; i <= order.size(); i++) {
numlog[i] = numlog[i / 2] + 1;
}
for (int i = 0; i <= numlog[order.size()]; i++) {
for (int j = 0; j < order.size(); j++) {
if (i == 0) {
table[i][j] = {deep[order[j]], order[j]};
} else {
table[i][j] = min(table[i - 1][j], table[i - 1][min(j + (1 << i - 1), (int)order.size() - 1)]);
}
}
}
}
int get(int l, int r) {
if (l > r) swap(l, r);
int lg = numlog[r - l + 1];
return min(table[lg][l], table[lg][r - (1 << lg) + 1]).second;
}
};
sparse tr;
void dfs(int v, int pr) {
tin[v] = ++tiktak;
pos[v] = order.size();
order.push_back(v);
for (auto id : gr[v]) {
int to = id.first;
int w = id.second;
if (to == pr) continue;
h[to] = h[v] + w;
deep[to] = deep[v] + 1;
dfs(to, v);
pos[v] = order.size();
order.push_back(v);
}
tout[v] = tiktak;
}
int lca(int a, int b) {
return tr.get(pos[a], pos[b]);
}
ll dist(int a, int b) {
return h[a] + h[b] - 2 * h[lca(a, b)];
}
void Init(int N, int A[], int B[], int D[]) {
for (int i = 0; i < N - 1; i++) {
gr[A[i]].push_back({B[i], D[i]});
gr[B[i]].push_back({A[i], D[i]});
}
dfs(0, 0);
tr.build();
}
ll dp1[maxn], dp2[maxn];
int col[maxn];
vector <int> vec;
int st;
void dfs1(int v, int pr, ll &ans) {
dp1[v] = dp2[v] = 1e18;
if (col[v] == 1) dp1[v] = 0;
if (col[v] == 2) dp2[v] = 0;
col[v] = 0;
while (st < vec.size() && tin[vec[st]] <= tout[v]) {
int to = vec[st]; st++;
dfs1(to, v, ans);
dp1[v] = min(dp1[v], dp1[to] + h[to] - h[v]);
dp2[v] = min(dp2[v], dp2[to] + h[to] - h[v]);
}
ans = min(ans, dp1[v] + dp2[v]);
}
long long Query(int S, int X[], int T, int Y[]) {
ll ans = 1e18;
for (int i = 0; i < S; i++) {
vec.push_back(X[i]);
col[X[i]] = 1;
}
for (int i = 0; i < T; i++) {
vec.push_back(Y[i]);
col[Y[i]] = 2;
}
sort(vec.begin(), vec.end(), [&](const int &A, const int &B) {
return tin[A] < tin[B];
});
for (int i = vec.size() - 2; i >= 0; i--) {
vec.push_back(lca(vec[i], vec[i + 1]));
}
sort(vec.begin(), vec.end(), [&](const int &A, const int &B) {
return tin[A] < tin[B];
});
vec.resize(unique(vec.begin(), vec.end()) - vec.begin());
st = 1;
dfs1(vec[0], vec[0], ans);
vec.clear();
return ans;
}
Compilation message
factories.cpp: In member function 'void sparse::build()':
factories.cpp:23:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 2; i <= order.size(); i++) {
~~^~~~~~~~~~~~~~~
factories.cpp:27:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j < order.size(); j++) {
~~^~~~~~~~~~~~~~
factories.cpp:31:70: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
table[i][j] = min(table[i - 1][j], table[i - 1][min(j + (1 << i - 1), (int)order.size() - 1)]);
~~^~~
factories.cpp: In function 'void dfs1(int, int, long long int&)':
factories.cpp:88:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while (st < vec.size() && tin[vec[st]] <= tout[v]) {
~~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
157 ms |
169080 KB |
Output is correct |
2 |
Correct |
888 ms |
177364 KB |
Output is correct |
3 |
Correct |
864 ms |
177564 KB |
Output is correct |
4 |
Correct |
879 ms |
177428 KB |
Output is correct |
5 |
Correct |
886 ms |
177452 KB |
Output is correct |
6 |
Correct |
653 ms |
177336 KB |
Output is correct |
7 |
Correct |
850 ms |
177276 KB |
Output is correct |
8 |
Correct |
860 ms |
177332 KB |
Output is correct |
9 |
Correct |
881 ms |
177448 KB |
Output is correct |
10 |
Correct |
651 ms |
177368 KB |
Output is correct |
11 |
Correct |
846 ms |
177288 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
146 ms |
168840 KB |
Output is correct |
2 |
Correct |
1495 ms |
229712 KB |
Output is correct |
3 |
Correct |
1503 ms |
230300 KB |
Output is correct |
4 |
Correct |
1307 ms |
230400 KB |
Output is correct |
5 |
Correct |
1547 ms |
247000 KB |
Output is correct |
6 |
Correct |
1553 ms |
231700 KB |
Output is correct |
7 |
Correct |
1043 ms |
189152 KB |
Output is correct |
8 |
Correct |
851 ms |
190052 KB |
Output is correct |
9 |
Correct |
925 ms |
192120 KB |
Output is correct |
10 |
Correct |
1086 ms |
190608 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
157 ms |
169080 KB |
Output is correct |
2 |
Correct |
888 ms |
177364 KB |
Output is correct |
3 |
Correct |
864 ms |
177564 KB |
Output is correct |
4 |
Correct |
879 ms |
177428 KB |
Output is correct |
5 |
Correct |
886 ms |
177452 KB |
Output is correct |
6 |
Correct |
653 ms |
177336 KB |
Output is correct |
7 |
Correct |
850 ms |
177276 KB |
Output is correct |
8 |
Correct |
860 ms |
177332 KB |
Output is correct |
9 |
Correct |
881 ms |
177448 KB |
Output is correct |
10 |
Correct |
651 ms |
177368 KB |
Output is correct |
11 |
Correct |
846 ms |
177288 KB |
Output is correct |
12 |
Correct |
146 ms |
168840 KB |
Output is correct |
13 |
Correct |
1495 ms |
229712 KB |
Output is correct |
14 |
Correct |
1503 ms |
230300 KB |
Output is correct |
15 |
Correct |
1307 ms |
230400 KB |
Output is correct |
16 |
Correct |
1547 ms |
247000 KB |
Output is correct |
17 |
Correct |
1553 ms |
231700 KB |
Output is correct |
18 |
Correct |
1043 ms |
189152 KB |
Output is correct |
19 |
Correct |
851 ms |
190052 KB |
Output is correct |
20 |
Correct |
925 ms |
192120 KB |
Output is correct |
21 |
Correct |
1086 ms |
190608 KB |
Output is correct |
22 |
Correct |
2495 ms |
232884 KB |
Output is correct |
23 |
Correct |
2287 ms |
235128 KB |
Output is correct |
24 |
Correct |
2501 ms |
258440 KB |
Output is correct |
25 |
Correct |
2312 ms |
262084 KB |
Output is correct |
26 |
Correct |
2268 ms |
256928 KB |
Output is correct |
27 |
Correct |
2616 ms |
272868 KB |
Output is correct |
28 |
Correct |
1789 ms |
260808 KB |
Output is correct |
29 |
Correct |
2249 ms |
256608 KB |
Output is correct |
30 |
Correct |
2252 ms |
256188 KB |
Output is correct |
31 |
Correct |
2314 ms |
256612 KB |
Output is correct |
32 |
Correct |
1657 ms |
207656 KB |
Output is correct |
33 |
Correct |
996 ms |
205276 KB |
Output is correct |
34 |
Correct |
1254 ms |
201300 KB |
Output is correct |
35 |
Correct |
1245 ms |
201340 KB |
Output is correct |
36 |
Correct |
1218 ms |
201792 KB |
Output is correct |
37 |
Correct |
1226 ms |
201588 KB |
Output is correct |