# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
637628 | 4EVER | Putovanje (COCI20_putovanje) | C++11 | 136 ms | 23464 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int n;
vector<pair<int, int>> adj[200001];
int val[200001], cnt[200001], depth[200001], c[200001], d[200001];
int spt[19][200001];
void DFS(int u = 1, int par = -1){
for(auto x : adj[u]){
int v = x.first;
if(v == par) continue;
depth[v] = depth[u] + 1;
spt[0][v] = u;
for(int i = 1; i <= 18; i++) spt[i][v] = spt[i - 1][spt[i - 1][v]];
DFS(v, u);
}
}
int LCA(int u, int v){
if(depth[u] < depth[v]) swap(u, v);
int k = depth[u] - depth[v];
for(int i = 18; i >= 0; i--) if((k >> i) & 1) u = spt[i][u];
if(u == v) return u;
for(int i = 18; i >= 0; i--) if(spt[i][u] != spt[i][v]){
u = spt[i][u];
v = spt[i][v];
}
return spt[0][u];
}
void reDFS(int u = 1, int par = -1, int id = -1){
for(auto x : adj[u]){
int v = x.first;
int w = x.second;
if(v == par) continue;
reDFS(v, u, w);
val[u] += val[v];
}
if(id != -1) cnt[id] = val[u];
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define TASK "PUTOVANJE"
if(fopen(TASK".inp", "r")) {
freopen(TASK".inp", "r", stdin);
freopen(TASK".out", "w", stdout);
}
cin >> n;
for(int i = 1; i < n; i++){
int u, v; cin >> u >> v;
cin >> c[i] >> d[i];
adj[u].push_back({v, i});
adj[v].push_back({u, i});
}
DFS();
for(int i = 2; i <= n; i++){
val[i]++;
val[i - 1]++;
val[LCA(i, i - 1)] -= 2;
}
reDFS();
long long res = 0;
for(int i = 1; i < n; i++){
res += min((long long) c[i] * cnt[i], (long long) d[i]);
}
cout << res;
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |