結果

問題 No.763 Noelちゃんと木遊び
ユーザー tottoripaper
提出日時 2018-12-19 22:40:42
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 69 ms / 2,000 ms
コード長 1,185 bytes
コンパイル時間 1,485 ms
コンパイル使用メモリ 163,864 KB
実行使用メモリ 17,608 KB
最終ジャッジ日時 2025-03-22 10:35:00
合計ジャッジ時間 3,432 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define fst(t) std::get<0>(t)
#define snd(t) std::get<1>(t)
#define thd(t) std::get<2>(t)
#define unless(p) if(!(p))
#define until(p) while(!(p))

using ll = long long;
using P = std::tuple<int,int>;

const int dx[8] = {-1, 1, 0, 0, -1, -1, 1, 1}, dy[8] = {0, 0, -1, 1, -1, 1, -1, 1};

int N;
std::vector<int> G[100100];
int dp[100100][2];

int rec(int v, int p, int f){
    if(dp[v][f] != -1){
        return dp[v][f];
    }
    
    // v を残す
    int n = !f;
    for(int w : G[v]){
        if(w == p){
            continue;
        }
        
        n += rec(w, v, 1);
    }

    // v を取り去る
    int m = 0;
    for(int w : G[v]){
        if(w == p){
            continue;
        }

        m += rec(w, v, 0);
    }

    int res = std::max(n, m);
    return dp[v][f] = res;
}

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

    std::cin >> N;

    for(int i=0;i<N-1;++i){
        int u, v;
        std::cin >> u >> v;

        G[u].emplace_back(v);
        G[v].emplace_back(u);
    }

    memset(dp, -1, sizeof(dp));
    
    int res = rec(1, -1, 0);
    std::cout << res << std::endl;
}
0