Submission #1089592

#TimeUsernameProblemLanguageResultExecution timeMemory
1089592vladiliusEvacuation plan (IZhO18_plan)C++17
100 / 100
399 ms66612 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; #define pb push_back #define ff first #define ss second #define arr3 array<int, 3> const int inf = 1e9; struct RT{ vector<vector<int>> g; vector<int> p, a; int n, cc; RT(int ns){ n = ns; p.resize(2 * n + 1); for (int i = 1; i <= 2 * n; i++){ p[i] = i; } a.resize(2 * n + 1); g.resize(2 * n + 1); cc = n; } int get(int v){ if (p[v] != v){ p[v] = get(p[v]); } return p[v]; } void add(int x, int y, int w){ x = get(x); y = get(y); if (x == y) return; cc++; a[cc] = w; p[x] = p[y] = cc; g[cc].pb(x); g[cc].pb(y); } vector<vector<int>> pw; vector<int> tin, tout; int timer, lg; void dfs(int v, int pr){ pw[v][0] = pr; for (int i = 1; i <= lg; i++){ pw[v][i] = pw[pw[v][i - 1]][i - 1]; } tin[v] = ++timer; for (int i: g[v]){ dfs(i, v); } tout[v] = timer; } void build(){ timer = 0; lg = log2(n) + 1; pw.resize(cc + 1); for (int i = 1; i <= cc; i++) pw[i].resize(lg + 1); tin.resize(cc + 1); tout.resize(cc + 1); dfs(cc, cc); } bool check(int x, int y){ return (tin[x] <= tin[y] && tout[x] >= tout[y]); } int lca(int x, int y){ if (check(x, y)) return x; if (check(y, x)) return y; for (int i = lg; i >= 0; i--){ if (!check(pw[x][i], y)){ x = pw[x][i]; } } return pw[x][0]; } int get(int x, int y){ return a[lca(x, y)]; } }; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m; cin>>n>>m; vector<pii> g[n + 1]; vector<arr3> ed; while (m--){ int x, y, w; cin>>x>>y>>w; g[x].pb({y, w}); g[y].pb({x, w}); ed.pb({0, x, y}); } int k; cin>>k; vector<int> a(k + 1), d(n + 1, inf); priority_queue<pii, vector<pii>, greater<pii>> pq; for (int i = 1; i <= k; i++){ cin>>a[i]; d[a[i]] = 0; pq.push({0, a[i]}); } while (!pq.empty()){ auto [W, v] = pq.top(); pq.pop(); for (auto [i, w]: g[v]){ int nw = d[v] + w; if (d[i] > nw){ d[i] = nw; pq.push({d[i], i}); } } } for (auto &[w, x, y]: ed) w = min(d[x], d[y]); sort(ed.begin(), ed.end(), greater<arr3>()); RT T(n); for (auto [w, x, y]: ed){ T.add(x, y, w); } T.build(); int Q; cin>>Q; while (Q--){ int a, b; cin>>a>>b; cout<<((a == b) ? d[a] : T.get(a, b))<<"\n"; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...