結果

問題 No.763 Noelちゃんと木遊び
ユーザー outlineoutline
提出日時 2020-12-24 19:58:18
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 74 ms / 2,000 ms
コード長 1,717 bytes
コンパイル時間 1,580 ms
コンパイル使用メモリ 138,600 KB
実行使用メモリ 24,288 KB
最終ジャッジ日時 2023-10-21 15:48:20
合計ジャッジ時間 3,748 ms
ジャッジサーバーID
(参考情報)
judge14 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 49 ms
24,288 KB
testcase_01 AC 21 ms
6,864 KB
testcase_02 AC 54 ms
11,880 KB
testcase_03 AC 32 ms
8,976 KB
testcase_04 AC 23 ms
7,392 KB
testcase_05 AC 33 ms
8,976 KB
testcase_06 AC 69 ms
13,464 KB
testcase_07 AC 70 ms
13,200 KB
testcase_08 AC 38 ms
9,240 KB
testcase_09 AC 23 ms
7,392 KB
testcase_10 AC 9 ms
4,752 KB
testcase_11 AC 74 ms
13,728 KB
testcase_12 AC 63 ms
12,672 KB
testcase_13 AC 64 ms
12,408 KB
testcase_14 AC 55 ms
11,352 KB
testcase_15 AC 34 ms
8,976 KB
testcase_16 AC 6 ms
4,348 KB
testcase_17 AC 35 ms
8,976 KB
testcase_18 AC 73 ms
13,728 KB
testcase_19 AC 66 ms
12,672 KB
testcase_20 AC 64 ms
12,672 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <array>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <cstring>
#include <unordered_map>
using namespace std;

using ll = long long;
using P = pair<int, int>;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

template<class T>
inline bool chmax(T& x, T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}
template<class T>
inline bool chmin(T& x, T y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}

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

    int n;
    cin >> n;
    vector<vector<int>> g(n);
    for(int i = 1; i < n; ++i){
        int u, v;
        cin >> u >> v;
        --u, --v;
        g[u].emplace_back(v);
        g[v].emplace_back(u);
    }

    vector<vector<int>> dp(n, vector<int>(2, -1));
    auto func = [&](auto&& self, int v, int i, int par = -1) -> int {
        if(dp[v][i] != -1)  return dp[v][i];
        int res = i;
        for(int ch : g[v]){
            if(ch == par)   continue;
            if(i)   res += max(self(self, ch, 0, v), self(self, ch, 1, v) - 1);
            else    res += max(self(self, ch, 0, v), self(self, ch, 1, v));
        }
        return dp[v][i] = res;
    };

    cout << max(func(func, 0, 0), func(func, 0, 1)) << endl;

    return 0;
}
0