Submission #637628

#TimeUsernameProblemLanguageResultExecution timeMemory
6376284EVERPutovanje (COCI20_putovanje)C++11
110 / 110
136 ms23464 KiB
#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)

putovanje.cpp: In function 'int main()':
putovanje.cpp:47:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   47 |         freopen(TASK".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
putovanje.cpp:48:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   48 |         freopen(TASK".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...