Submission #1166796

#TimeUsernameProblemLanguageResultExecution timeMemory
1166796CrabCNHEvacuation plan (IZhO18_plan)C++20
0 / 100
276 ms69340 KiB
#include <bits/stdc++.h> #define task "BriantheCrab" #define int long long #define pii pair <int, int> #define fi first #define se second #define szf sizeof #define sz(s) (int)((s).size()) using namespace std; template <class T> void mini (T &t, T f) {if (t > f) t = f;} template <class T> void maxi (T &t, T f) {if (t < f) t = f;} const int maxN = 1e5 + 5; const int LOG = 20; const int inf = 1e18 + 7; const int mod = 1e9 + 7; struct Node { int u, dis; bool operator < (const Node& other) const { return dis > other.dis; } }; int n, m, K; int L[maxN], d[maxN], h[maxN]; int par[maxN], sz[maxN]; int mi[maxN][LOG], mn[maxN][LOG], up[maxN][LOG]; bool check[maxN]; vector <Node> adj[maxN]; vector <int> nAdj[maxN], spe; vector <pii> all; void multiSources () { priority_queue <Node> pq; for (int i = 1; i <= n; i ++) { L[i] = inf; } for (auto u : spe) { L[u] = 0; pq.push ({u, 0}); } while (!pq.empty ()) { auto [u, c] = pq.top (); pq.pop (); if (c > L[u]) { continue; } for (auto [v, dis] : adj[u]) { if (L[v] > L[u] + dis) { L[v] = L[u] + dis; pq.push ({v, L[v]}); } } } } void init () { for (int i = 1; i <= n; i ++) { par[i] = i; sz[i] = 1; } } int getRoot (int u) { if (par[u] == u) { return u; } return (par[u] = getRoot (par[u])); } void merge (int u, int v) { u = getRoot (u); v = getRoot (v); if (u == v) { return; } if (sz[u] < sz[v]) { swap (u, v); } par[v] = u; sz[u] += sz[v]; return; } void dfs (int u, int p) { up[u][0] = p; for (int j = 1; j < LOG; j ++) { up[u][j] = up[up[u][j - 1]][j - 1]; mi[u][j] = min (mi[u][j - 1], mi[up[u][j - 1]][j - 1]); } for (auto v : nAdj[u]) { if (v == p) { continue; } h[v] = h[u] + 1; mi[v][0] = L[v]; dfs (v, u); } } int LCA (int u, int v) { if (h[u] < h[v]) { swap (u, v); } int k = h[u] - h[v]; int res = inf; for (int j = LOG - 1; j >= 0; j --) { if ((k >> j) & 1) { res = min (res, mi[u][j]); u = up[u][j]; } } if (u == v) { return res; } for (int j = LOG - 1; j >= 0; j --) { if (up[u][j] != up[v][j]) { res = min ({res, mi[u][j], mi[v][j]}); u = up[u][j]; v = up[v][j]; } } return min ({res, mi[u][0], mi[v][0]}); } void solve () { cin >> n >> m; for (int i = 1; i <= m; i ++) { int u, v, w; cin >> u >> v >> w; adj[u].push_back ({v, w}); adj[v].push_back ({u, w}); } cin >> K; for (int i = 1; i <= K; i ++) { int u; cin >> u; spe.push_back (u); } multiSources (); for (int i = 1; i <= n; i ++) { all.push_back ({L[i], i}); } sort (all.rbegin (), all.rend ()); init (); for (auto [_, u] : all) { check[u] = 1; for (auto [v, w] : adj[u]) { int rtU = getRoot (u); int rtV = getRoot (v); if (rtU == rtV || !check[v]) { continue; } merge (u, v); nAdj[u].push_back (v); nAdj[v].push_back (u); } } dfs (1, 0); int q; cin >> q; while (q --) { int u, v; cin >> u >> v; cout << LCA (u, v) << '\n'; } return; } signed main () { cin.tie (nullptr) -> sync_with_stdio (false); if (fopen (task".inp", "r")) { freopen (task".inp", "r", stdin); freopen (task".out", "w", stdout); } int t = 1; //cin >> t; while (t --) { solve (); } return 0; } // thfdgb

Compilation message (stderr)

plan.cpp: In function 'int main()':
plan.cpp:179:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  179 |         freopen (task".inp", "r", stdin);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
plan.cpp:180:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  180 |         freopen (task".out", "w", stdout);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#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...