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>
#include "factories.h"
#define pb push_back
#define LINF 0x3f3f3f3f3f3f3f3f
#define endl '\n'
#define ll long long
#define f first
#define s second
using namespace std;
typedef pair<int, ll> pii;
const int MAXN = 5e5+5;
int sz[MAXN], used[MAXN];
ll best[MAXN];
vector<pii> g[MAXN], ng[MAXN];
int getsz(int u, int p) {
sz[u] = 1;
for(pii &i : g[u])
if(i.f != p && !used[i.f])
sz[u] += getsz(i.f, u);
return sz[u];
}
int findc(int u, int p, int n) {
for(pii &i : g[u])
if(i.f != p && !used[i.f] && sz[i.f] > n/2)
return findc(i.f, u, n);
return u;
}
void getng(int u, int p, int c, ll dist) {
if(u != c)
ng[u].pb({c, dist});
for(pii &i : g[u])
if(i.f != p && !used[i.f])
getng(i.f, u, c, dist + i.s);
}
void build(int u, int p) {
int c = findc(u, -1, getsz(u, -1));
getng(c, -1, c, 0);
used[c] = 1;
for(pii &i : g[c])
if(!used[i.f])
build(i.f, c);
}
void Init(int n, int a[], int b[], int c[]) {
for(int i = 0; i < n; i++)
best[i] = LINF;
for(int i = 0; i < n-1; i++) {
g[a[i]].pb({b[i], c[i]});
g[b[i]].pb({a[i], c[i]});
}
build(0, -1);
for(int i = 0; i < n; i++) reverse(ng[i].begin(), ng[i].end());
}
ll Query(int s, int x[], int t, int y[]) {
ll ans = LINF;
for(int u = 0; u < s; u++)
for(pii &i : ng[u])
best[i.f] = min(best[i.f], i.s);
for(int u = 0; u < t; u++)
for(pii &i : ng[u])
ans = min(ans, best[i.f] + i.s);
for(int u = 0; u < s; u++)
for(pii &i : ng[u])
best[i.f] = LINF;
return ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |