This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "factories.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef pair<ll, ll> pll;
typedef pair<ll, pll> plll;
#define fi first
#define se second
const int INF = 1e9+1;
const ll LLINF = (ll)1e18+1;
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) { for(auto i : v) os << i << " "; os << "\n"; return os; }
template <typename T1, typename T2>
ostream& operator<<(ostream& os, const pair<T1, T2>& p) { os << p.fi << " " << p.se; return os; }
vector<vector<int>> S, E;
vector<int> P, L, I;
vector<ll> D, DU, DV;
vector<bool> U, V;
int lca(int x, int y) {
if(L[x] < L[y]) swap(x, y);
int c = L[x] - L[y];
for(int i = 19; i >= 0; i--)
if((c>>i)&1)
x = S[x][i];
if(x == y) return x;
for(int i = 19; i >= 0; i--)
if(S[x][i] != S[y][i])
x = S[x][i], y = S[y][i];
return S[x][0];
}
void make_sparse() {
//cout << "Make Sparse\n";
S.resize(P.size(), vector<int>(20));
for(int i = 0; i < P.size(); i++) S[i][0] = P[i];
for(int i = 0; i < P.size(); i++)
for(int j = 1; j < 20; j++)
S[i][j] = S[S[i][j-1]][j-1];
}
void dfs(int x, int p, int &cnt, vector<vector<pii>> &E) {
I[x] = cnt++;
for(auto &i : E[x]) {
if(i.fi == p) continue;
L[i.fi] = L[x]+1;
D[i.fi] = D[x]+i.se;
P[i.fi] = x;
dfs(i.fi, x, cnt, E);
}
}
ll solve(int x) {
ll ans = LLINF;
if(U[x]) DU[x] = 0LL;
if(V[x]) DV[x] = 0LL;
U[x] = V[x] = false;
for(auto i : E[x]) {
ans = min(ans, solve(i));
DV[x] = min(DV[x], DV[i]+D[i]-D[x]);
DU[x] = min(DU[x], DU[i]+D[i]-D[x]);
}
ans = min(ans, DV[x]+DU[x]);
return ans;
}
void sort_vertex(vector<int> &vertex) {
sort(vertex.begin(), vertex.end(), [&](int a, int b){ return I[a] < I[b]; });
vertex.erase(unique(vertex.begin(), vertex.end()), vertex.end());
}
void make_tree(vector<int> &M) {
int tmpsize = M.size();
for(int i = 1; i < tmpsize; i++) M.push_back(lca(M[i-1], M[i]));
sort_vertex(M);
for(int i = 1; i < M.size(); i++) E[lca(M[i-1], M[i])].push_back(M[i]);
}
void Init(int N, int A[], int B[], int X[]) {
vector<vector<pii>> T(N);
for(int i = 0; i < N-1; i++) {
T[A[i]].push_back({B[i], X[i]});
T[B[i]].push_back({A[i], X[i]});
}
I.resize(N); P.resize(N);
D.resize(N, 0LL); L.resize(N, 0);
int cnt = 0;
dfs(0, 0, cnt, T);
make_sparse();
U.resize(N, false); V.resize(N, false); E.resize(N);
DU.resize(N, LLINF); DV.resize(N, LLINF);
}
ll Query(int S, int X[], int T, int Y[]) {
//cout << "query " << S << " " << T << "\n";
vector<int> XX(X, X+S), YY(Y, Y+T), M;
for(auto i : XX) { M.push_back(i); U[i] = true; }
for(auto i : YY) { M.push_back(i); V[i] = true; }
sort_vertex(M);
make_tree(M);
ll ans = solve(M[0]);
assert(DU[M[0]] < LLINF && DV[M[0]] < LLINF);
for(auto i : M) {
U[i] = V[i] = false;
DU[i] = DV[i] = LLINF;
E[i].clear();
}
return ans;
}
Compilation message (stderr)
factories.cpp: In function 'void make_sparse()':
factories.cpp:39:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | for(int i = 0; i < P.size(); i++) S[i][0] = P[i];
| ~~^~~~~~~~~~
factories.cpp:40:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | for(int i = 0; i < P.size(); i++)
| ~~^~~~~~~~~~
factories.cpp: In function 'void make_tree(std::vector<int>&)':
factories.cpp:79:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
79 | for(int i = 1; i < M.size(); i++) E[lca(M[i-1], M[i])].push_back(M[i]);
| ~~^~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |