#include "factories.h"
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define ll long long
#define lf (id<<1)
#define rg ((id<<1)|1)
#define md ((l+r)>>1)
#define ld long double
using namespace std;
typedef pair<ll,ll> pii;
const ll INF = 1e18+10;
const int MAXN = 5e5+10;
const int LOG = 21;
int n;
vector <pii> adj[MAXN];
ll dep[MAXN], up[MAXN], anc[MAXN][LOG+5];
ll x[MAXN], y[MAXN];
void dfs(ll nw, ll par){
dep[nw] = dep[par]+1;
anc[nw][0] = par;
for(auto nx : adj[nw]){
if(nx.fi == par) continue;
up[nx.fi] = up[nw]+nx.se;
dfs(nx.fi, nw);
}
}
ll LCA(ll x, ll y){
if(dep[x] > dep[y]) swap(x, y); //gerakin y
for(int i=LOG-1; i>=0; i--){
if(dep[anc[y][i]] >= dep[x]) y = anc[y][i]; // dibawah ato sama dengan
}
if(x == y) return x;
for(int i=LOG-1; i>=0; i--){
if(anc[y][i] != anc[x][i]){
x = anc[x][i];
y = anc[y][i];
}
}
return anc[x][0];
}
ll DIST(ll x, ll y){
ll lca = LCA(x, y);
return (ll)up[x]+up[y]-2*up[lca];
}
void Init(int N, int A[], int B[], int D[]) {
n = N;
for(int i=0; i<n-1; i++){
A[i]++; B[i]++;
adj[A[i]].pb({B[i], D[i]}); adj[B[i]].pb({A[i], D[i]});
}
up[0] = 0; dep[0] = 0;
dfs(1, -1);
for(int i=1; i<LOG; i++){
for(int j=1; j<=n; j++){
anc[j][i] = anc[anc[j][i-1]][i-1];
}
}
//cout << LCA(6, 7) << "pp\n";
//for(int i=1; i<=n; i++) cout << up[i] << " p\n";
}
long long Query(int S, int X[], int T, int Y[]) {
ll mn = INF;
for(int i=0; i<S; i++){
for(int j=0; j<T; j++){
ll te = DIST(X[i]+1, Y[j]+1);
mn = min(mn, te);
//cout << X[i]+1 << ' ' << Y[j]+1 << ' '<<
// DIST(X[i]+1, Y[j]+1) << ' '<< up[X[i]+1] << ' ' << " x\n";
assert(te != 0);
}
}
return mn;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
260 ms |
37208 KB |
Output is correct |
2 |
Execution timed out |
8021 ms |
51024 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
37212 KB |
Output is correct |
2 |
Correct |
1893 ms |
187208 KB |
Output is correct |
3 |
Correct |
4425 ms |
191100 KB |
Output is correct |
4 |
Correct |
1248 ms |
186596 KB |
Output is correct |
5 |
Correct |
4279 ms |
208432 KB |
Output is correct |
6 |
Correct |
3439 ms |
192700 KB |
Output is correct |
7 |
Correct |
7003 ms |
80124 KB |
Output is correct |
8 |
Correct |
2447 ms |
79372 KB |
Output is correct |
9 |
Correct |
5512 ms |
82656 KB |
Output is correct |
10 |
Correct |
4008 ms |
80980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
260 ms |
37208 KB |
Output is correct |
2 |
Execution timed out |
8021 ms |
51024 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |