結果

問題 No.2677 Minmax Independent Set
ユーザー kokoseikokosei
提出日時 2024-03-16 00:03:19
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 294 ms / 2,000 ms
コード長 1,771 bytes
コンパイル時間 3,135 ms
コンパイル使用メモリ 252,624 KB
実行使用メモリ 70,656 KB
最終ジャッジ日時 2024-03-16 00:03:33
合計ジャッジ時間 13,271 ms
ジャッジサーバーID
(参考情報)
judge10 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 3 ms
6,676 KB
testcase_02 AC 3 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 3 ms
6,676 KB
testcase_05 AC 229 ms
32,640 KB
testcase_06 AC 234 ms
33,152 KB
testcase_07 AC 236 ms
33,664 KB
testcase_08 AC 235 ms
34,944 KB
testcase_09 AC 235 ms
37,120 KB
testcase_10 AC 194 ms
31,612 KB
testcase_11 AC 193 ms
31,616 KB
testcase_12 AC 260 ms
70,656 KB
testcase_13 AC 276 ms
44,284 KB
testcase_14 AC 252 ms
44,892 KB
testcase_15 AC 294 ms
53,008 KB
testcase_16 AC 262 ms
27,648 KB
testcase_17 AC 254 ms
27,648 KB
testcase_18 AC 150 ms
18,944 KB
testcase_19 AC 189 ms
21,760 KB
testcase_20 AC 55 ms
9,984 KB
testcase_21 AC 208 ms
23,680 KB
testcase_22 AC 12 ms
6,676 KB
testcase_23 AC 3 ms
6,676 KB
testcase_24 AC 3 ms
6,676 KB
testcase_25 AC 3 ms
6,676 KB
testcase_26 AC 3 ms
6,676 KB
testcase_27 AC 3 ms
6,676 KB
testcase_28 AC 179 ms
32,188 KB
testcase_29 AC 269 ms
54,656 KB
testcase_30 AC 3 ms
6,676 KB
testcase_31 AC 2 ms
6,676 KB
testcase_32 AC 2 ms
6,676 KB
testcase_33 AC 3 ms
6,676 KB
testcase_34 AC 3 ms
6,676 KB
testcase_35 AC 3 ms
6,676 KB
testcase_36 AC 4 ms
6,676 KB
testcase_37 AC 3 ms
6,676 KB
testcase_38 AC 3 ms
6,676 KB
testcase_39 AC 4 ms
6,676 KB
testcase_40 AC 4 ms
6,676 KB
testcase_41 AC 7 ms
6,676 KB
testcase_42 AC 10 ms
6,676 KB
testcase_43 AC 20 ms
6,676 KB
testcase_44 AC 45 ms
9,472 KB
testcase_45 AC 105 ms
15,360 KB
testcase_46 AC 234 ms
27,136 KB
testcase_47 AC 236 ms
27,392 KB
testcase_48 AC 242 ms
27,392 KB
testcase_49 AC 254 ms
27,520 KB
testcase_50 AC 62 ms
14,172 KB
testcase_51 AC 6 ms
6,676 KB
testcase_52 AC 152 ms
27,164 KB
testcase_53 AC 138 ms
25,320 KB
testcase_54 AC 5 ms
6,676 KB
testcase_55 AC 218 ms
31,536 KB
testcase_56 AC 226 ms
31,468 KB
testcase_57 AC 230 ms
31,828 KB
testcase_58 AC 220 ms
31,800 KB
testcase_59 AC 237 ms
31,540 KB
testcase_60 AC 101 ms
27,648 KB
testcase_61 AC 109 ms
28,160 KB
testcase_62 AC 115 ms
37,504 KB
testcase_63 AC 126 ms
51,968 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int N;
vector<int> G[202020];
vector<pair<int, int>> dp[202020];
pair<int, int> ans[202020];
pair<int, int> add(pair<int, int> a, pair<int, int> b){
    a.first += b.second;
    a.second += max(b.first, b.second);
    return a;
}
pair<int, int> unit(pair<int, int> a, pair<int, int> b){
    a.first += b.first;
    a.second += b.second;
    return a;
}
pair<int, int> addroot(pair<int, int> p){
    p.first += 1;
    return p;
}
pair<int, int> dfs0(int v, int p){
    pair<int, int> res = {0, 0};
    int sz = G[v].size();
    dp[v].resize(sz);
    for(int i = 0;i < sz;i++){
        int nv = G[v][i];
        if(nv == p)continue;
        dp[v][i] = dfs0(nv, v);
        res = add(res, dp[v][i]);
    }
    return addroot(res);
}
void dfs(int v, pair<int, int> pre, int p){
    int sz = G[v].size();
    for(int i = 0;i < sz;i++){
        if(G[v][i] == p)dp[v][i] = pre;
    }
    vector<pair<int, int>> dpl(sz + 1, {0, 0}), dpr(sz + 1, {0, 0});
    for(int i = 0;i < sz;i++){
        dpl[i + 1] = add(dpl[i], dp[v][i]);
    }
    for(int i = sz - 1;i >= 0;i--){
        dpr[i] = add(dpr[i + 1], dp[v][i]);
    }
    ans[v] = addroot(dpl[sz]);
    for(int i = 0;i < sz;i++){
        int nv = G[v][i];
        if(nv == p)continue;
        dfs(nv, addroot(unit(dpl[i], dpr[i + 1])), v);
    }
}
int main(void){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    cin >> N;
    for(int i = 0;i < N - 1;i++){
        int u, v; cin >> u >> v;
        u--, v--;
        G[u].push_back(v);
        G[v].push_back(u);
    }
    dfs0(0, -1);
    dfs(0, {0, 0}, -1);
    int all = 1e9;
    for(int i = 0;i < N;i++){
        all = min(all, ans[i].first);
    }
    cout << all << endl;
    return 0;
}
0