Submission #604978

# Submission time Handle Problem Language Result Execution time Memory
604978 2022-07-25T11:43:56 Z jhnah917 Designated Cities (JOI19_designated_cities) C++14
23 / 100
2000 ms 20456 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];

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];
}

priority_queue<ll> CostFromRoot(int root){
    priority_queue<ll> pq;
    function<ll(int,int)> dfs = [&](int v, int b) -> ll {
        vector<ll> ch;
        for(auto [i,w] : G[v]) if(i != b) ch.push_back(dfs(i, v) + w);
        if(ch.empty()) return 0LL;
        sort(ch.begin(), ch.end(), greater<>());
        for(int i=1; i<ch.size(); i++) pq.push(ch[i]);
        return ch[0];
    };
    pq.push(dfs(root, -1));
    return pq;
}

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);
    for(int i=1; i<=N; i++){
        if(G[i].size() > 1) continue;
        auto pq = CostFromRoot(i);
        ll res = CostToRoot(i);
        for(int k=2; k<=N; k++){
            if(!pq.empty()) res += pq.top(), pq.pop();
            R[k] = min(R[k], Sum - res);
        }
    }

    cin >> Q;
    for(int i=1; i<=Q; i++){
        int t; cin >> t;
        cout << R[t] << "\n";
    }
}

Compilation message

designated_cities.cpp: In function 'void TreeDP(int, int, ll, ll)':
designated_cities.cpp:9:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
    9 |     for(auto [i,w] : G[v]) if(i == b) CostTo1 += w, up += w;
      |              ^
designated_cities.cpp:11:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   11 |     for(auto [i,w] : G[v]) if(i != b) TreeDP(i, v, up, dw+w);
      |              ^
designated_cities.cpp: In lambda function:
designated_cities.cpp:22:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   22 |         for(auto [i,w] : G[v]) if(i != b) ch.push_back(dfs(i, v) + w);
      |                  ^
designated_cities.cpp:25:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int, std::allocator<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   25 |         for(int i=1; i<ch.size(); i++) pq.push(ch[i]);
      |                      ~^~~~~~~~~~
# Verdict Execution time Memory 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 6612 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 4 ms 6612 KB Output is correct
11 Correct 4 ms 6612 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6612 KB Output is correct
2 Execution timed out 2068 ms 20332 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 6612 KB Output is correct
2 Execution timed out 2081 ms 20456 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory 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 6612 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 4 ms 6612 KB Output is correct
11 Correct 4 ms 6612 KB Output is correct
12 Correct 3 ms 6612 KB Output is correct
13 Correct 156 ms 6904 KB Output is correct
14 Correct 5 ms 7012 KB Output is correct
15 Correct 140 ms 6844 KB Output is correct
16 Correct 151 ms 6844 KB Output is correct
17 Correct 155 ms 6752 KB Output is correct
18 Correct 152 ms 6860 KB Output is correct
19 Correct 154 ms 6840 KB Output is correct
20 Correct 161 ms 6856 KB Output is correct
21 Correct 146 ms 6888 KB Output is correct
22 Correct 120 ms 6860 KB Output is correct
23 Correct 193 ms 6868 KB Output is correct
24 Correct 368 ms 6872 KB Output is correct
25 Correct 33 ms 6996 KB Output is correct
26 Correct 434 ms 6868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6612 KB Output is correct
2 Execution timed out 2068 ms 20332 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory 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 6612 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 4 ms 6612 KB Output is correct
11 Correct 4 ms 6612 KB Output is correct
12 Correct 3 ms 6612 KB Output is correct
13 Execution timed out 2068 ms 20332 KB Time limit exceeded
14 Halted 0 ms 0 KB -