This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
ID: varunra2
LANG: C++
TASK: factories
*/
#include <bits/stdc++.h>
#include "factories.h"
using namespace std;
#ifdef DEBUG
#include "lib/debug.h"
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define debug_arr(...) \
cerr << "[" << #__VA_ARGS__ << "]:", debug_arr(__VA_ARGS__)
#pragma GCC diagnostic ignored "-Wsign-compare"
//#pragma GCC diagnostic ignored "-Wunused-parameter"
//#pragma GCC diagnostic ignored "-Wunused-variable"
#else
#define debug(...) 42
#endif
#define EPS 1e-9
#define IN(A, B, C) assert(B <= A && A <= C)
#define INF (int)1e9
#define MEM(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define MP make_pair
#define PB push_back
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define x first
#define y second
const double PI = acos(-1.0);
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> PII;
typedef map<int, int> MPII;
typedef multiset<int> MSETI;
typedef set<int> SETI;
typedef set<string> SETS;
typedef vector<int> VI;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef vector<string> VS;
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto& a : x)
#define sz(x) (int)(x).size()
typedef pair<int, int> pii;
typedef vector<int> vi;
#pragma GCC diagnostic ignored "-Wsign-compare"
// util functions
#define MAXN (int)5e5 + 5
#define MAXSEG 20
int n;
int par[MAXN];
bool use[MAXN];
int sub[MAXN];
VII adj[MAXN];
int lca[MAXN][MAXSEG];
ll d[MAXN];
int tin[MAXN];
int tout[MAXN];
int timer = 0;
VI centpar[MAXN];
vector<ll> distpar[MAXN];
int dfs(int u, int v) {
sub[u] = 1;
for (auto& x : adj[u]) {
if (x.x == v or use[x.x]) continue;
sub[u] += dfs(x.x, u);
}
return sub[u];
}
int getCent(int u, int v, int siz) {
for (auto& x : adj[u]) {
if (x.x == v or use[x.x]) continue;
if (sub[x.x] * 2 > siz) {
return getCent(x.x, u, siz);
}
}
return u;
}
void decompose(int u, int v, int cnt) {
int cur = getCent(u, -1, dfs(u, -1));
use[cur] = true;
par[cur] = v;
for (auto& x : adj[cur]) {
if (use[x.x]) continue;
decompose(x.x, cur, cnt);
}
}
void getAdj(int a[], int b[], int d[]) {
for (int i = 0; i < n - 1; i++) {
int u, v, w;
u = a[i];
v = b[i];
w = d[i];
adj[u].PB(MP(v, w));
adj[v].PB(MP(u, w));
}
}
void dfsLCA(int u, int v) {
tin[u] = ++timer;
lca[u][0] = v;
for (int i = 1; i < MAXSEG; i++) {
int to = lca[u][i - 1];
if (to == -1)
lca[u][i] = -1;
else
lca[u][i] = lca[to][i - 1];
}
for (auto& x : adj[u]) {
if (x.x == v) continue;
d[x.x] = d[u] + x.y;
dfsLCA(x.x, u);
}
tout[u] = ++timer;
}
bool isAnc(int u, int v) {
if (u == -1) return true;
if (v == -1) return false;
return tin[u] <= tin[v] and tout[u] >= tout[v];
}
int getLCA(int u, int v) {
if (isAnc(u, v)) return u;
if (isAnc(v, u)) return v;
for (int i = MAXSEG - 1; i >= 0; i--) {
if (!isAnc(lca[u][i], v)) {
u = lca[u][i];
}
}
return lca[u][0];
}
inline ll getDist(int u, int v) { return d[u] + d[v] - 2 * d[getLCA(u, v)]; }
ll mindist[MAXN];
bool vis[MAXN];
void Init(int N, int a[], int b[], int d[]) {
// init_siz(N);
n = N;
getAdj(a, b, d);
decompose(0, -1, 0);
dfsLCA(0, -1);
for (int i = 0; i < MAXN; i++) {
mindist[i] = 0;
vis[i] = false;
}
for (int i = 0; i < n; i++) {
int u = i;
while (u != -1) {
centpar[i].PB(u);
distpar[i].PB(getDist(i, u));
u = par[u];
}
}
}
ll Query(int s, int x[], int t, int y[]) {
for (int i = 0; i < s; i++) {
// int u = x[i];
// while (u != -1) {
// if (vis[u]) {
// mindist[u] = min(mindist[u], getDist(u, x[i]));
// } else {
// vis[u] = true;
// mindist[u] = getDist(u, x[i]);
// }
// u = par[u];
// }
for (int j = 0; j < centpar[x[i]].size(); j++) {
int to = centpar[x[i]][j];
ll todist = distpar[x[i]][j];
if (vis[to]) {
mindist[to] = min(mindist[to], todist);
} else {
vis[to] = true;
mindist[to] = todist;
}
// mindist[centpar[i][j]] = min(mindist[centpar[i][j]])
}
}
ll ret = (ll)1e17;
for (int i = 0; i < t; i++) {
// int u = y[i];
// while (u != -1) {
// if (vis[u]) {
// ret = min(ret, mindist[u] + getDist(u, y[i]));
// }
// u = par[u];
// }
for(int j = 0; j < centpar[y[i]].size(); j++) {
int to = centpar[y[i]][j];
ll todist = distpar[y[i]][j];
if(vis[to]) {
ret = min(ret, mindist[to] + todist);
}
}
}
for (int i = 0; i < s; i++) {
int u = x[i];
while (u != -1) {
vis[u] = false;
u = par[u];
}
}
for (int i = 0; i < t; i++) {
int u = y[i];
while (u != -1) {
vis[u] = false;
u = par[u];
}
}
return ret;
}
// int main() {
// #ifndef ONLINE_JUDGE
// freopen("factories.in", "r", stdin);
// freopen("factories.out", "w", stdout);
// #endif
// cin.sync_with_stdio(0);
// cin.tie(0);
// int N, q;
// cin >> N >> q;
// int a[N - 1];
// int b[N - 1];
// int d[N - 1];
// for (int i = 0; i < N - 1; i++) {
// cin >> a[i] >> b[i] >> d[i];
// }
// Init(N, a, b, d);
// for (int i = 0; i < q; i++) {
// int s, t;
// cin >> s >> t;
// int x[s];
// int y[t];
// for (int j = 0; j < s; j++) {
// cin >> x[j];
// }
// for (int j = 0; j < t; j++) {
// cin >> y[j];
// }
// cout << Query(s, x, t, y) << '\n';
// }
// return 0;
// }
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |