답안 #1108365

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1108365 2024-11-04T01:54:40 Z Zero_OP Designated Cities (JOI19_designated_cities) C++14
7 / 100
166 ms 43516 KB
#include <bits/stdc++.h>

using namespace std;

#define all(v) begin(v), end(v)
#define compact(v) v.erase(unique(all(v)), end(v))
#define sz(v) (int)v.size()

template<typename T>
    bool minimize(T& a, const T& b){
        if(a > b){
            return a = b, true;
        }
        return false;
    }
template<typename T>
    bool maximize(T& a, const T& b){
        if(a < b){
            return a = b, true;
        }
        return false;
    }

using ll = long long;
using ld = long double;
using ull = unsigned long long;

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);

#ifdef LOCAL
    freopen("task.inp", "r", stdin);
    freopen("task.out", "w", stdout);
#endif // LOCAL

    int N;
    cin >> N;
    vector<vector<tuple<int, int, int>>> adj(N);
    for(int i = 0; i < N - 1; ++i){
        int u, v, c, d;
        cin >> u >> v >> c >> d;
        --u, --v;
        adj[u].emplace_back(v, c, d);
        adj[v].emplace_back(u, d, c);
    }

    vector<ll> f(N), g(N), up(N), down(N);

    function<void(int, int)> dfs = [&](int u, int p){
        for(auto [v, c, d] : adj[u]) if(v != p){
            down[v] = down[u] + c;
            up[v] = up[u] + d;
            dfs(v, u);
            f[u] += f[v] + c;
        }
    };

    dfs(0, -1);
    g[0] = f[0];

    function<void(int, int)> reroot_dfs = [&](int u, int p){
        for(auto [v, c, d] : adj[u]) if(v != p){
            g[v] = g[u] - c + d;
            reroot_dfs(v, u);
        }
    };

    reroot_dfs(0, -1);
    vector<long long> answer(N + 1, LLONG_MAX);

    for(int i = 0; i < N; ++i){
        minimize(answer[1], g[i]);
    }

    const long long inf = 1e18;
    vector<pair<long long, int>> lowest(N, {-inf, -1});
    tuple<long long, int, int> solution_for_two_leaves = {inf, -1, -1};

    for(int u = 0; u < N; ++u){
        if((int)adj[u].size() == 1){
            lowest[u] = {down[u], u};
        }
    }

    function<void(int, int)> dfs_dp = [&](int u, int p){
        pair<long long, int> A = lowest[u], B = {-inf, -1};

        for(auto [v, c, d] : adj[u]) if(v != p){
            dfs_dp(v, u);

            if(A < lowest[v]){
                B = A;
                A = lowest[v];
            } else maximize(B, lowest[v]);

            maximize(lowest[u], lowest[v]);
        }

        if(B.first != -inf){
            long long delta = 2 * down[u] - A.first - B.first;
            int u = A.second, v = B.second;

            minimize(solution_for_two_leaves, make_tuple(delta, u, v));
        }
    };

    dfs_dp(0, -1);

    long long delta;
    int l1, l2;
    tie(delta, l1, l2) = solution_for_two_leaves;
    answer[2] = f[0] + delta;

    int Q;
    cin >> Q;
    while(Q--){
        int k;
        cin >> k;
        cout << answer[k] << '\n';
    }

    return 0;
}

Compilation message

designated_cities.cpp: In lambda function:
designated_cities.cpp:51:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   51 |         for(auto [v, c, d] : adj[u]) if(v != p){
      |                  ^
designated_cities.cpp: In lambda function:
designated_cities.cpp:63:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   63 |         for(auto [v, c, d] : adj[u]) if(v != p){
      |                  ^
designated_cities.cpp: In lambda function:
designated_cities.cpp:89:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   89 |         for(auto [v, c, d] : adj[u]) if(v != p){
      |                  ^
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Incorrect 1 ms 336 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 116 ms 24364 KB Output is correct
3 Correct 155 ms 42688 KB Output is correct
4 Correct 127 ms 24136 KB Output is correct
5 Correct 166 ms 24052 KB Output is correct
6 Correct 135 ms 26952 KB Output is correct
7 Correct 99 ms 24060 KB Output is correct
8 Correct 164 ms 43516 KB Output is correct
9 Correct 83 ms 24488 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Incorrect 138 ms 30760 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Incorrect 1 ms 336 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 116 ms 24364 KB Output is correct
3 Correct 155 ms 42688 KB Output is correct
4 Correct 127 ms 24136 KB Output is correct
5 Correct 166 ms 24052 KB Output is correct
6 Correct 135 ms 26952 KB Output is correct
7 Correct 99 ms 24060 KB Output is correct
8 Correct 164 ms 43516 KB Output is correct
9 Correct 83 ms 24488 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Incorrect 138 ms 30760 KB Output isn't correct
12 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Incorrect 1 ms 336 KB Output isn't correct
3 Halted 0 ms 0 KB -