Submission #342960

#TimeUsernameProblemLanguageResultExecution timeMemory
342960alradPutovanje (COCI20_putovanje)C++17
110 / 110
214 ms42732 KiB
#include <bits/stdc++.h> using namespace std; using ld = long double; using ull = unsigned long long; /* #pragma comment(linker, "/STACK:256000000") #pragma GCC optimize("unroll-loops") #pragma GCC optimize("Ofast") #pragma GCC optimize("-O3") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") */ template <class T> inline T gcd(T a , T b) { return !a ? b : gcd(b % a , a); } template <class T> inline T lcm(T a , T b) { return (a * b) / gcd(a , b) ; } mt19937 rnd(time(0)); #define all(x) x.begin(), x.end() #define debug(x) { cerr << #x << " = " << x << endl; } const int N = 2e5 + 2; const int LOG = 20; int timer = 0, tin[N], tout[N], prefix[N], dp[N]; vector<vector<pair<int, int>>> g(N, vector<pair<int, int>>()); vector<vector<int>> up(N, vector<int>(LOG, 1)); void dfs1(int v, int p = 1) { tin[v] = timer++; up[v][0] = p; for (int i = 1; i < LOG; i++) { up[v][i] = up[up[v][i - 1]][i - 1]; } for (auto edge : g[v]) { int to = edge.first; if (to != p) { dfs1(to, v); } } tout[v] = timer++; } bool ancestor(int a, int b) { return (tin[a] <= tin[b] && tout[a] >= tout[b]); } int lca(int a, int b) { if (ancestor(a, b)) { return a; } if (ancestor(b, a)) { return b; } for (int i = LOG - 1; i >= 0; i--) { if (!ancestor(up[a][i], b)) { a = up[a][i]; } } return up[a][0]; } void dfs2(int v, int p = -1, int in = -1) { for (auto edge : g[v]) { int to = edge.first, num = edge.second; if (to != p) { dfs2(to, v, num); prefix[v] += prefix[to]; } } if (in != -1) { dp[in] = prefix[v]; } } void solve() { int n; cin >> n; vector<int> cost1(n), cost2(n); for (int i = 1; i < n; i++) { int u, v; cin >> u >> v >> cost1[i] >> cost2[i]; g[u].push_back({v, i}); g[v].push_back({u, i}); } dfs1(1); for (int i = 1; i < n; i++) { prefix[i]++; prefix[i + 1]++; prefix[lca(i, i + 1)] -= 2; } dfs2(1); long long ans = 0ll; for (int i = 1; i < n; i++) { ans += min(1ll * cost2[i], cost1[i] * 1ll * dp[i]); } cout << ans << '\n'; return; } signed main() { ios_base :: sync_with_stdio(0); cin.tie(0) , cout.tie(0); int t = 1; while (t-- > 0) { solve(); } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...