답안 #865046

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
865046 2023-10-24T03:51:05 Z LittleOrange Designated Cities (JOI19_designated_cities) C++17
13 / 100
198 ms 43344 KB
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
struct obj{
    ll to,c0,c1;// to, back
};
int main(){
    ios::sync_with_stdio(0);cin.tie(0);
    ll n,q;
    cin >> n;
    vector<vector<obj>> con(n);
    ll tot = 0;
    for(ll i = 1;i<n;i++){
        ll a,b,c,d;
        cin >> a >> b >> c >> d;a--;b--;
        con[a].push_back({b,c,d});
        con[b].push_back({a,d,c});
        tot += c+d;
    }
    ll leaf_cnt = 0;
    for(auto &o : con) if (o.size()==1) leaf_cnt++;
    cin >> q;
    if (n<=16){
        vector<ll> ans(n+1,tot);
        ll cur = tot;
        vector<vector<ll>> used(n,vector<ll>(n,0));
        function<void(ll,ll)> dfs;
        dfs = [&](ll i, ll p){
            for(obj l : con[i]){
                if (l.to!=p){
                    if (!used[i][l.to]){
                        used[i][l.to] = 1;
                        cur -= l.c1;
                    }
                    dfs(l.to,i);
                }
            }
        };
        for(ll x = 1;x<(1<<n);x++){
            cur = tot;
            for(ll i = 0;i<n;i++) for(ll j = 0;j<n;j++) used[i][j] = 0;
            ll lvl = __builtin_popcountll(x);
            for(ll i = 0;i<n;i++) if(x>>i&1) dfs(i,-1);
            ans[lvl] = min(ans[lvl],cur);
        }
        while(q--){
            ll e;
            cin >> e;
            cout << ans[e] << "\n";
        }
        return 0;
    }
    vector<ll> dp(n,0);
    {
        function<ll(ll,ll)> dfs;
        dfs = [&](ll i, ll p){
            ll cur = 0;
            for(obj l : con[i]){
                if (l.to!=p){
                    cur += l.c1 + dfs(l.to,i);
                }
            }
            return cur;
        };
        dp[0] = dfs(0,-1);
    }
    {
        function<void(ll,ll,ll)> dfs;
        dfs = [&](ll i, ll p, ll v){
            dp[i] = v;
            for(obj l : con[i]){
                if (l.to!=p){
                    dfs(l.to,i,v+l.c0-l.c1);
                }
            }
        };
        dfs(0,-1,dp[0]);
    }
    while(q--){
        ll e;
        cin >> e;
        if (e>=leaf_cnt){
            cout << "0\n";
            continue;
        }
        if (e==1){
            ll ans = 0;
            for(ll i : dp) ans = max(i,ans);
            ans = tot-ans;
            cout << ans << "\n";
            continue;
        }
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 69 ms 456 KB Output is correct
3 Correct 81 ms 592 KB Output is correct
4 Correct 73 ms 344 KB Output is correct
5 Correct 65 ms 348 KB Output is correct
6 Correct 68 ms 436 KB Output is correct
7 Correct 69 ms 432 KB Output is correct
8 Correct 70 ms 708 KB Output is correct
9 Correct 63 ms 344 KB Output is correct
10 Correct 75 ms 600 KB Output is correct
11 Correct 61 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 158 ms 24916 KB Output is correct
3 Correct 178 ms 42800 KB Output is correct
4 Correct 120 ms 23888 KB Output is correct
5 Correct 134 ms 24580 KB Output is correct
6 Correct 148 ms 27472 KB Output is correct
7 Correct 124 ms 24320 KB Output is correct
8 Correct 198 ms 43344 KB Output is correct
9 Correct 82 ms 24312 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Incorrect 150 ms 26140 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 69 ms 456 KB Output is correct
3 Correct 81 ms 592 KB Output is correct
4 Correct 73 ms 344 KB Output is correct
5 Correct 65 ms 348 KB Output is correct
6 Correct 68 ms 436 KB Output is correct
7 Correct 69 ms 432 KB Output is correct
8 Correct 70 ms 708 KB Output is correct
9 Correct 63 ms 344 KB Output is correct
10 Correct 75 ms 600 KB Output is correct
11 Correct 61 ms 344 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Incorrect 1 ms 724 KB Output isn't correct
14 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 158 ms 24916 KB Output is correct
3 Correct 178 ms 42800 KB Output is correct
4 Correct 120 ms 23888 KB Output is correct
5 Correct 134 ms 24580 KB Output is correct
6 Correct 148 ms 27472 KB Output is correct
7 Correct 124 ms 24320 KB Output is correct
8 Correct 198 ms 43344 KB Output is correct
9 Correct 82 ms 24312 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Incorrect 150 ms 26140 KB Output isn't correct
12 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 69 ms 456 KB Output is correct
3 Correct 81 ms 592 KB Output is correct
4 Correct 73 ms 344 KB Output is correct
5 Correct 65 ms 348 KB Output is correct
6 Correct 68 ms 436 KB Output is correct
7 Correct 69 ms 432 KB Output is correct
8 Correct 70 ms 708 KB Output is correct
9 Correct 63 ms 344 KB Output is correct
10 Correct 75 ms 600 KB Output is correct
11 Correct 61 ms 344 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 158 ms 24916 KB Output is correct
14 Correct 178 ms 42800 KB Output is correct
15 Correct 120 ms 23888 KB Output is correct
16 Correct 134 ms 24580 KB Output is correct
17 Correct 148 ms 27472 KB Output is correct
18 Correct 124 ms 24320 KB Output is correct
19 Correct 198 ms 43344 KB Output is correct
20 Correct 82 ms 24312 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Incorrect 150 ms 26140 KB Output isn't correct
23 Halted 0 ms 0 KB -