Submission #571456

#TimeUsernameProblemLanguageResultExecution timeMemory
571456bojackduyEvacuation plan (IZhO18_plan)C++14
100 / 100
714 ms76828 KiB
#include<iostream> #include<queue> #include<stack> #include<algorithm> #include<string.h> #define task #define size() size() * 1ll #define all(x) (x).begin(), (x).end() #define allr(x, sz) (x) + 1, (x) + 1 + sz #define pb push_back #define pii pair<int,int> #define fi first #define se second #define MASK(x) (1LL<<(x)) #define BIT(x,i) (((x)>>(i))&1) #define numbit(x) __builtin_popcountll(x) using namespace std; const int N = 1e6 + 1; const int M = 1e3 + 1; const long long mod = 1e9 + 7; const long long oo = 1e18 + 7; typedef vector<int> vi; typedef vector<pii> vii; typedef long long ll; template<class t> bool mini(t &x,t y) { if (x > y) { x = y; return 1; } return 0; } template<class t> bool maxi(t &x,t y) { if (x < y) { x = y; return 1; } return 0; } void file() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); //freopen(task.in, r, stdin); //freopen(task.out, w, stdout); return ; } int n, m, k, Q; int v[N], cha[N], sub[N], h[N], heavy[N], head[N], pos[N], d[N]; vi adj[N]; vii a[N]; pii e[N]; struct DSU { int n; vi cha; DSU(const int &_n = 0): n(_n + 3), cha(_n + 7, -1) {} int root(int u) { return (cha[u] < 0 ? u : cha[u] = root(cha[u])); } bool join(int u, int v) { if ((u = root(u)) == (v = root(v))) return 0; if (cha[u] > cha[v]) swap(u, v); cha[u] += cha[v]; cha[v] = u; return 1; } }; struct IT { int n; vi st; IT(const int &_n = 0): n(_n + 1), st(4 * _n + 1, mod) {} void adj(int id, int l, int r, int i, int val) { if (i < l || r < i) return ; if (l == r) { st[id] = val; return ; } int mid = l + r >> 1; adj(id << 1, l, mid, i, val); adj(id << 1 | 1, mid + 1, r, i, val); st[id] = min(st[id << 1], st[id << 1 | 1]); } int get(int id, int l, int r, int u, int v) { if (v < l || r < u) return mod; if (u <= l && r <= v) return st[id]; int mid = l + r >> 1; return min(get(id << 1, l, mid, u, v), get(id << 1 | 1, mid + 1, r, u, v)); } void adj(int i, int val) { adj(1, 1, n, i, val); } int get(int u, int v) { return get(1, 1, n, u, v); } }it; void build_tree() { cin >> k; priority_queue<pii, vii, greater<pii>> q; for (int i = 1; i <= n; i++) d[i] = mod; for (int i = 1; i <= k; i++) { cin >> v[i]; q.push(pii(0, v[i])); d[v[i]] = 0; } while (q.size()) { int u = q.top().se; int du = q.top().fi; q.pop(); if (d[u] < du) continue ; for (auto v: a[u]) { if (mini(d[v.se], d[u] + v.fi)) { q.push(pii(d[v.se], v.se)); } } } sort(e + 1, e + 1 + m, [&](const pii &x, const pii &y){ return min(d[x.fi], d[x.se]) < min(d[y.fi], d[y.se]); }); DSU dsu(n); for (int i = m; i >= 1; i--) { if (dsu.join(e[i].fi, e[i].se)) { adj[e[i].fi].pb(e[i].se); adj[e[i].se].pb(e[i].fi); } } } void dfs(int u, int chu) { cha[u] = chu; sub[u] = 1; int cmax = -1; for (auto v: adj[u]) if (v != chu) { h[v] = h[u] + 1; dfs(v, u); sub[u] += sub[v]; if (maxi(cmax, sub[v])) { heavy[u] = v; } } } void hld(int u, int h) { head[u] = h; pos[u] = ++pos[0]; it.adj(pos[0], d[u]); if (heavy[u]) hld(heavy[u], h); for (auto v: adj[u]) if (v != cha[u]) { if (v != heavy[u]) hld(v, v); } } int lca(int u, int v) { int res = mod; while (head[u] != head[v]) { if (h[head[u]] < h[head[v]]) swap(u, v); int x = head[u]; mini(res, it.get(pos[x], pos[u])); u = cha[x]; } if (h[u] < h[v]) swap(u, v); mini(res, it.get(pos[v], pos[u])); return res; } void solve(int test = -1) { cin >> n >> m; for (int i = 1; i <= m; i++) { int x, y, z; cin >> x >> y >> z; e[i] = pii(x, y); a[x].pb(pii(z, y)); a[y].pb(pii(z, x)); } build_tree(); dfs(1, 1); it = IT(n); hld(1, 1); cin >> Q; while (Q--) { int s, t; cin >> s >> t; cout << lca(s, t) << '\n'; } } int32_t main() { file(); int T = 1; // cin >> T; for (int i = 1; i <= T; i++) { solve(i); } return 0; }

Compilation message (stderr)

plan.cpp: In member function 'void IT::adj(int, int, int, int, int)':
plan.cpp:85:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   85 |         int mid = l + r >> 1;
      |                   ~~^~~
plan.cpp: In member function 'int IT::get(int, int, int, int, int)':
plan.cpp:93:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   93 |         int mid = l + r >> 1;
      |                   ~~^~~
plan.cpp: In function 'void build_tree()':
plan.cpp:7:23: warning: '*' in boolean context, suggest '&&' instead [-Wint-in-bool-context]
    7 | #define size() size() * 1ll
      |                       ^
plan.cpp:114:14: note: in expansion of macro 'size'
  114 |     while (q.size()) {
      |              ^~~~
#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...