結果

問題 No.2677 Minmax Independent Set
ユーザー maeshunmaeshun
提出日時 2024-03-15 23:50:56
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,265 bytes
コンパイル時間 5,307 ms
コンパイル使用メモリ 265,576 KB
実行使用メモリ 116,692 KB
最終ジャッジ日時 2024-03-15 23:51:58
合計ジャッジ時間 20,584 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 21 ms
19,924 KB
testcase_01 AC 21 ms
19,924 KB
testcase_02 AC 21 ms
19,924 KB
testcase_03 AC 21 ms
19,924 KB
testcase_04 AC 20 ms
19,924 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 323 ms
48,812 KB
testcase_29 WA -
testcase_30 AC 20 ms
19,924 KB
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 AC 136 ms
30,820 KB
testcase_51 AC 28 ms
20,820 KB
testcase_52 AC 270 ms
43,804 KB
testcase_53 AC 258 ms
42,024 KB
testcase_54 AC 26 ms
20,564 KB
testcase_55 AC 362 ms
44,136 KB
testcase_56 WA -
testcase_57 AC 368 ms
46,076 KB
testcase_58 AC 366 ms
46,152 KB
testcase_59 WA -
testcase_60 WA -
testcase_61 WA -
testcase_62 WA -
testcase_63 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#include <atcoder/all>
using namespace atcoder;
#define rep(i, n) for(int i=0;i<(n);++i)
#define rep1(i, n) for(int i=1;i<=(n);i++)
#define ll long long
using mint = modint;
using P = pair<ll,ll>;
using lb = long double;
#ifdef LOCAL
#  include <debug_print.hpp>
#  define dbg(...) debug_print::multi_print(#__VA_ARGS__, __VA_ARGS__)
#else
#  define dbg(...) (static_cast<void>(0))
#endif

int n;
vector<vector<int>> dp(2e5+5, vector<int>(2));
vector<int> ans(2e5+5);
vector<vector<int>> g(2e5+5);
int m;


//0 白 1 黒
vector<int> dfs(int u, int p=-1) {
    dp[u][0] = 0;
    dp[u][1] = 1;
    for(int v : g[u]){
        if(v==p) continue;
        dfs(v,u);
        dp[u][0] += max(dp[v][0], dp[v][1]);
        dp[u][1] += dp[v][0];
    }
    return dp[u];
};

void dfs2(int u, int p=-1){
    int N = g[u].size();
    if(N==0) return;
    vector<vector<int>> l(N, vector<int>(2));
    vector<vector<int>> r(N, vector<int>(2));
    ans[u] = 1;
    rep(i,N){
        ans[u] += dp[g[u][i]][0];
    }
    l[0][0] = dp[g[u][0]][0];
    l[0][1] = max(dp[g[u][0]][0],dp[g[u][0]][1]); 
    r[0][0] = dp[g[u][N-1]][0];
    r[0][1] = max(dp[g[u][N-1]][0],dp[g[u][N-1]][1]); 
    rep(i,N){
        int v = g[u][i];
        if(i-1>=0) {
            l[i][0] = l[i-1][0]+dp[v][0];
            l[i][1] = l[i-1][1]+max(dp[v][0],dp[v][1]);
        }
    }
    for(int i=N-1;i>=0;i--){
        int v = g[u][i];
        if(i+1<N) {
            r[i][0] = r[i+1][0]+dp[v][0];
            r[i][1] = r[i+1][1]+max(dp[v][0],dp[v][1]);
        }
    }
    rep(i,N){
        int v = g[u][i];
        if(v==p) continue;
        dp[u][0] = 0;
        dp[u][1] = 1;
        if(i-1>=0) {
            dp[u][1] += l[i-1][0];
            dp[u][0] += max(l[i-1][1],l[i-1][0]);
        }
        if(i+1<N) {
            dp[u][1] += r[i+1][0];
            dp[u][0] += max(r[i+1][1],r[i+1][0]);
        }
        dfs2(v, u);
    }
};

int main()
{
    cin >> n;
    if(n==1){
        cout<<1<<endl;
        return 0;
    }
    rep(i,n-1){
        int u, v;
        cin >> u >> v;
        --u;--v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    dfs(0);
    dfs2(0);
    cout<<*min_element(ans.begin(),ans.begin()+n)<<endl;
    return 0;
}
0