이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//#pragma optimization_level 3
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
#include"factories.h"
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>ordset;
*/
#define fr first
#define sc second
#define vec vector
#define pb push_back
#define pii pair<int, int>
#define forn(x,y) for(ll x = 1 ; x <= y ; ++x)
#define all(x) (x).begin(),(x).end()
#define fast cin.tie(0);cout.tie(0);cin.sync_with_stdio(0);cout.sync_with_stdio(0);
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef pair<ll,ll> pll;
const int nmax = 500005;
const ll linf = 1e17;
const ll mod = 1e9+7;
const int inf = 1e9+7;
const ll lim = 1e4;
int up[nmax][20];
ll dist[nmax];
int tin[nmax], tout[nmax];
int tt = 1;
int parent[nmax], sz[nmax];
vec < pair < int , ll > > g[nmax];
bitset <nmax> vis;
pll as[nmax];
void dfs(int nod , int par){
tin[nod] = ++tt;
up[nod][0] = par;
for(auto i : g[nod]){
if(i.fr != par){
dfs(i.fr,nod);
dist[i.sc] = dist[nod] + i.sc;
}
}
tout[nod] = ++tt;
}
bool anc(int x , int y){
return (tin[x] <= tin[y] && tout[x] >= tout[y]);
}
int lca(int x , int y){
int p = x;
if(anc(x,y))return x;
for(int i = 19; i >= 0; i--){
if(!anc(up[p][i],y))p = up[p][i];
}
return up[p][0];
}
ll distanta(int a , int b){
int l = lca(a,b);
return dist[a] + dist[b] - 2*dist[l];
}
void reroot(int nod, int p = -1){
sz[nod] = 1;
for(auto i : g[nod]){
if(i.fr != p && !vis[i.fr]){
reroot(i.fr,nod);
sz[nod] += sz[i.fr];
}
}
}
int centr(int nod){
reroot(nod);
int cen = nod;
bool flag = false;
while(!flag){
flag = true;
for(auto i : g[nod]){
if(!vis[i.fr] && sz[i.fr]*2 > sz[nod]){
flag = false;
cen = i.fr;
}
}
}
vis[cen] = 1;
for(auto i : g[nod]){
int j = i.fr;
if(!vis[j]){
parent[centr(j)] = cen;
}
}
return cen;
}
void Init(int N, int A[], int B[], int D[]){
for(int i = 0; i < N-1 ; i++){
g[A[i]].pb({B[i],D[i]});
g[B[i]].pb({A[i],D[i]});
}
dfs(0,0);
up[0][0] = 0;
forn(i,19){
for(int j = 0 ; j < N; j++){
up[j][i] = up[up[j][i-1]][i-1];
}
}
for(int i = 0 ; i < N ; i++){
as[i].fr = linf;
as[i].sc = linf;
}
parent[centr(0)] = -1;
}
ll Query(int S, int A[], int T, int B[]){
vec < int > used;
ll rez = linf;
for(int i = 0 ; i < S ; i++){
int nod = A[i];
while(nod != -1){
used.pb(nod);
as[i].fr = min(as[i].fr, distanta(A[i],nod));
nod = parent[nod];
}
}
for(int i = 0 ; i < T ; i++){
int nod = B[i];
while(nod != -1){
used.pb(nod);
as[i].sc = min(as[i].sc, distanta(B[i],nod));
rez = min(rez , as[i].fr + as[i].sc);
nod = parent[nod];
}
}
for(int i : used){
as[i].fr = linf;
as[i].sc = linf;
}
return rez;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |