Submission #1231764

#TimeUsernameProblemLanguageResultExecution timeMemory
1231764PlayVoltzEvacuation plan (IZhO18_plan)C++20
100 / 100
560 ms81964 KiB
#include <cstdio> #include <stdio.h> #include <stdbool.h> #include <iostream> #include <map> #include <vector> #include <climits> #include <stack> #include <string> #include <queue> #include <algorithm> #include <set> #include <unordered_set> #include <unordered_map> #include <cmath> #include <cctype> #include <bitset> #include <iomanip> #include <cstring> #include <numeric> #include <cassert> #include <random> #include <chrono> #include <fstream> using namespace std; #define int long long #define pii pair<int, int> #define mp make_pair #define pb push_back #define fi first #define se second vector<int> dsu, depth, dj; vector<vector<int> > twok, mn; vector<vector<pii> > graph; int fp(int a){ if (dsu[a]==-1)return a; return dsu[a]=fp(dsu[a]); } bool merge(int a, int b){ a=fp(a), b=fp(b); if (a==b)return 0; dsu[a]=b; return 1; } void dfs(int node, int par, int d){ depth[node]=d; twok[node][0]=par; mn[node][0]=dj[par]; for (int i=1; i<20; ++i)twok[node][i]=twok[twok[node][i-1]][i-1], mn[node][i]=min(mn[node][i-1], mn[twok[node][i-1]][i-1]); for (auto num:graph[node])if (num.fi!=par)dfs(num.fi, node, d+1); } int lca(int a, int b){ if (depth[a]<depth[b])swap(a, b); int res=min(dj[a], dj[b]); for (int i=0, k=depth[a]-depth[b]; i<20; ++i)if (k&(1<<i))res=min(res, mn[a][i]), a=twok[a][i]; if (a==b)return res; for (int i=19; i>=0; --i)if (twok[a][i]!=twok[b][i])res=min({res, mn[a][i], mn[b][i]}), a=twok[a][i], b=twok[b][i]; return min(res, dj[twok[a][0]]); } int32_t main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m, k, q, a, b, c; cin>>n>>m; graph.resize(n+1); vector<pii> temp; while (m--){ cin>>a>>b>>c; temp.pb(mp(a, b)); graph[a].pb(mp(b, c)); graph[b].pb(mp(a, c)); } dj.resize(n+1, LLONG_MAX/2); priority_queue<pii, vector<pii>, greater<pii> > pq; cin>>k; while (k--)cin>>a, pq.push(mp(0, a)); while (pq.size()){ int d=pq.top().fi, node=pq.top().se; pq.pop(); if (d>=dj[node])continue; dj[node]=d; for (auto num:graph[node])pq.push(mp(dj[node]+num.se, num.fi)); } vector<pair<int, pii> > edges; for (auto a:temp)edges.pb(mp(min(dj[a.fi], dj[a.se]), mp(a.fi, a.se))); sort(edges.begin(), edges.end(), greater<pair<int, pii> >()); dsu.resize(n+1, -1); graph.clear(); graph.resize(n+1); for (auto c:edges)if (merge(c.se.fi, c.se.se)){ graph[c.se.fi].pb(mp(c.se.se, c.fi)); graph[c.se.se].pb(mp(c.se.fi, c.fi)); } depth.resize(n+1); twok.resize(n+1, vector<int>(20)); mn.resize(n+1, vector<int>(20, LLONG_MAX/2)); dfs(1, 1, 0); cin>>q; while (q--){ cin>>a>>b; cout<<lca(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...