結果

問題 No.763 Noelちゃんと木遊び
ユーザー tottoripapertottoripaper
提出日時 2018-12-19 22:40:42
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 50 ms / 2,000 ms
コード長 1,185 bytes
コンパイル時間 1,769 ms
コンパイル使用メモリ 169,724 KB
実行使用メモリ 17,684 KB
最終ジャッジ日時 2023-10-26 00:08:46
合計ジャッジ時間 3,648 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
17,684 KB
testcase_01 AC 16 ms
7,832 KB
testcase_02 AC 38 ms
9,256 KB
testcase_03 AC 24 ms
8,460 KB
testcase_04 AC 19 ms
7,980 KB
testcase_05 AC 23 ms
8,408 KB
testcase_06 AC 42 ms
9,784 KB
testcase_07 AC 41 ms
9,672 KB
testcase_08 AC 27 ms
8,540 KB
testcase_09 AC 18 ms
7,940 KB
testcase_10 AC 9 ms
7,232 KB
testcase_11 AC 42 ms
9,844 KB
testcase_12 AC 37 ms
9,496 KB
testcase_13 AC 37 ms
9,448 KB
testcase_14 AC 33 ms
9,160 KB
testcase_15 AC 24 ms
8,436 KB
testcase_16 AC 7 ms
7,052 KB
testcase_17 AC 24 ms
8,448 KB
testcase_18 AC 50 ms
9,800 KB
testcase_19 AC 42 ms
9,524 KB
testcase_20 AC 40 ms
9,536 KB
権限があれば一括ダウンロードができます

ソースコード

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