제출 #379026

#제출 시각아이디문제언어결과실행 시간메모리
379026rk42745417Evacuation plan (IZhO18_plan)C++17
100 / 100
620 ms41036 KiB
#include <bits/stdc++.h> using namespace std; using ll = int64_t; using ull = uint64_t; using uint = uint32_t; using ld = long double; const int MOD = 1e9 + 7; const int INF = 0x3f3f3f3f; const ll LINF = 2e18; const double EPS = 1e-9; #define EMT ios::sync_with_stdio(0); cin.tie(0); const int N = 1e5 + 25, LGN = 18; int dis[N], anc[LGN][N], dep[N], mn[LGN][N]; bool vis[N]; vector<int> npp; vector<pair<int, int>> edge[N]; vector<int> nwedge[N]; struct DisjointSet { int pa[N], sz[N]; void init(int n) { for(int i = 1; i <= n; i++) sz[i] = 1, pa[i] = i; } int fnd(int x) { return pa[x] == x ? pa[x] : fnd(pa[x]); } bool uni(int a, int b) { if((a = fnd(a)) == (b = fnd(b))) return false; if(sz[a] > sz[b]) swap(a, b); sz[b] += sz[a]; pa[a] = b; return true; } } dsu; void solve() { memset(dis, 0x3f, sizeof dis); priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq; for(int a : npp) dis[a] = 0, pq.push({dis[a], a}); while(!pq.empty()) { auto [c, u] = pq.top(); pq.pop(); if(c > dis[u]) continue; for(const auto &[v, d] : edge[u]) { if(dis[v] > c + d) { dis[v] = c + d; pq.push({dis[v], v}); } } } }; void dfs(int u, int p) { for(int v : nwedge[u]) { if(v == p) continue; dep[v] = dep[u] + 1; mn[0][v] = dis[v]; anc[0][v] = u; dfs(v, u); } } void build(int n) { for(int i = 1; i < LGN; i++) for(int j = 1; j <= n; j++) anc[i][j] = anc[i - 1][anc[i - 1][j]], mn[i][j] = min(mn[i - 1][j], mn[i - 1][anc[i - 1][j]]); } int lca(int a, int b) { int res = INF; if(dep[a] < dep[b]) swap(a, b); for(int i = 0, x = dep[a] - dep[b]; i < LGN; i++) if(x >> i & 1) res = min(res, mn[i][a]), a = anc[i][a]; if(a == b) return min(res, dis[a]); for(int i = LGN - 1; ~i; i--) { if(anc[i][a] == anc[i][b]) continue; res = min(res, mn[i][a]); res = min(res, mn[i][b]); a = anc[i][a]; b = anc[i][b]; } return min({res, mn[0][a], mn[0][b], dis[anc[0][a]]}); } const int SQN = 350; signed main() { EMT int n, m; cin >> n >> m; for(int i = 0; i < m; i++) { int u, v, w; cin >> u >> v >> w; edge[u].push_back({v, w}); edge[v].push_back({u, w}); } int k; cin >> k; npp.resize(k); for(int &a : npp) cin >> a; solve(); dsu.init(n); vector<pair<int, int>> tmp(n); for(int i = 1; i <= n; i++) { tmp[i - 1] = {dis[i], i}; } sort(tmp.begin(), tmp.end(), greater<pair<int, int>>()); for(const auto &[w, u] : tmp) { vis[u] = 1; for(const auto &[v, d] : edge[u]) if(vis[v] && dsu.uni(u, v)) nwedge[u].push_back(v), nwedge[v].push_back(u); } assert(dsu.sz[dsu.fnd(1)] == n); dfs(1, 1); build(n); int q; cin >> q; while(q--) { int a, b; cin >> a >> b; cout << lca(a, b) << '\n'; } } /* 9 12 1 9 4 1 2 5 2 3 7 2 4 3 4 3 6 3 6 4 8 7 10 6 7 5 5 8 1 9 5 7 5 4 12 6 8 2 2 4 7 5 1 6 5 3 4 8 5 8 1 5 */
#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...