#include "factories.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pil pair <int, ll>
#define fi first
#define se second
#define mp make_pair
const int NM = 5e5, QM = 1e5, LOG = 18;
const ll inf = 1e18;
int n;
vector <pil> adj[NM+5], son[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;
stack <int> st;
int col[NM+5];
ll dp1[NM+5], dp2[NM+5], res;
void DFS(int u){
s[u] = ++t;
for (int i = 0; i < 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; (1<<j) < n; j++)
for (int i = 0; i < n; i++)
jump[i][j] = (jump[i][j-1] == -1 ? -1 : jump[jump[i][j-1]][j-1]);
}
void Init(int N, int A[], int B[], int D[]){
n = N;
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[1] = 0; d[1] = 0;
for (int i = 1; i < n; i++) h[i] = -1;
t = 0;
DFS(1);
build();
}
bool cmp(int x, int y){
return s[x] < s[y];
}
int LCA(int u, int v){
if (h[u] < h[v]) swap(u, v);
for (int i = __lg(h[u]); i >= 0; i--)
if (h[u]-(1<<i) >= h[v]) u = jump[u][i];
if (u == v) return u;
for (int i = __lg(h[u]); 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];
}
bool is_ancestor(int a, int u){
return s[a] <= s[u] && e[a] >= e[u];
}
void DFS2(int u){
dp1[u] = dp2[u] = +inf;
for (int i = 0; i < son[u].size(); i++){
int v = son[u][i].fi;
ll c = son[u][i].se;
DFS2(v);
res = min(res, dp1[u]+dp2[v]+c);
res = min(res, dp2[u]+dp1[v]+c);
dp1[u] = min(dp1[u], dp1[v]+c);
dp2[u] = min(dp2[u], dp2[v]+c);
}
if (col[u] == 1){
dp1[u] = 0;
res = min(res, dp2[u]);
}
else if (col[u] == 2){
dp2[u] = 0;
res = min(res, dp1[u]);
}
}
long long 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]] = 1;
L.push_back(Y[i]);
}
sort(L.begin(), L.end(), cmp);
for (int i = 0; i+1 < S+T; i++){
int a = LCA(L[i], L[i+1]);
if (col[a] == 0){
col[a] = 3;
L.push_back(a);
}
}
sort(L.begin(), L.end(), cmp);
for (int i = 0; i < L.size(); i++)
son[L[i]].clear();
while (!st.empty()) st.pop();
st.push(L[0]);
for (int i = 1; i < L.size(); i++){
while (is_ancestor(st.top(), L[i])) st.pop();
son[st.top()].push_back(mp(L[i], d[L[i]]-d[st.top()]));
st.push(L[i]);
}
res = +inf;
DFS2(L[0]);
for (int i = 0; i < (int)L.size(); i++)
col[L[i]] = 0;
return res;
}
Compilation message
factories.cpp: In function 'void DFS(int)':
factories.cpp:26:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
26 | for (int i = 0; i < adj[u].size(); i++){
| ~~^~~~~~~~~~~~~~~
factories.cpp: In function 'void DFS2(int)':
factories.cpp:82:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
82 | for (int i = 0; i < son[u].size(); i++){
| ~~^~~~~~~~~~~~~~~
factories.cpp: In function 'long long int Query(int, int*, int, int*)':
factories.cpp:120:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
120 | for (int i = 0; i < L.size(); i++)
| ~~^~~~~~~~~~
factories.cpp:124:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
124 | for (int i = 1; i < L.size(); i++){
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
39 ms |
48800 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
33 ms |
48468 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
39 ms |
48800 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |