結果

問題 No.2677 Minmax Independent Set
ユーザー RubikunRubikun
提出日時 2024-03-15 22:21:37
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 184 ms / 2,000 ms
コード長 1,514 bytes
コンパイル時間 2,322 ms
コンパイル使用メモリ 204,984 KB
実行使用メモリ 37,824 KB
最終ジャッジ日時 2024-03-15 22:21:47
合計ジャッジ時間 9,470 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
12,224 KB
testcase_01 AC 6 ms
12,224 KB
testcase_02 AC 5 ms
12,224 KB
testcase_03 AC 5 ms
12,224 KB
testcase_04 AC 5 ms
12,224 KB
testcase_05 AC 136 ms
21,056 KB
testcase_06 AC 138 ms
21,184 KB
testcase_07 AC 140 ms
21,440 KB
testcase_08 AC 163 ms
21,824 KB
testcase_09 AC 139 ms
22,720 KB
testcase_10 AC 112 ms
19,620 KB
testcase_11 AC 106 ms
19,624 KB
testcase_12 AC 184 ms
37,824 KB
testcase_13 AC 168 ms
25,636 KB
testcase_14 AC 145 ms
25,608 KB
testcase_15 AC 175 ms
29,496 KB
testcase_16 AC 171 ms
19,392 KB
testcase_17 AC 183 ms
19,392 KB
testcase_18 AC 101 ms
16,960 KB
testcase_19 AC 128 ms
17,728 KB
testcase_20 AC 34 ms
14,272 KB
testcase_21 AC 138 ms
18,240 KB
testcase_22 AC 11 ms
12,736 KB
testcase_23 AC 5 ms
12,224 KB
testcase_24 AC 5 ms
12,224 KB
testcase_25 AC 5 ms
12,224 KB
testcase_26 AC 5 ms
12,224 KB
testcase_27 AC 6 ms
12,224 KB
testcase_28 AC 97 ms
19,684 KB
testcase_29 AC 163 ms
30,912 KB
testcase_30 AC 5 ms
12,224 KB
testcase_31 AC 6 ms
12,224 KB
testcase_32 AC 5 ms
12,224 KB
testcase_33 AC 5 ms
12,224 KB
testcase_34 AC 5 ms
12,224 KB
testcase_35 AC 5 ms
12,224 KB
testcase_36 AC 5 ms
12,224 KB
testcase_37 AC 5 ms
12,224 KB
testcase_38 AC 5 ms
12,224 KB
testcase_39 AC 6 ms
12,224 KB
testcase_40 AC 6 ms
12,352 KB
testcase_41 AC 7 ms
12,480 KB
testcase_42 AC 10 ms
12,736 KB
testcase_43 AC 18 ms
13,248 KB
testcase_44 AC 34 ms
14,144 KB
testcase_45 AC 76 ms
16,064 KB
testcase_46 AC 165 ms
19,136 KB
testcase_47 AC 171 ms
19,264 KB
testcase_48 AC 167 ms
19,264 KB
testcase_49 AC 164 ms
19,264 KB
testcase_50 AC 37 ms
15,216 KB
testcase_51 AC 7 ms
12,480 KB
testcase_52 AC 89 ms
18,404 KB
testcase_53 AC 83 ms
18,020 KB
testcase_54 AC 8 ms
12,352 KB
testcase_55 AC 122 ms
19,672 KB
testcase_56 AC 124 ms
19,608 KB
testcase_57 AC 125 ms
19,708 KB
testcase_58 AC 119 ms
19,676 KB
testcase_59 AC 120 ms
19,676 KB
testcase_60 AC 68 ms
19,264 KB
testcase_61 AC 71 ms
19,392 KB
testcase_62 AC 72 ms
22,848 KB
testcase_63 AC 77 ms
28,992 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=998244353,MAX=300005,INF=1<<30;

vector<int> G[MAX];
int dp[MAX][2];

int ans=INF;

void make(int u,int p){
    dp[u][0]=0;
    dp[u][1]=1;
    for(int to:G[u]){
        if(to==p) continue;
        make(to,u);
        dp[u][0]+=max(dp[to][0],dp[to][1]);
        dp[u][1]+=dp[to][0];
    }
}

void solve(int u,int p){
    dp[u][0]=0;
    dp[u][1]=1;
    for(int to:G[u]){
        dp[u][0]+=max(dp[to][0],dp[to][1]);
        dp[u][1]+=dp[to][0];
    }
    chmin(ans,dp[u][1]);
    
    for(int to:G[u]){
        if(to==p) continue;
        
        int a=dp[u][0],b=dp[u][1],c=dp[to][0],d=dp[to][1];
        
        dp[u][0]-=max(c,d);
        dp[u][1]-=c;
        
        solve(to,u);
        
        dp[u][0]=a;
        dp[u][1]=b;
        dp[to][0]=c;
        dp[to][1]=d;
    }
}

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    int N;cin>>N;
    for(int i=0;i<N-1;i++){
        int a,b;cin>>a>>b;a--;b--;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    make(0,-1);
    solve(0,-1);
    cout<<ans<<endl;
}

0