# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
285116 | BeanZ | Factories (JOI14_factories) | C++14 | 0 ms | 0 KiB |
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 <bits/stdc++.h>
using namespace std;
#define ll int
#define endl '\n'
const int N = 1e6 + 5;
const int mod = 1e9 + 7;
const int lg = 19;
vector<pair<ll, ll>> node[N];
ll dep[N], tin[N];
long long dis[N];
ll sz[N];
ll par[N], st[N], X[N], Y[N], cur[N];
long long ans[N];
bool ok[N], in[N];
ll timer = 0;
pair<ll, ll> dp[20][N * 2];
void dfs(ll u, ll p){
st[u] = ++timer;
tin[timer] = u;
for (auto j : node[u]){
if (j.first == p) continue;
dis[j.first] = dis[u] + j.second;
dep[j.first] = dep[u] + 1;
dfs(j.first, u);
tin[++timer] = u;
}
}
void buildLCA(){
for (int i = 1; i <= timer; i++) dp[0][i] = {dep[tin[i]], tin[i]};
for (int i = 1; i <= lg; i++){
for (int j = 1; j <= timer; j++){
if ((j + (1 << i)) > timer) break;
dp[i][j] = min(dp[i - 1][j], dp[i - 1][j + (1 << (i - 1))]);
}
}
}
ll findnewCT(ll u, ll p, ll S){
for (auto j : node[u]){
if (j.first == p || in[j.first]) continue;
if ((sz[j.first] * 2) > S) return findnewCT(j.first, u, S);
}
return u;
}
ll initsz(ll u, ll p){
sz[u] = 1;
for (auto j : node[u]){
if (j.first == p || in[j.first]) continue;
sz[u] += initsz(j.first, u);;
}
return sz[u];
}
ll findCT(ll u){
ll S = initsz(u, u);
u = findnewCT(u, u, S);
in[u] = 1;
for (auto j : node[u]){
if (in[j.first]) continue;
par[findCT(j.first)] = u;
}
return u;
}
ll LCA(ll u, ll v){
ll l = st[u], r = st[v];
if (l > r) swap(l, r);
ll k = 31 - __builtin_clz(r - l + 1);
return min(dp[k][l], dp[k][r - (1 << k) + 1]).second;
}
long long dist(ll u, ll v){
ll lca = LCA(u, v);
return dis[u] + dis[v] - 2 * dis[lca];
}
ll cs;
void upd(ll u, ll root){
if (u == 0) return;
if (cur[u] != cs){
cur[u] = cs;
ans[u] = 1e15;
}
ans[u] = min(ans[u], dist(root, u));
upd(par[u], root);
}
long long get(ll u, ll root){
if (u == 0) return 1e15;
if (cur[u] != cs){
cur[u] = cs;
ans[u] = 1e15;
}
return min(ans[u] + dist(u, root), get(par[u], root));
}
void fs(int &number){
number = 0;
register int c;
c = getchar();
for (; c >= '0' && c <= '9'; c = getchar()){
number = number * 10 + (c - '0');
}
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int main(){
if (fopen("VietCT.INP", "r")){
freopen("VietCT.INP", "r", stdin);
freopen("VietCT.OUT", "w", stdout);
}
ll n, q;
fs(n); fs(q);
for (int i = 1; i < n; i++){
ll u, v, c;
fs(u); fs(v); fs(c);
node[u + 1].push_back({v + 1, c});
node[v + 1].push_back({u + 1, c});
}
dfs(1, 1);
buildLCA();
ll root = findCT(1);
for (int i = 1; i <= n; i++) ans[i] = 1e15;
memset(cur, -1, sizeof(cur));
while (q--){
ll S, T;
fs(S); fs(T);
for (int i = 0; i < S; i++) fs(X[i]);
for (int i = 0; i < T; i++) fs(Y[i]);
cs = q;
if (S > T){
for (int i = 1; i <= T; i++){
ll could = rand() % 10;
if (could) continue;
upd(Y[i - 1] + 1, Y[i - 1] + 1);
}
long long res = 1e15;
for (int i = 1; i <= S; i++){
ll could = rand() % 10;
if (could) continue;
res = min(res, get(X[i - 1] + 1, X[i - 1] + 1));
}
cout << res << endl;
} else {
for (int i = 1; i <= S; i++){
ll could = rand() % 10;
if (could) continue;
upd(X[i - 1] + 1, X[i - 1] + 1);
}
long long res = 1e15;
for (int i = 1; i <= T; i++){
ll could = rand() % 10;
if (could) continue;
res = min(res, get(Y[i - 1] + 1, Y[i - 1] + 1));
}
cout << res << endl;
}
}
}
/*
*/