#include "factories.h"
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
//#define Sz(x) int((x).size())
//#define All(x) (x).begin(), (x).end()
//#define wtf(x) cout<<#x <<" : " <<x <<endl
const ll inf = 1e18, maxn = 1e6 + 10, Log = 25;
int n, par[maxn][Log], st[maxn], fn[maxn], h[maxn], cnt[maxn], tim;
ll d[maxn], dp[maxn][2], ans;
vector<pair<int, ll> > G[maxn], child[maxn];
bool cmp(int i, int j) {
return st[i] < st[j];
}
void dfs(int x, int p = 0) {
st[x] = tim++;
par[x][0] = p;
for(int i = 1; i < Log; i++) par[x][i] = par[par[x][i - 1]][i - 1];
for(auto i : G[x]) {
if(i.first == p) continue;
h[i.first] = h[x] + 1, d[i.first] = d[x] + i.second;
dfs(i.first, x);
}
fn[x] = tim++;
}
int get_par(int x, int y) {
for(int i = 0; i < Log; i++) {
if((y >> i) & 1) x = par[x][i];
}
return x;
}
int lca(int x, int y) {
if(h[x] > h[y]) swap(x, y);
y = get_par(y, h[y] - h[x]);
if(x == y) return x;
for(int i = Log - 1; i >= 0; i--) {
if(par[x][i] != par[y][i]) {
x = par[x][i], y = par[y][i];
}
}
return par[x][0];
}
void main_dfs(int x) {
dp[x][0] = dp[x][1] = inf;
if(cnt[x] == 0) dp[x][0] = 0;
if(cnt[x] == 1) dp[x][1] = 0;
for(auto i : child[x]) {
main_dfs(i.first);
dp[x][0] = min(dp[x][0], dp[i.first][0] + i.second);
dp[x][1] = min(dp[x][1], dp[i.first][1] + i.second);
}
ans = min(ans, dp[x][0] + dp[x][1]);
}
void Init(int N, int A[], int B[], int D[]) {
n = N;
for(int i = 0; i < n - 1; i++) {
G[A[i]].push_back({B[i], D[i]});
G[B[i]].push_back({A[i], D[i]});
}
dfs(0);
for(int i = 0; i < n; i++) cnt[i] = -1;
}
long long Query(int S, int X[], int T, int Y[]) {
vector<int> vc;
for(int i = 0; i < S; i++) vc.push_back(X[i]), cnt[X[i]] = 0;
for(int i = 0; i < T; i++) vc.push_back(Y[i]), cnt[Y[i]] = 1;
sort(vc.begin(), vc.end(), cmp);
int sz = vc.size();
for(int i = 0; i < sz - 1; i++) {
vc.push_back(lca(vc[i], vc[i + 1]));
}
sort(vc.begin(), vc.end(), cmp);
vc.erase(unique(vc.begin(), vc.end()), vc.end());
stack<int> st;
st.push(vc[0]);
for(int i = 1; i < vc.size(); i++) {
int v = vc[i];
while(fn[st.top()] < fn[v]) st.pop();
child[st.top()].push_back({v, d[v] - d[st.top()]});
st.push(v);
}
ans = inf;
main_dfs(vc[0]);
for(auto i : vc) {
child[i].clear();
cnt[i] = -1;
dp[i][0] = dp[i][1] = inf;
}
return ans;
}
Compilation message
factories.cpp: In function 'long long int Query(int, int*, int, int*)':
factories.cpp:82:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
82 | for(int i = 1; i < vc.size(); i++) {
| ~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
47708 KB |
Output is correct |
2 |
Correct |
892 ms |
56560 KB |
Output is correct |
3 |
Correct |
896 ms |
56456 KB |
Output is correct |
4 |
Correct |
938 ms |
56544 KB |
Output is correct |
5 |
Correct |
855 ms |
56864 KB |
Output is correct |
6 |
Correct |
592 ms |
56396 KB |
Output is correct |
7 |
Correct |
875 ms |
56416 KB |
Output is correct |
8 |
Correct |
887 ms |
56628 KB |
Output is correct |
9 |
Correct |
814 ms |
56868 KB |
Output is correct |
10 |
Correct |
617 ms |
56420 KB |
Output is correct |
11 |
Correct |
878 ms |
56672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
47488 KB |
Output is correct |
2 |
Correct |
1459 ms |
154424 KB |
Output is correct |
3 |
Correct |
2028 ms |
158944 KB |
Output is correct |
4 |
Correct |
1037 ms |
170188 KB |
Output is correct |
5 |
Correct |
2139 ms |
211900 KB |
Output is correct |
6 |
Correct |
2183 ms |
178960 KB |
Output is correct |
7 |
Correct |
1800 ms |
91652 KB |
Output is correct |
8 |
Correct |
895 ms |
91196 KB |
Output is correct |
9 |
Correct |
1791 ms |
98256 KB |
Output is correct |
10 |
Correct |
1714 ms |
93468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
47708 KB |
Output is correct |
2 |
Correct |
892 ms |
56560 KB |
Output is correct |
3 |
Correct |
896 ms |
56456 KB |
Output is correct |
4 |
Correct |
938 ms |
56544 KB |
Output is correct |
5 |
Correct |
855 ms |
56864 KB |
Output is correct |
6 |
Correct |
592 ms |
56396 KB |
Output is correct |
7 |
Correct |
875 ms |
56416 KB |
Output is correct |
8 |
Correct |
887 ms |
56628 KB |
Output is correct |
9 |
Correct |
814 ms |
56868 KB |
Output is correct |
10 |
Correct |
617 ms |
56420 KB |
Output is correct |
11 |
Correct |
878 ms |
56672 KB |
Output is correct |
12 |
Correct |
28 ms |
47488 KB |
Output is correct |
13 |
Correct |
1459 ms |
154424 KB |
Output is correct |
14 |
Correct |
2028 ms |
158944 KB |
Output is correct |
15 |
Correct |
1037 ms |
170188 KB |
Output is correct |
16 |
Correct |
2139 ms |
211900 KB |
Output is correct |
17 |
Correct |
2183 ms |
178960 KB |
Output is correct |
18 |
Correct |
1800 ms |
91652 KB |
Output is correct |
19 |
Correct |
895 ms |
91196 KB |
Output is correct |
20 |
Correct |
1791 ms |
98256 KB |
Output is correct |
21 |
Correct |
1714 ms |
93468 KB |
Output is correct |
22 |
Correct |
2880 ms |
189368 KB |
Output is correct |
23 |
Correct |
2562 ms |
190552 KB |
Output is correct |
24 |
Correct |
3111 ms |
194292 KB |
Output is correct |
25 |
Correct |
3068 ms |
197884 KB |
Output is correct |
26 |
Correct |
3187 ms |
187172 KB |
Output is correct |
27 |
Correct |
3264 ms |
219844 KB |
Output is correct |
28 |
Correct |
1721 ms |
185480 KB |
Output is correct |
29 |
Correct |
3329 ms |
185620 KB |
Output is correct |
30 |
Correct |
3292 ms |
185172 KB |
Output is correct |
31 |
Correct |
3359 ms |
185640 KB |
Output is correct |
32 |
Correct |
1715 ms |
100408 KB |
Output is correct |
33 |
Correct |
1000 ms |
94784 KB |
Output is correct |
34 |
Correct |
1607 ms |
90472 KB |
Output is correct |
35 |
Correct |
1590 ms |
90392 KB |
Output is correct |
36 |
Correct |
1757 ms |
91324 KB |
Output is correct |
37 |
Correct |
1799 ms |
91192 KB |
Output is correct |