#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.s])
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);
}
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
47 ms |
48600 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
42 ms |
48164 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
47 ms |
48600 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |