# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
200372 | 2020-02-06T12:28:27 Z | arnold518 | Designated Cities (JOI19_designated_cities) | C++14 | 972 ms | 56568 KB |
#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(u); 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
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 17 ms | 19192 KB | Output is correct |
2 | Correct | 17 ms | 19064 KB | Output is correct |
3 | Correct | 17 ms | 19064 KB | Output is correct |
4 | Incorrect | 17 ms | 19192 KB | Output isn't correct |
5 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 17 ms | 19192 KB | Output is correct |
2 | Correct | 655 ms | 42348 KB | Output is correct |
3 | Correct | 517 ms | 53240 KB | Output is correct |
4 | Correct | 578 ms | 42216 KB | Output is correct |
5 | Correct | 712 ms | 42220 KB | Output is correct |
6 | Correct | 671 ms | 44264 KB | Output is correct |
7 | Correct | 843 ms | 43756 KB | Output is correct |
8 | Correct | 489 ms | 56568 KB | Output is correct |
9 | Correct | 972 ms | 52072 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 17 ms | 19064 KB | Output is correct |
2 | Incorrect | 632 ms | 42220 KB | Output isn't correct |
3 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 17 ms | 19192 KB | Output is correct |
2 | Correct | 17 ms | 19064 KB | Output is correct |
3 | Correct | 17 ms | 19064 KB | Output is correct |
4 | Incorrect | 17 ms | 19192 KB | Output isn't correct |
5 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 17 ms | 19192 KB | Output is correct |
2 | Correct | 655 ms | 42348 KB | Output is correct |
3 | Correct | 517 ms | 53240 KB | Output is correct |
4 | Correct | 578 ms | 42216 KB | Output is correct |
5 | Correct | 712 ms | 42220 KB | Output is correct |
6 | Correct | 671 ms | 44264 KB | Output is correct |
7 | Correct | 843 ms | 43756 KB | Output is correct |
8 | Correct | 489 ms | 56568 KB | Output is correct |
9 | Correct | 972 ms | 52072 KB | Output is correct |
10 | Correct | 17 ms | 19064 KB | Output is correct |
11 | Incorrect | 632 ms | 42220 KB | Output isn't correct |
12 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 17 ms | 19192 KB | Output is correct |
2 | Correct | 17 ms | 19064 KB | Output is correct |
3 | Correct | 17 ms | 19064 KB | Output is correct |
4 | Incorrect | 17 ms | 19192 KB | Output isn't correct |
5 | Halted | 0 ms | 0 KB | - |