제출 #653724

#제출 시각아이디문제언어결과실행 시간메모리
653724Koful123Putovanje (COCI20_putovanje)C++17
110 / 110
158 ms39640 KiB
#include <bits/stdc++.h> using namespace std; #define endl "\n" #define int long long #define pb push_back #define ff first #define ss second #define mod 1000000007 #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() const int N = 2e5 + 5; int n; vector<int> cnt(N),sum(N),vis(N),depth(N); vector<pair<int,int>> adj[N],val(N); int par[N][20]; void dfs(int node,int p){ for(auto[go,who] : adj[node]){ if(go == p) continue; dfs(go,node); } for(auto[go,who] : adj[node]){ if(go == p) continue; cnt[who] = sum[go],sum[node] += sum[go]; } } void find(int node,int p){ par[node][0] = p,depth[node] = depth[p] + 1; for(auto[go,who] : adj[node]){ if(go == p) continue; find(go,node); } } int lca(int x,int y){ if(depth[x] < depth[y]) swap(x,y); int d = depth[x] - depth[y]; for(int i=19;i>=0;i--){ if(d & 1ll<<i){ x = par[x][i]; } } if(x == y) return x; for(int i=19;i>=0;i--){ if(par[x][i] != par[y][i]){ x = par[x][i],y = par[y][i]; } } return par[x][0]; } void solve(){ cin >> n; for(int i=1;i<n;i++){ int a,b; cin >> a >> b >> val[i].ff >> val[i].ss; adj[a].pb({b,i}); adj[b].pb({a,i}); } find(1,0); for(int i=1;i<20;i++){ for(int j=1;j<=n;j++){ par[j][i] = par[par[j][i-1]][i-1]; } } for(int i=1;i<n;i++){ int get = lca(i,i+1); sum[get] -= 2,sum[i]++,sum[i+1]++; } dfs(1,0); int ans = 0; for(int i=1;i<n;i++){ ans += min(cnt[i] * val[i].ff,val[i].ss); } cout << ans << endl; } signed main(){ ios::sync_with_stdio(0); cin.tie(0); int t = 1; // cin >> t; while(t--) solve(); return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...