結果

問題 No.2677 Minmax Independent Set
ユーザー GOTKAKOGOTKAKO
提出日時 2024-03-15 22:50:50
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 405 ms / 2,000 ms
コード長 1,742 bytes
コンパイル時間 2,277 ms
コンパイル使用メモリ 210,988 KB
実行使用メモリ 133,872 KB
最終ジャッジ日時 2024-03-15 22:51:09
合計ジャッジ時間 14,578 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 314 ms
46,704 KB
testcase_06 AC 348 ms
47,856 KB
testcase_07 AC 317 ms
48,880 KB
testcase_08 AC 320 ms
51,184 KB
testcase_09 AC 323 ms
55,408 KB
testcase_10 AC 253 ms
41,792 KB
testcase_11 AC 250 ms
41,792 KB
testcase_12 AC 378 ms
133,872 KB
testcase_13 AC 387 ms
72,328 KB
testcase_14 AC 347 ms
72,716 KB
testcase_15 AC 405 ms
92,256 KB
testcase_16 AC 329 ms
37,580 KB
testcase_17 AC 351 ms
37,580 KB
testcase_18 AC 195 ms
25,268 KB
testcase_19 AC 234 ms
29,204 KB
testcase_20 AC 66 ms
12,544 KB
testcase_21 AC 260 ms
32,032 KB
testcase_22 AC 15 ms
6,676 KB
testcase_23 AC 2 ms
6,676 KB
testcase_24 AC 2 ms
6,676 KB
testcase_25 AC 2 ms
6,676 KB
testcase_26 AC 2 ms
6,676 KB
testcase_27 AC 3 ms
6,676 KB
testcase_28 AC 247 ms
42,384 KB
testcase_29 AC 403 ms
98,260 KB
testcase_30 AC 2 ms
6,676 KB
testcase_31 AC 2 ms
6,676 KB
testcase_32 AC 2 ms
6,676 KB
testcase_33 AC 2 ms
6,676 KB
testcase_34 AC 2 ms
6,676 KB
testcase_35 AC 2 ms
6,676 KB
testcase_36 AC 2 ms
6,676 KB
testcase_37 AC 3 ms
6,676 KB
testcase_38 AC 2 ms
6,676 KB
testcase_39 AC 3 ms
6,676 KB
testcase_40 AC 5 ms
6,676 KB
testcase_41 AC 7 ms
6,676 KB
testcase_42 AC 14 ms
6,676 KB
testcase_43 AC 28 ms
7,680 KB
testcase_44 AC 62 ms
12,032 KB
testcase_45 AC 151 ms
20,352 KB
testcase_46 AC 327 ms
37,188 KB
testcase_47 AC 322 ms
37,420 KB
testcase_48 AC 315 ms
37,420 KB
testcase_49 AC 317 ms
37,804 KB
testcase_50 AC 67 ms
18,004 KB
testcase_51 AC 6 ms
6,676 KB
testcase_52 AC 228 ms
35,680 KB
testcase_53 AC 175 ms
33,160 KB
testcase_54 AC 5 ms
6,676 KB
testcase_55 AC 297 ms
41,652 KB
testcase_56 AC 292 ms
41,772 KB
testcase_57 AC 303 ms
42,020 KB
testcase_58 AC 294 ms
42,032 KB
testcase_59 AC 313 ms
41,640 KB
testcase_60 AC 149 ms
37,756 KB
testcase_61 AC 149 ms
38,420 KB
testcase_62 AC 165 ms
56,304 KB
testcase_63 AC 193 ms
86,904 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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

    int N; cin >> N;
    vector<vector<int>> Graph(N);
    for(int i=0; i<N-1; i++){
        int u,v; cin >> u >> v;
        u--; v--;
        Graph.at(u).push_back(v);
        Graph.at(v).push_back(u);
    }
    if(N == 1){cout << 1 << endl; return 0;}

    vector<vector<int>> ko1(N),ko2(N);
    vector<int> par(N,-1);
auto d1 = [&](auto d1,int pos,int back) -> pair<int,int> {
    int w = 0,b = 1;
    int siz = Graph.at(pos).size();
    for(int i=0; i<siz; i++){
        int to = Graph.at(pos).at(i);
        if(to == back){par.at(pos) = i; ko1.at(pos).push_back(0),ko2.at(pos).push_back(0); continue;}

        auto[kow,kob] = d1(d1,to,pos);
        ko1.at(pos).push_back(max(kow,kob)),ko2.at(pos).push_back(kow);
        w += max(kow,kob),b += kow;
    }
    return {w,b};
};
    d1(d1,0,-1);

    int answer = 1e9;
auto d2 = [&](auto d2,int pos,int back,int pw,int pb) -> void {
    if(back != -1) ko1.at(pos).at(par.at(pos)) = max(pb,pw), ko2.at(pos).at(par.at(pos)) = pw;
    int siz = Graph.at(pos).size();

    vector<int> l1 = ko1.at(pos),r1 = ko1.at(pos),l2 = ko2.at(pos),r2 = ko2.at(pos);
    for(int i=1; i<siz; i++) l1.at(i) += l1.at(i-1),l2.at(i) += l2.at(i-1);
    for(int i=siz-2; i>=0; i--) r1.at(i) += r1.at(i+1),r2.at(i) += r2.at(i+1);
    answer = min(answer,l2.back()+1);

    for(int i=0; i<siz; i++){
        int to = Graph.at(pos).at(i);
        if(to == back) continue;

        int gw = 0,gb = 1;
        if(i) gw += l1.at(i-1),gb += l2.at(i-1);
        if(i != siz-1) gw += r1.at(i+1),gb += r2.at(i+1);
        d2(d2,to,pos,gw,gb);
    }
};
    d2(d2,0,-1,0,0);
    cout << answer << endl;
}
0