Submission #891707

#TimeUsernameProblemLanguageResultExecution timeMemory
891707ind1vPutovanje (COCI20_putovanje)C++11
110 / 110
85 ms48212 KiB
#include <bits/stdc++.h> using namespace std; #define int long long const int N = 2e5 + 4; const int LG = 20; int par[N][LG]; int a[N], b[N], d[N], lz[N]; array<int, 2> c[N]; array<int, 2> up[N]; vector<array<int, 3>> adj[N]; int n, ans; void dfs(int u, int v, int depth) { d[u] = depth; par[u][0] = v; for (int i = 0; i < (int)adj[u].size(); i++) { int s = adj[u][i][0]; int c1 = adj[u][i][1], c2 = adj[u][i][2]; if (s != v) { array<int, 2> nx; nx[0] = c1; nx[1] = c2; up[s] = nx; dfs(s, u, depth + 1); } } } void dfs2(int u, int v) { for (int i = 0; i < (int)adj[u].size(); i++) { int s = adj[u][i][0]; if (s != v) { dfs2(s, u); lz[u] += lz[s]; } } } int lca(int u, int v) { if (d[u] < d[v]) { swap(u, v); } int df = d[u] - d[v]; for (int i = LG - 1; i >= 0; i--) { if (df >= (1 << i)) { df -= (1 << i); u = par[u][i]; } } if (u == v) { return u; } for (int i = LG - 1; i >= 0; i--) { if (par[u][i] != par[v][i]) { u = par[u][i]; v = par[v][i]; } } return par[u][0]; } int32_t main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n; for (int i = 1; i <= n - 1; i++) { cin >> a[i] >> b[i] >> c[i][0] >> c[i][1]; array<int, 3> nz; nz[0] = b[i]; nz[1] = c[i][0]; nz[2] = c[i][1]; adj[a[i]].push_back(nz); array<int, 3> nx; nx[0] = a[i]; nx[1] = c[i][0]; nx[2] = c[i][1]; adj[b[i]].push_back(nx); } dfs(1, -1, 0); for (int j = 1; j < LG; j++) { for (int i = 1; i <= n; i++) { if (par[i][j - 1] == -1) { par[i][j] = -1; continue; } par[i][j] = par[par[i][j - 1]][j - 1]; } } for (int i = 2; i <= n; i++) { ++lz[i - 1]; ++lz[i]; lz[lca(i, i - 1)] -= 2; } dfs2(1, -1); ans = 0; for (int i = 1; i <= n; i++) { ans += min(lz[i] * up[i][0], up[i][1]); } cout << ans; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...