#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 of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
23 | for (int i = 2; i <= order.size(); i++) {
| ~~^~~~~~~~~~~~~~~
factories.cpp:27:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | for (int j = 0; j < order.size(); j++) {
| ~~^~~~~~~~~~~~~~
factories.cpp:31:70: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
31 | 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 of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
88 | while (st < vec.size() && tin[vec[st]] <= tout[v]) {
| ~~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
100 ms |
169196 KB |
Output is correct |
2 |
Correct |
831 ms |
186988 KB |
Output is correct |
3 |
Correct |
780 ms |
186988 KB |
Output is correct |
4 |
Correct |
818 ms |
186860 KB |
Output is correct |
5 |
Correct |
821 ms |
187244 KB |
Output is correct |
6 |
Correct |
605 ms |
186860 KB |
Output is correct |
7 |
Correct |
774 ms |
186988 KB |
Output is correct |
8 |
Correct |
801 ms |
187244 KB |
Output is correct |
9 |
Correct |
827 ms |
187244 KB |
Output is correct |
10 |
Correct |
598 ms |
186988 KB |
Output is correct |
11 |
Correct |
819 ms |
186860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
86 ms |
168940 KB |
Output is correct |
2 |
Correct |
1399 ms |
248640 KB |
Output is correct |
3 |
Correct |
1393 ms |
250220 KB |
Output is correct |
4 |
Correct |
1119 ms |
248916 KB |
Output is correct |
5 |
Correct |
1352 ms |
279440 KB |
Output is correct |
6 |
Correct |
1471 ms |
252348 KB |
Output is correct |
7 |
Correct |
1075 ms |
203536 KB |
Output is correct |
8 |
Correct |
888 ms |
204264 KB |
Output is correct |
9 |
Correct |
926 ms |
207840 KB |
Output is correct |
10 |
Correct |
1103 ms |
205004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
100 ms |
169196 KB |
Output is correct |
2 |
Correct |
831 ms |
186988 KB |
Output is correct |
3 |
Correct |
780 ms |
186988 KB |
Output is correct |
4 |
Correct |
818 ms |
186860 KB |
Output is correct |
5 |
Correct |
821 ms |
187244 KB |
Output is correct |
6 |
Correct |
605 ms |
186860 KB |
Output is correct |
7 |
Correct |
774 ms |
186988 KB |
Output is correct |
8 |
Correct |
801 ms |
187244 KB |
Output is correct |
9 |
Correct |
827 ms |
187244 KB |
Output is correct |
10 |
Correct |
598 ms |
186988 KB |
Output is correct |
11 |
Correct |
819 ms |
186860 KB |
Output is correct |
12 |
Correct |
86 ms |
168940 KB |
Output is correct |
13 |
Correct |
1399 ms |
248640 KB |
Output is correct |
14 |
Correct |
1393 ms |
250220 KB |
Output is correct |
15 |
Correct |
1119 ms |
248916 KB |
Output is correct |
16 |
Correct |
1352 ms |
279440 KB |
Output is correct |
17 |
Correct |
1471 ms |
252348 KB |
Output is correct |
18 |
Correct |
1075 ms |
203536 KB |
Output is correct |
19 |
Correct |
888 ms |
204264 KB |
Output is correct |
20 |
Correct |
926 ms |
207840 KB |
Output is correct |
21 |
Correct |
1103 ms |
205004 KB |
Output is correct |
22 |
Correct |
2313 ms |
257120 KB |
Output is correct |
23 |
Correct |
2149 ms |
260108 KB |
Output is correct |
24 |
Correct |
2364 ms |
259792 KB |
Output is correct |
25 |
Correct |
2275 ms |
263808 KB |
Output is correct |
26 |
Correct |
2181 ms |
258656 KB |
Output is correct |
27 |
Correct |
2356 ms |
283764 KB |
Output is correct |
28 |
Correct |
1584 ms |
261072 KB |
Output is correct |
29 |
Correct |
2124 ms |
258332 KB |
Output is correct |
30 |
Correct |
2148 ms |
257388 KB |
Output is correct |
31 |
Correct |
2131 ms |
258552 KB |
Output is correct |
32 |
Correct |
1371 ms |
209640 KB |
Output is correct |
33 |
Correct |
906 ms |
205536 KB |
Output is correct |
34 |
Correct |
1205 ms |
201188 KB |
Output is correct |
35 |
Correct |
1195 ms |
201184 KB |
Output is correct |
36 |
Correct |
1178 ms |
201960 KB |
Output is correct |
37 |
Correct |
1191 ms |
201956 KB |
Output is correct |