#include <bits/stdc++.h>
using namespace std;
#include "factories.h"
#define ll long long
#define pii pair <int, int>
#define pil pair <int, ll>
#define fi first
#define se second
#define mp make_pair
const int NM = 1e6, LOG = 20;
const ll inf = 1e18;
int N, Q;
int A[NM+5], B[NM+5], D[NM+5], X[NM+5], Y[NM+5];
vector <pii> adj[NM+5];
int parent[NM+5], h[NM+5], jump[NM+5][LOG+5];
ll d[NM+5];
int t, s[NM+5], e[NM+5];
vector <int> L, L1;
int col[NM+5];
stack <int> st;
vector <pil> son[NM+5];
ll dp1[NM+5], dp2[NM+5], ans;
void DFS(int u){
s[u] = ++t;
for (int i = 0; i < (int)adj[u].size(); i++){
int v = adj[u][i].fi;
if (h[v] != -1) continue;
parent[v] = u;
h[v] = h[u]+1;
d[v] = d[u]+(ll)adj[u][i].se;
DFS(v);
}
e[u] = t;
}
void build(){
for (int i = 0; i < N; i++)
jump[i][0] = parent[i];
for (int j = 1; j <= LOG; j++)
for (int i = 0; i < N; i++)
if (jump[i][j-1] != -1) jump[i][j] = jump[jump[i][j-1]][j-1];
else jump[i][j] = -1;
}
bool is_ancestor(int a, int u){
return s[a] <= s[u] && e[a] >= e[u];
}
int LCA(int u, int v){
if (h[u] < h[v]) swap(u, v);
for (int i = LOG; i >= 0; i--)
if (h[u]-(1<<i) >= h[v]) u = jump[u][i];
if (u == v) return u;
for (int i = LOG; i >= 0; i--)
if (jump[u][i] != -1 && jump[u][i] != jump[v][i]){
u = jump[u][i];
v = jump[v][i];
}
return parent[u];
}
ll dist(int u, int v){
return d[u]+d[v]-2*d[LCA(u, v)];
}
bool cmp(int x, int y){
return s[x] < s[y];
}
void DFS2(int u){
dp1[u] = +inf, dp2[u] = +inf;
for (int i = 0; i < (int)son[u].size(); i++){
int v = son[u][i].fi;
DFS2(v);
ans = min(ans, dp1[u]+dp2[v]+son[u][i].se);
ans = min(ans, dp2[u]+dp1[v]+son[u][i].se);
dp1[u] = min(dp1[u], dp1[v]+son[u][i].se);
dp2[u] = min(dp2[u], dp2[v]+son[u][i].se);
}
if (col[u] == 1){
dp1[u] = 0;
ans = min(ans, dp2[u]);
}
if (col[u] == 2){
dp2[u] = 0;
ans = min(ans, dp1[u]);
}
}
void Init(int N, int A[], int B[], int D[]){
for (int i = 0; i < N; i++) adj[i].clear();
for (int i = 0; i < N-1; i++){
adj[A[i]].push_back(mp(B[i], D[i]));
adj[B[i]].push_back(mp(A[i], D[i]));
}
parent[0] = -1, h[0] = 0, d[0] = 0;
for (int i = 1; i < N; i++) h[i] = -1;
t = 0;
DFS(0);
build();
}
ll Query(int S, int X[], int T, int Y[]){
L.clear();
for (int i = 0; i < S; i++){
col[X[i]] = 1;
L.push_back(X[i]);
}
for (int i = 0; i < T; i++){
col[Y[i]] = 2;
L.push_back(Y[i]);
}
sort(L.begin(), L.end(), cmp);
L1.clear();
for (int i = 0; i < (int)L.size(); i++){
L1.push_back(L[i]);
if (i+1 < (int)L.size()){
int tmp = LCA(L[i], L[i+1]);
if (col[tmp] == 0){
col[tmp] = 3;
L1.push_back(tmp);
}
}
}
/* if (col[0] == 0){
col[0] = 3;
L1.push_back(0);
}*/
sort(L1.begin(), L1.end(), cmp);
for (int i = 0; i < (int)L1.size(); i++) son[L1[i]].clear();
while (!st.empty()) st.pop(); st.push(L1[0]);
for (int i = 1; i < (int)L1.size(); i++){
while (!st.empty() && !is_ancestor(st.top(), L1[i])) st.pop();
if (!st.empty()) son[st.top()].push_back(mp(L1[i], dist(st.top(), L1[i])));
st.push(L1[i]);
}
ans = +inf;
DFS2(L1[0]);
for (int i = 0; i < (int)L1.size(); i++) col[L1[i]] = 0;
return ans;
}
Compilation message
factories.cpp: In function 'long long int Query(int, int*, int, int*)':
factories.cpp:136:2: warning: this 'while' clause does not guard... [-Wmisleading-indentation]
136 | while (!st.empty()) st.pop(); st.push(L1[0]);
| ^~~~~
factories.cpp:136:32: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'
136 | while (!st.empty()) st.pop(); st.push(L1[0]);
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
36 ms |
47700 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
24 ms |
47512 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
36 ms |
47700 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |