Submission #526728

#TimeUsernameProblemLanguageResultExecution timeMemory
526728cpp219Designated Cities (JOI19_designated_cities)C++14
100 / 100
381 ms29248 KiB
#include <bits/stdc++.h> #define ll long long #define ld double #define fs first #define sc second #define debug(y) cout<<y,exit(0) using namespace std; typedef pair<ll,ll> LL; const ll N = 2e5 + 9; const ll inf = 1e18 + 7; ll n,sum[N],res[N]; int deg[N]; struct edge{ int v,c,d; }; vector<edge> g[N]; void dfs(int u,int p){ for (auto i : g[u]) if (i.v != p){ dfs(i.v,u); sum[u] += sum[i.v] + i.c; } } void dfs1(int u,int p,ll s){ res[1] = min(res[1],s); for (auto i : g[u]) if (i.v != p) dfs1(i.v,u,s - i.c + i.d); } int main(){ ios_base::sync_with_stdio(NULL); cin.tie(0); cout.tie(0); #define task "tst" if (fopen(task".inp","r")){ freopen(task".inp","r",stdin); //freopen(task".out","w",stdout); } cin>>n; for (ll i = 1;i < n;i++){ int u,v,c,d; cin>>u>>v>>c>>d; g[u].push_back({v,c,d}); g[v].push_back({u,d,c}); deg[u]++; deg[v]++; } res[1] = inf; dfs(1,0); dfs1(1,0,sum[1]); priority_queue<LL,vector<LL>,greater<LL>> pq; for (ll i = 1;i <= n;i++) if (g[i].size() == 1) pq.push({g[i][0].d,i}); //return 0; while(pq.size() > 2){ LL now = pq.top(); pq.pop(); ll nxt = -1; for (auto i : g[now.sc]){ if (deg[i.v] > 1){ deg[i.v]--; if (deg[i.v] == 1) nxt = i.v; else res[pq.size()] = res[pq.size() + 1] + now.fs; } } if (nxt > 0){ for (auto i : g[nxt]) if (deg[i.v] > 1) pq.push({now.fs + i.d,nxt}); } } ll Q; cin>>Q; while(Q--){ ll x; cin>>x; cout<<res[x]<<"\n"; } } /* stuff you should look for * int overflow, array bounds * special cases (n=1?) * do smth instead of nothing and stay organized * WRITE STUFF DOWN * DON'T GET STUCK ON ONE APPROACH */

Compilation message (stderr)

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