# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
605151 | 2022-07-25T13:28:13 Z | jhnah917 | Designated Cities (JOI19_designated_cities) | C++14 | 2000 ms | 22808 KB |
#include <bits/stdc++.h> using namespace std; using ll = long long; ll N, Q, Sum, CostTo1, C[202020], R[202020]; vector<pair<ll,ll>> G[202020]; int S[202020], U[202020]; void TreeDP(int v, int b=-1, ll up=0, ll dw=0){ for(auto [i,w] : G[v]) if(i == b) CostTo1 += w, up += w; C[v] = dw - up; for(auto [i,w] : G[v]) if(i != b) TreeDP(i, v, up, dw+w); } ll CostToRoot(int root){ return CostTo1 + C[root]; } vector<pair<ll,ll>> CostFromRoot(int root){ vector<pair<ll,ll>> vec; function<ll(int,int,int)> dfs = [&](int st, int v, int b) -> ll { ll mx = 0; for(auto [i,w] : G[v]){ if(i == b || U[i]) continue; ll nxt = dfs(st, i, v) + w; if(nxt > mx) swap(nxt, mx); if(nxt > 0) vec.emplace_back(nxt, st); } return mx; }; for(auto [i,w] : G[root]) if(!U[i]) vec.emplace_back(dfs(i, i, -1) + w, i); return vec; } int GetSize(int v, int b=-1){ S[v] = 1; for(auto [i,w] : G[v]) if(i != b && !U[i]) S[v] += GetSize(i, v); return S[v]; } int GetCent(int v, int tot, int b=-1){ for(auto [i,w] : G[v]) if(i != b && !U[i] && S[i]*2 > tot) return GetCent(i, tot, v); return v; } void GetAnswer(int v=1){ v = GetCent(v, GetSize(v)); U[v] = 1; auto vec = CostFromRoot(v); sort(vec.begin(), vec.end(), greater<>()); ll now = CostToRoot(v); for(int k=2; k<=N; k++){ if(k-2 < vec.size()) now += vec[k-2].first; R[k] = min(R[k], Sum - now); } int mx2 = -1; for(int i=1; i<vec.size(); i++) if(vec[i].second != vec[0].second) { mx2 = i; break; } if(mx2 != -1){ now = CostToRoot(v) + vec[mx2].first; vec.erase(vec.begin()+mx2); for(int k=2; k<=N; k++){ if(k-2 < vec.size()) now += vec[k-2].first; R[k] = min(R[k], Sum - now); } } for(auto [i,w] : G[v]) if(!U[i]) GetAnswer(i); } int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> N; for(int i=1; i<N; i++){ int a, b, c, d; cin >> a >> b >> c >> d; Sum += c + d; G[a].emplace_back(b, c); G[b].emplace_back(a, d); } TreeDP(1); memset(R, 0x3f, sizeof R); R[1] = Sum - CostTo1 - *max_element(C+1, C+N+1); GetAnswer(); cin >> Q; for(int i=1; i<=Q; i++){ int t; cin >> t; cout << R[t] << "\n"; } }
Compilation message
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 3 ms | 6612 KB | Output is correct |
2 | Correct | 3 ms | 6612 KB | Output is correct |
3 | Correct | 3 ms | 6612 KB | Output is correct |
4 | Correct | 3 ms | 6612 KB | Output is correct |
5 | Correct | 3 ms | 6612 KB | Output is correct |
6 | Correct | 3 ms | 6664 KB | Output is correct |
7 | Correct | 3 ms | 6612 KB | Output is correct |
8 | Correct | 3 ms | 6612 KB | Output is correct |
9 | Correct | 3 ms | 6612 KB | Output is correct |
10 | Correct | 3 ms | 6612 KB | Output is correct |
11 | Correct | 3 ms | 6596 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 3 ms | 6612 KB | Output is correct |
2 | Execution timed out | 2073 ms | 22276 KB | Time limit exceeded |
3 | Halted | 0 ms | 0 KB | - |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 3 ms | 6612 KB | Output is correct |
2 | Execution timed out | 2076 ms | 22808 KB | Time limit exceeded |
3 | Halted | 0 ms | 0 KB | - |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 3 ms | 6612 KB | Output is correct |
2 | Correct | 3 ms | 6612 KB | Output is correct |
3 | Correct | 3 ms | 6612 KB | Output is correct |
4 | Correct | 3 ms | 6612 KB | Output is correct |
5 | Correct | 3 ms | 6612 KB | Output is correct |
6 | Correct | 3 ms | 6664 KB | Output is correct |
7 | Correct | 3 ms | 6612 KB | Output is correct |
8 | Correct | 3 ms | 6612 KB | Output is correct |
9 | Correct | 3 ms | 6612 KB | Output is correct |
10 | Correct | 3 ms | 6612 KB | Output is correct |
11 | Correct | 3 ms | 6596 KB | Output is correct |
12 | Correct | 4 ms | 6612 KB | Output is correct |
13 | Correct | 10 ms | 6740 KB | Output is correct |
14 | Correct | 11 ms | 6976 KB | Output is correct |
15 | Correct | 12 ms | 6844 KB | Output is correct |
16 | Correct | 10 ms | 6848 KB | Output is correct |
17 | Correct | 10 ms | 6740 KB | Output is correct |
18 | Correct | 10 ms | 6868 KB | Output is correct |
19 | Correct | 10 ms | 6852 KB | Output is correct |
20 | Correct | 10 ms | 6840 KB | Output is correct |
21 | Correct | 10 ms | 6864 KB | Output is correct |
22 | Correct | 10 ms | 6740 KB | Output is correct |
23 | Correct | 10 ms | 6868 KB | Output is correct |
24 | Correct | 8 ms | 6868 KB | Output is correct |
25 | Correct | 11 ms | 6964 KB | Output is correct |
26 | Correct | 9 ms | 6864 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 3 ms | 6612 KB | Output is correct |
2 | Execution timed out | 2073 ms | 22276 KB | Time limit exceeded |
3 | Halted | 0 ms | 0 KB | - |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 3 ms | 6612 KB | Output is correct |
2 | Correct | 3 ms | 6612 KB | Output is correct |
3 | Correct | 3 ms | 6612 KB | Output is correct |
4 | Correct | 3 ms | 6612 KB | Output is correct |
5 | Correct | 3 ms | 6612 KB | Output is correct |
6 | Correct | 3 ms | 6664 KB | Output is correct |
7 | Correct | 3 ms | 6612 KB | Output is correct |
8 | Correct | 3 ms | 6612 KB | Output is correct |
9 | Correct | 3 ms | 6612 KB | Output is correct |
10 | Correct | 3 ms | 6612 KB | Output is correct |
11 | Correct | 3 ms | 6596 KB | Output is correct |
12 | Correct | 3 ms | 6612 KB | Output is correct |
13 | Execution timed out | 2073 ms | 22276 KB | Time limit exceeded |
14 | Halted | 0 ms | 0 KB | - |