# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
792157 | bane | Synchronization (JOI13_synchronization) | C++17 | 0 ms | 0 KiB |
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>
using namespace std;
#include "factories.h"
struct weighted_graph{
int N;
vector<vector<pair<long long,long long>>>adj, GC;
vector<long long>in, depth, crnci, dist, dp, sz;
vector<vector<int>>lift;
inline void dec(int _N){
N = _N;
adj.resize(N+1);
in.resize(N+1);
lift.resize(N+1);
depth.resize(N+1);
crnci.resize(N+1);
dist.resize(N+1);
GC.resize(N+1);
dp.resize(N+1);
sz.resize(N+1);
for (int i = 1; i <= N; i++){
dp[i] = (long long)1e18;
}
for (int i = 1; i <= N; i++){
lift[i].resize(20);
}
}
inline void add_edge(int A, int B, int D){
adj[A].push_back({B,D});
adj[B].push_back({A,D});
}
inline void init(){
int timer = 1;
function<int(int,int, long long h)>Dfs = [&](int u, int p, long long h){
in[u] = timer++;
depth[u] = depth[p] + 1;
sz[u] = 1;
dist[u] = h;
lift[u][0] = p;
for (auto& [x,w] : adj[u]){
if (x == p)continue;
sz[u] += Dfs(x,u, h + w);
}
return sz[u];
};
depth[1] = -1;
Dfs(1,1,0);
for (int i = 1; i < 20; i++){
for (int j = 1; j <= N; j++){
lift[j][i] = lift[lift[j][i-1]][i-1];
}
}
}
inline int LCA(int A, int B){
if (depth[A] > depth[B])swap(A,B);
int k = depth[B] - depth[A];
for (int i = 19; i>=0; i--){
if ((1 << i) & k){
B = lift[B][i];
}
}
if (A == B)
return A;
for (int i = 19; i>=0; i--){
if (lift[B][i] != lift[A][i]){
B = lift[B][i];
A = lift[A][i];
}
}
return lift[A][0];
}
inline int Dijkstra(int S, int X[]){
priority_queue<pair<long long, long long>>pq;
for (int i = 0; i < S; i++){
pq.push({0,X[i]});
dp[X[i]] = 0;
}
while(!pq.empty()){
int node = pq.top().second;
long long dst = -pq.top().first;
pq.pop();
if (dst != dp[node])continue;
if (crnci[node] == 1)return dst;
for (auto& [x,w] : GC[node]){
if (dp[x] > w + dst){
dp[x] = w + dst;
pq.push({-dp[x], x});
}
}
}
return -1;
}
inline int Query(int S, int X[], int T, int Y[]){
vector<int>sve;
for (int i = 0; i < S; i++)X[i]++;
for (int i = 0; i < T; i++)Y[i]++;
for (int i = 0; i < S; i++){
sve.push_back(X[i]);
}
for (int i = 0; i < T; i++){
sve.push_back(Y[i]);
}
sort(sve.begin(),sve.end(), [&](int u, int p){
return in[u] < in[p];
});
for (int i = 1; i < S + T; i++){
sve.push_back(LCA(sve[i - 1], sve[i]));
}
sort(sve.begin(),sve.end(), [&](int u, int p){
return in[u] < in[p];
});
sve.erase(unique(sve.begin(),sve.end()), sve.end());
int where = 0;
function<void()>build = [&](){
int cp = sve[where];
where++;
int max_ind = in[cp] + sz[cp] - 1;
while(where < sve.size() && in[sve[where]] <= max_ind){
GC[cp].push_back({sve[where], dist[sve[where]] - dist[cp]});
GC[sve[where]].push_back({cp,dist[sve[where]] - dist[cp]});
build();
}
};
build();
//for (int i : sve)cout << " " << i;
// cout << endl;
for (int i = 0; i < T; i++){
crnci[Y[i]] = 1;
}
long long ans = Dijkstra(S,X);
for (int i = 0; i < T; i++){
crnci[Y[i]] = 0;
}
for (int i = 0; i < (int)sve.size(); i++){
GC[sve[i]].clear();
dp[sve[i]] = (long long)1e18;
}
return ans;
}
} G;
void Init(int N, int A[], int B[], int D[]) {
G.dec(N);
for (int i = 0; i < N - 1; i++){
G.add_edge(++A[i],++B[i],D[i]);
}
G.init();
}
long long Query(int S, int X[], int T, int Y[]) {
return G.Query(S,X,T,Y);
}