Submission #213287

#TimeUsernameProblemLanguageResultExecution timeMemory
213287quocnguyen1012Putovanje (COCI20_putovanje)C++14
110 / 110
196 ms24056 KiB
#include <bits/stdc++.h> #define fi first #define se second #define mp make_pair #define pb push_back #define eb emplace_back using namespace std; typedef long long ll; typedef pair<int, int> ii; const int maxn = 2e5 + 5; vector<pair<int, ii>> adj[maxn]; ll c1[maxn], c2[maxn]; int par[maxn][20], depth[maxn], sum[maxn]; int N; void prepare(int u, int p = -1) { for(int i = 1; (1 << i) <= N; ++i){ par[u][i] = par[par[u][i - 1]][i - 1]; } for(auto & v : adj[u]) if(v.fi != p){ c1[v.fi] = v.se.fi; c2[v.fi] = v.se.se; depth[v.fi] = depth[u] + 1; par[v.fi][0] = u; prepare(v.fi, u); } } int LCA(int x, int y) { if(depth[x] < depth[y]) swap(x, y); for(int k = 19; k >= 0; --k){ if (depth[par[x][k]] >= depth[y]){ x = par[x][k]; } } if(x == y) return x; for(int k = 19; k >= 0; --k){ if(par[x][k] != par[y][k]){ x = par[x][k]; y = par[y][k]; } } return par[x][0]; } void dfs(int u, int p = -1) { for(auto & v : adj[u]) if(v.fi != p){ dfs(v.fi, u); sum[u] += sum[v.fi]; } } signed main(void) { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #ifdef LOCAL freopen("A.INP", "r", stdin); freopen("A.OUT", "w", stdout); #endif // LOCAL cin >> N; for(int i = 1; i < N; ++i){ int u, v, w1, w2; cin >> u >> v >> w1 >> w2; adj[u].eb(v, mp(w1, w2)); adj[v].eb(u, mp(w1, w2)); } depth[1] = 1; prepare(1); for(int i = 1; i < N; ++i){ int lc = LCA(i, i + 1); sum[i]++; sum[i + 1]++; sum[lc] -= 2; } dfs(1); ll res = 0; for(int i = 2; i <= N; ++i){ c1[i] *= sum[i]; res += min(c1[i], c2[i]); } cout << res; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...