結果

問題 No.2677 Minmax Independent Set
ユーザー Rubikun
提出日時 2024-03-15 22:21:37
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 179 ms / 2,000 ms
コード長 1,514 bytes
コンパイル時間 2,117 ms
コンパイル使用メモリ 196,892 KB
最終ジャッジ日時 2025-02-20 05:27:01
ジャッジサーバーID
(参考情報)
judge4 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 61
権限があれば一括ダウンロードができます

ソースコード

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