#include "factories.h"
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
using namespace std;
long long int f[500005];
int n, d[500005], tin[500005], b[2000005], l[2000005], z = 0;
vector<int> gg[500005], g[500005];
int p[500005][20], pp[2000005][22];
void bfs(int x) {
queue<int> q;
q.push(x);
p[x][0] = x;
f[x] = d[x] = 0;
while (!q.empty()) {
int u = q.front();
q.pop();
for (int j = 1; j <= 19; j++) {
p[u][j] = p[p[u][j - 1]][j - 1];
if (p[u][j] == 0) break;
}
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i];
if (d[u] + 1 < d[v]) {
d[v] = d[u] + 1;
f[v] = f[u] + gg[u][i];
p[v][0] = u;
q.push(v);
}
}
}
}
void dfs(int x, int p) {
b[++z] = x;
pp[z][0] = x;
tin[x] = z;
for (int w : g[x]) {
if (w == p) continue;
dfs(w, x);
b[++z] = x;
pp[z][0] = x;
}
b[++z] = x;
pp[z][0] = x;
}
long long int check(int u, int v) {
return f[u] + f[v] - f[lca(u, v)] * 2;
}
void Init(int N, int A[], int B[], int D[]) {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
n = N;
for (int i = 0; i < 500000; i++) {
f[i] = 999999999999999999;
d[i] = n * 2;
}
for (int i = 0; i < n - 1; i++) {
int u = A[i], v = B[i], w = D[i];
g[u].push_back(v);
g[v].push_back(u);
gg[u].push_back(w);
gg[v].push_back(w);
}
bfs(0);
dfs(0, -1);
for (int i = 2; i <= 2000000; i++) l[i] = l[i / 2] + 1;
for (int i = 1; (1 << i) <= z; i++) {
for (int j = 1; j <= z - (1 << i) + 1; j++) {
if (d[pp[j][i - 1]] < d[pp[j + (1 << (i - 1))][i - 1]]) pp[j][i] = pp[j][i - 1];
else pp[j][i] = pp[j + (1 << (i - 1))][i - 1];
}
}
}
int lca(int u, int v) {
int x = tin[u], y = tin[v];
if (y <= x) swap(x, y);
int g = l[y - x + 1];
int h = pp[x][g], k = pp[y - (1 << g) + 1][g];
if (d[h] < d[k]) return h;
return k;
}
long long int check(int u, int v) {
return f[u] + f[v] - f[lca(u, v)] * 2;
}
long long int Query(int S, int X[], int T, int Y[]) {
if (S > 45 && T > 45) {
vector<long long int> ff(n);
vector<bool> dda(n), dd(n);
for (int i = 0; i < n; i++) {
dd[i] = dda[i] = 0;
ff[i] = 999999999999999999;
}
for (int i = 0; i < T; i++) {
dda[Y[i]] = 1;
}
priority_queue<pair<long long int, long long int>> q;
for (int i = 0; i < S; i++) {
q.push({0, X[i]});
ff[X[i]] = 0;
}
while (!q.empty()) {
int u = q.top().second;
q.pop();
if (dd[u] == 1) continue;
dd[u] = 1;
if (dda[u] == 1) return ff[u];
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i], w = gg[u][i];
if (ff[u] + w < ff[v]) {
ff[v] = ff[u] + w;
q.push({-ff[v], v});
}
}
}
}
long long int res = 999999999999999999;
for (int i = 0; i < S; i++) {
for (int j = 0; j < T; j++) {
res = min(res, check(X[i], Y[j]));
}
}
return res;
}
Compilation message
factories.cpp: In function 'void bfs(int)':
factories.cpp:22:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | for (int i = 0; i < g[u].size(); i++) {
| ~~^~~~~~~~~~~~~
factories.cpp: In function 'long long int check(int, int)':
factories.cpp:47:28: error: 'lca' was not declared in this scope
47 | return f[u] + f[v] - f[lca(u, v)] * 2;
| ^~~
factories.cpp: At global scope:
factories.cpp:82:15: error: redefinition of 'long long int check(int, int)'
82 | long long int check(int u, int v) {
| ^~~~~
factories.cpp:46:15: note: 'long long int check(int, int)' previously defined here
46 | long long int check(int u, int v) {
| ^~~~~
factories.cpp: In function 'long long int Query(int, int*, int, int*)':
factories.cpp:107:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
107 | for (int i = 0; i < g[u].size(); i++) {
| ~~^~~~~~~~~~~~~