Submission #200378

#TimeUsernameProblemLanguageResultExecution timeMemory
200378arnold518Designated Cities (JOI19_designated_cities)C++14
100 / 100
1020 ms62692 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int MAXN = 2e5; int N, Q, lf; map<int, int> adj[MAXN+10]; set<int> leaf[MAXN+10]; int pos[MAXN+10]; ll dist[MAXN+10], ans[MAXN+10]; void dfs(int now, int bef) { for(auto nxt : adj[now]) { if(nxt.first==bef) continue; ans[1]+=nxt.second; dfs(nxt.first, now); } } void dfs2(int now, int bef, ll val) { ans[1]=min(ans[1], val); for(auto nxt : adj[now]) { if(nxt.first==bef) continue; dfs2(nxt.first, now, val-adj[now][nxt.first]+adj[nxt.first][now]); } } pll propa(int u) { int now=u, nxt; ll ret=0; while(adj[now].size()==1 && leaf[now].size()==0) { nxt=adj[now].begin()->first; ret+=adj[nxt][now]; adj[now].erase(nxt); adj[nxt].erase(now); now=nxt; } return {now, ret}; } int main() { int i, j; scanf("%d", &N); for(i=1; i<N; i++) { int a, b, c, d; scanf("%d%d%d%d", &a, &b, &c, &d); adj[a][b]=c; adj[b][a]=d; } dfs2(1, 1, 0); dfs(1, 1); set<pll> PQ; vector<int> V; for(i=1; i<=N; i++) if(adj[i].size()==1) V.push_back(i); for(auto i : V) { pll t=propa(i); leaf[t.first].insert(i); dist[i]=t.second; pos[i]=t.first; PQ.insert({dist[i], i}); //printf("%d %lld\n", i, dist[i]); lf++; } ll cost=0; for(i=lf-1; i>=2; i--) { pll now=*PQ.begin(); PQ.erase(PQ.begin()); cost+=now.first; //printf("!%lld %lld\n", now.first, now.second); assert(adj[now.second].size()==0); leaf[pos[now.second]].erase(now.second); if(leaf[pos[now.second]].size()==1) { int u=*leaf[pos[now.second]].begin(); leaf[pos[now.second]].erase(leaf[pos[now.second]].begin()); PQ.erase({dist[u], u}); pll t=propa(pos[now.second]); leaf[t.first].insert(u); dist[u]+=t.second; pos[u]=t.first; PQ.insert({dist[u], u}); } ans[i]=cost; } scanf("%d", &Q); while(Q--) { int p; scanf("%d", &p); printf("%lld\n", ans[p]); } }

Compilation message (stderr)

designated_cities.cpp: In function 'int main()':
designated_cities.cpp:52:9: warning: unused variable 'j' [-Wunused-variable]
  int i, j;
         ^
designated_cities.cpp:54:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &N);
  ~~~~~^~~~~~~~~~
designated_cities.cpp:58:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d%d%d", &a, &b, &c, &d);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
designated_cities.cpp:102:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &Q);
  ~~~~~^~~~~~~~~~
designated_cities.cpp:106:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &p);
   ~~~~~^~~~~~~~~~
#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...