Submission #342641

#TimeUsernameProblemLanguageResultExecution timeMemory
342641NurstanDuisengalievEvacuation plan (IZhO18_plan)C++14
100 / 100
981 ms63344 KiB
// Nurstan Duisengaliev // не, не надо меня узнавать /*#pragma GCC target ("avx2") #pragma GCC optimize ("Ofast") #pragma GCC optimize ("unroll-loops") #pragma GCC target("avx,avx2,fma") #pragma GCC optimize("O3")*/ #include <bits/stdc++.h> //#include <ext/pb_ds/assoc_container.hpp> //#include <ext/pb_ds/tree_policy.hpp> #define ll long long #define all(x) x.begin(), x.end() #define in insert #define mp make_pair #define F first #define S second #define ppf pop_front #define pb push_back #define ppb pop_back #define pf push_front #define pii pair <int, int> #define pll pair <ll, ll> #define boost() ios_base::sync_with_stdio(0), cin.tie(0) #define sz(x) (int)x.size() using namespace std; //using namespace __gnu_pbds; //template<typename T> using ordered_set = tree <T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; const int N = (int)3e5 + 123; const int mod = (int)1e9 + 7; const ll INF = (ll)1e18 + 1; int n, m; vector <pii> VE, ve, g[N]; vector <int> t[N]; int d[N]; set <pii> s; int par[N], NN, ok[N]; int GetPar (int v) { if (par[v] == v) return v; return par[v] = GetPar (par[v]); } void calc () { for (auto it : VE) { par[it.S] = it.S; } for (auto it : VE) { int v = it.S; for (auto to : g[v]) { if (GetPar(to.F) != 0 && GetPar (to.F) != GetPar (v)) { NN ++; t[GetPar (to.F)].pb (NN); t[NN].pb (GetPar (to.F)); t[GetPar (v)].pb (NN); t[NN].pb (GetPar (v)); par[GetPar (v)] = NN; par[GetPar (to.F)] = NN; par[NN] = NN; ok[NN] = it.F; } } } } int tin[N], tout[N], timer = 0, up[N][21]; void dfs (int v, int p) { tin[v] = ++ timer; up[v][0] = p; for (int i = 1; i <= 20; i ++) up[v][i] = up[up[v][i - 1]][i - 1]; for (auto to : t[v]) { if (to != p) dfs (to, v); } tout[v] = timer; } bool anc (int v, int u) { return (tin[v] <= tin[u] && tout[v] >= tout[u]); } int LCA (int v, int u) { if (anc (v, u)) return v; if (anc (u, v)) return u; for (int i = 20; i >= 0; i --) { if (!anc (up[v][i], u)) v = up[v][i]; } return up[v][0]; } void solve () { cin >> n >> m; NN = n; for (int i = 1; i <= m; i ++) { int l, r, w; cin >> l >> r >> w; g[l].pb (mp (r, w)); g[r].pb (mp (l, w)); } for (int i = 1; i <= n; i ++) d[i] = mod; int K; cin >> K; for (int i = 1; i <= K; i ++) { int x; cin >> x; s.in (mp (0, x)); d[x] = 0; } for (int i = 1; i <= n; i ++) { if (d[i] == mod) s.in (mp (mod, i)); } while (sz (s)) { pii o = *s.begin(); s.erase (o); int v = o.S; for (auto to : g[v]) { if (d[to.F] > d[v] + to.S) { s.erase (mp (d[to.F], to.F)); d[to.F] = d[v] + to.S; s.in (mp (d[to.F], to.F)); } } } for (int i = 1; i <= n; i ++) { ve.pb (mp (d[i], i)); } sort (all (ve)); reverse (all (ve)); VE.pb (ve[0]); for (int i = 1; i < sz (ve); i ++) { if (ve[i].F == ve[i - 1].F) VE.pb (ve[i]); else { calc (); VE.clear (); VE.pb (ve[i]); } } calc (); dfs (NN, NN); int Q; cin >> Q; while (Q --) { int v, u; cin >> v >> u; int x = LCA (v, u); cout << ok[x] << '\n'; } } main () { // freopen (".in", "r", stdin); // freopen (".out", "w", stdout); boost (); int TT = 1; // cin >> TT; while (TT --) { solve (); } return 0; }

Compilation message (stderr)

plan.cpp:147:7: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  147 | main () {
      |       ^
#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...