Submission #605202

# Submission time Handle Problem Language Result Execution time Memory
605202 2022-07-25T13:50:16 Z jhnah917 Designated Cities (JOI19_designated_cities) C++14
23 / 100
2000 ms 18480 KB
#pragma GCC optimize ("O3")
#pragma GCC target ("avx2")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int N, Q;
ll Sum, CostTo1, C[202020], R[202020];
vector<pair<int,int>> 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);
}

inline ll CostToRoot(int root){ return CostTo1 + C[root]; }

pair<ll,int> V[202020]; int sz, st;
ll DFS(int v, int b=-1){
    ll mx = 0;
    for(auto [i,w] : G[v]){
        if(i == b || U[i]) continue;
        ll nxt = DFS(i, v) + w;
        if(nxt > mx) swap(nxt, mx);
        if(nxt > 0) V[sz++] = {nxt, st};
    }
    return mx;
}
void CostFromRoot(int root){
    sz = 0;
    for(auto [i,w] : G[root]) if(!U[i]) V[sz++] = {DFS(st=i, -1) + w, i};
}

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;
    while(true){
        int nxt = -1;
        for(auto [i,w] : G[v]) if(i != b && !U[i] && S[i]*2 > tot) { nxt = i; break; }
        if(nxt == -1) break;
        else b = v, v = nxt;
    }
    return v;
}

int GetAnswer(int v){
    v = GetCent(v, GetSize(v)); U[v] = 1;
    CostFromRoot(v);
    sort(V, V+sz, greater<>());

    ll now = CostToRoot(v);
    for(int k=2; k<=N; k++){
        if(k-2 < sz) now += V[k-2].first;
        R[k] = min(R[k], Sum - now);
    }

    int mx2 = -1;
    for(int i=1; i<sz; i++) if(V[i].second != V[0].second) { mx2 = i; break; }
    if(mx2 != -1){
        now = CostToRoot(v) + V[mx2].first;
        for(int k=2; k<=N; k++){
            int idx = k-2 < mx2 ? k-2 : k-1;
            if(idx < sz) now += V[idx].first;
            R[k] = min(R[k], Sum - now);
        }
    }

    return v;
}

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);

    queue<int> que; que.push(1);
    while(!que.empty()){
        int v = que.front(); que.pop();
        v = GetAnswer(v); U[v] = 1;
        for(auto [i,w] : G[v]) if(!U[i]) que.push(i);
    }

    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:13:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   13 |     for(auto [i,w] : G[v]) if(i == b) CostTo1 += w, up += w;
      |              ^
designated_cities.cpp:15:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   15 |     for(auto [i,w] : G[v]) if(i != b) TreeDP(i, v, up, dw+w);
      |              ^
designated_cities.cpp: In function 'll DFS(int, int)':
designated_cities.cpp:23:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   23 |     for(auto [i,w] : G[v]){
      |              ^
designated_cities.cpp: In function 'void CostFromRoot(int)':
designated_cities.cpp:33:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   33 |     for(auto [i,w] : G[root]) if(!U[i]) V[sz++] = {DFS(st=i, -1) + w, i};
      |              ^
designated_cities.cpp: In function 'int GetSize(int, int)':
designated_cities.cpp:38:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   38 |     for(auto [i,w] : G[v]) if(i != b && !U[i]) S[v] += GetSize(i, v);
      |              ^
designated_cities.cpp: In function 'int GetCent(int, int)':
designated_cities.cpp:46:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   46 |         for(auto [i,w] : G[v]) if(i != b && !U[i] && S[i]*2 > tot) { nxt = i; break; }
      |                  ^
designated_cities.cpp: In function 'int main()':
designated_cities.cpp:94:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   94 |         for(auto [i,w] : G[v]) if(!U[i]) que.push(i);
      |                  ^
# Verdict Execution time Memory Grader output
1 Correct 5 ms 6612 KB Output is correct
2 Correct 4 ms 6612 KB Output is correct
3 Correct 6 ms 6612 KB Output is correct
4 Correct 4 ms 6612 KB Output is correct
5 Correct 5 ms 6612 KB Output is correct
6 Correct 4 ms 6616 KB Output is correct
7 Correct 4 ms 6740 KB Output is correct
8 Correct 4 ms 6612 KB Output is correct
9 Correct 4 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 4 ms 6612 KB Output is correct
2 Execution timed out 2088 ms 18480 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 2094 ms 18368 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 6612 KB Output is correct
2 Correct 4 ms 6612 KB Output is correct
3 Correct 6 ms 6612 KB Output is correct
4 Correct 4 ms 6612 KB Output is correct
5 Correct 5 ms 6612 KB Output is correct
6 Correct 4 ms 6616 KB Output is correct
7 Correct 4 ms 6740 KB Output is correct
8 Correct 4 ms 6612 KB Output is correct
9 Correct 4 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 4 ms 6612 KB Output is correct
13 Correct 13 ms 6740 KB Output is correct
14 Correct 15 ms 6816 KB Output is correct
15 Correct 12 ms 6796 KB Output is correct
16 Correct 13 ms 6796 KB Output is correct
17 Correct 18 ms 6800 KB Output is correct
18 Correct 13 ms 6752 KB Output is correct
19 Correct 14 ms 6740 KB Output is correct
20 Correct 13 ms 6800 KB Output is correct
21 Correct 20 ms 6800 KB Output is correct
22 Correct 19 ms 6740 KB Output is correct
23 Correct 13 ms 6780 KB Output is correct
24 Correct 15 ms 6740 KB Output is correct
25 Correct 17 ms 6740 KB Output is correct
26 Correct 9 ms 6740 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 6612 KB Output is correct
2 Execution timed out 2088 ms 18480 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 6612 KB Output is correct
2 Correct 4 ms 6612 KB Output is correct
3 Correct 6 ms 6612 KB Output is correct
4 Correct 4 ms 6612 KB Output is correct
5 Correct 5 ms 6612 KB Output is correct
6 Correct 4 ms 6616 KB Output is correct
7 Correct 4 ms 6740 KB Output is correct
8 Correct 4 ms 6612 KB Output is correct
9 Correct 4 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 4 ms 6612 KB Output is correct
13 Execution timed out 2088 ms 18480 KB Time limit exceeded
14 Halted 0 ms 0 KB -