結果

問題 No.763 Noelちゃんと木遊び
ユーザー face4face4
提出日時 2018-12-13 13:33:28
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 86 ms / 2,000 ms
コード長 914 bytes
コンパイル時間 588 ms
コンパイル使用メモリ 71,868 KB
実行使用メモリ 17,816 KB
最終ジャッジ日時 2023-10-25 09:19:56
合計ジャッジ時間 2,821 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
17,816 KB
testcase_01 AC 28 ms
7,444 KB
testcase_02 AC 67 ms
9,212 KB
testcase_03 AC 44 ms
8,224 KB
testcase_04 AC 32 ms
7,628 KB
testcase_05 AC 42 ms
8,156 KB
testcase_06 AC 80 ms
9,868 KB
testcase_07 AC 79 ms
9,728 KB
testcase_08 AC 45 ms
8,324 KB
testcase_09 AC 31 ms
7,576 KB
testcase_10 AC 13 ms
6,760 KB
testcase_11 AC 84 ms
9,940 KB
testcase_12 AC 73 ms
9,508 KB
testcase_13 AC 71 ms
9,448 KB
testcase_14 AC 63 ms
9,092 KB
testcase_15 AC 43 ms
8,192 KB
testcase_16 AC 9 ms
6,580 KB
testcase_17 AC 42 ms
8,208 KB
testcase_18 AC 86 ms
9,888 KB
testcase_19 AC 70 ms
9,544 KB
testcase_20 AC 72 ms
9,560 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<cstring>
#include<stack>
using namespace std;

int dp[100000][2] = {};
bool memo[100000][2] = {};
vector<int> path[100000];

int dfs(int cur, int par, int flag){
    if(memo[cur][flag]) return dp[cur][flag];

    memo[cur][flag] = true;

    int ret = flag;

    if(flag){
        for(int next : path[cur]){
            if(next == par) continue;
            ret += dfs(next, cur, 0);
        }
    }else{
        for(int next : path[cur]){
            if(next == par)  continue;
            ret += max(dfs(next, cur, 0), dfs(next, cur, 1));
        }
    }

    dp[cur][flag] = ret;

    return ret;
}

int main(){
    int n;
    cin >> n;

    int u, v;
    for(int i = 0; i < n-1; i++){
        cin >> u >> v;
        u--, v--;
        path[u].push_back(v);
        path[v].push_back(u);
    }

    cout << max(dfs(0, -1, 0) , dfs(0, -1, 1)) << endl;

    return 0;
}
0