제출 #604966

#제출 시각아이디문제언어결과실행 시간메모리
604966jhnah917Designated Cities (JOI19_designated_cities)C++14
13 / 100
2076 ms27576 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

ll N, Q, Sum, CostTo1, Result1=1e18, C[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];
}

ll CostFromRoot(int root, int k){
    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];
    };
    ll res = dfs(root, -1);
    for(int i=2; i<k && !pq.empty(); i++) res += pq.top(), pq.pop();
    return res;
}

ll Solve(int K){
    if(K == 1) return Result1;
    ll res = 0;
    for(int i=1; i<=N; i++) if(G[i].size() == 1) res = max(res, CostToRoot(i) + CostFromRoot(i, K));
    return Sum - res;
}

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); Result1 = Sum - CostTo1 - *max_element(C+1, C+N+1);

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

컴파일 시 표준 에러 (stderr) 메시지

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 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...