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;
typedef long long ll;
typedef unsigned long long ull;
#define ALL(v) (v).begin(), (v).end()
#define MASK(i) (1LL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
// mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rng(1);
ll rngesus(ll l, ll r){return ((ull) rng()) % (r - l + 1) + l;}
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll LASTBIT(ll mask){return mask & (-mask);}
ll pop_cnt(ll mask){return __builtin_popcountll(mask);}
ll ctz(ll mask){return __builtin_ctzll(mask);}
ll clz(ll mask){return __builtin_clzll(mask);}
ll logOf(ll mask){return 63 - clz(mask);}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){
if (a > b){a = b; return true;}
return false;
}
template <class T1, class T2>
bool maximize(T1 &a, T2 b){
if (a < b){a = b; return true;}
return false;
}
template <class T>
void printArr(T a, string separator = " ", string finish = "\n", ostream& out = cout){
for(auto i: a) out << i << separator;
out << finish;
}
template <class T>
void remove_dup(vector<T> &a){
sort(ALL(a));
a.resize(unique(ALL(a)) - a.begin());
}
struct P{
int i, w;
P(){}
P(int _i, int _w){i = _i, w = _w;}
};
struct compare{
bool operator () (P a, P b){return a.w > b.w;}
};
struct DSU{
int n;
vector<int> parent, sz;
DSU(int _n){
n = _n;
parent.resize(n+1); sz.resize(n+1, 1);
for(int i = 1; i<=n; ++i) parent[i] = i;
}
int find_set(int u){return (u == parent[u]) ? u : (parent[u] = find_set(parent[u]));}
bool join_set(int u, int v){
u = find_set(u), v = find_set(v);
if (u != v){
if (sz[u] < sz[v]) swap(u, v);
parent[v] = u;
sz[u] += sz[v];
return true;
}
return false;
}
};
const int N = 1e5 + 69, INF = 1e9 + 69, LOG_N = 17;
int n, m;
vector<P> graph[N];
vector<pair<int, int>> tree[N];
int dis[N];
int h[N], parent[N][LOG_N], min_w[N][LOG_N];
void dfs(int u, int p){
h[u] = h[p] + 1;
for(int j = 1; MASK(j) < h[u]; ++j){
int prev = parent[u][j-1];
parent[u][j] = parent[prev][j-1];
min_w[u][j] = min(min_w[u][j-1], min_w[prev][j-1]);
}
for(pair<int, int> v: tree[u]) if (v.first != p){
parent[v.first][0] = u;
min_w[v.first][0] = v.second;
dfs(v.first, u);
}
}
int LCA(int u, int v){
if (h[u] < h[v]) swap(u, v);
int ans = INF;
int diff = h[u] - h[v];
for(int j = 0; j< LOG_N; ++j) if (GETBIT(diff, j)) {
minimize(ans, min_w[u][j]);
u = parent[u][j];
}
if (u == v) return ans;
for(int j= LOG_N - 1; j>=0; --j) if (parent[u][j] != parent[v][j]){
minimize(ans, min(min_w[u][j], min_w[v][j]));
u = parent[u][j], v = parent[v][j];
}
minimize(ans, min(min_w[u][0], min_w[v][0]));
return ans;
}
int main(void){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m;
for(int i =0; i<m; ++i){
int u, v, w; cin >> u >> v >> w;
graph[u].push_back(P(v, w));
graph[v].push_back(P(u, w));
}
int k; cin >> k;
vector<int> npp(k);
for(int i= 0; i<k; ++i)
cin >> npp[i];
remove_dup(npp);
k = npp.size();
priority_queue<P, vector<P>, compare> pq;
for(int i = 1; i<=n; ++i) dis[i] = INF;
for(int i: npp) {
dis[i] = 0;
pq.push(P(i, 0));
}
while(pq.size()){
P u = pq.top(); pq.pop();
for(P v: graph[u.i])
if (minimize(dis[v.i], dis[u.i] + v.w))
pq.push(P(v.i, dis[v.i]));
}
vector<array<int, 3>> edging;
for(int i = 1; i<=n; ++i) for(P j: graph[i]) if (i < j.i){
edging.push_back({{i, j.i, min(dis[i], dis[j.i])}});
}
sort(ALL(edging), [](array<int, 3> x, array<int, 3> y){return x[2] > y[2];});
DSU mst(n);
for(auto i: edging) if (mst.join_set(i[0], i[1])){
int u = i[0], v = i[1], w = i[2];
tree[u].push_back({v, w});
tree[v].push_back({u, w});
}
dfs(1, 1);
int q; cin >> q;
while(q--){
int u, v; cin >> u >> v;
int ans = LCA(u, v);
cout << ans << "\n";
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |