結果

問題 No.763 Noelちゃんと木遊び
ユーザー tottoripapertottoripaper
提出日時 2018-12-19 22:38:37
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,165 bytes
コンパイル時間 1,760 ms
コンパイル使用メモリ 169,328 KB
実行使用メモリ 26,060 KB
最終ジャッジ日時 2023-10-26 00:08:41
合計ジャッジ時間 9,310 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
権限があれば一括ダウンロードができます

ソースコード

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(v > N){
        return 0;
    }

    // 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