結果

問題 No.763 Noelちゃんと木遊び
ユーザー kura197kura197
提出日時 2021-11-04 21:45:10
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 92 ms / 2,000 ms
コード長 1,436 bytes
コンパイル時間 2,259 ms
コンパイル使用メモリ 211,236 KB
実行使用メモリ 29,824 KB
最終ジャッジ日時 2024-04-23 04:37:33
合計ジャッジ時間 4,890 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 83 ms
29,824 KB
testcase_01 AC 29 ms
8,576 KB
testcase_02 AC 69 ms
12,288 KB
testcase_03 AC 46 ms
10,112 KB
testcase_04 AC 33 ms
8,924 KB
testcase_05 AC 44 ms
9,984 KB
testcase_06 AC 86 ms
13,824 KB
testcase_07 AC 82 ms
13,408 KB
testcase_08 AC 51 ms
10,368 KB
testcase_09 AC 36 ms
8,704 KB
testcase_10 AC 15 ms
7,040 KB
testcase_11 AC 92 ms
14,080 KB
testcase_12 AC 79 ms
13,056 KB
testcase_13 AC 72 ms
12,800 KB
testcase_14 AC 65 ms
12,032 KB
testcase_15 AC 44 ms
10,112 KB
testcase_16 AC 10 ms
6,604 KB
testcase_17 AC 47 ms
10,112 KB
testcase_18 AC 86 ms
13,896 KB
testcase_19 AC 83 ms
13,056 KB
testcase_20 AC 82 ms
13,184 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:18:26: warning: use of 'auto' in parameter declaration only available with '-std=c++20' or '-fconcepts'
   18 | void dfs(int v, int par, auto& dp){
      |                          ^~~~

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
#define REP(i, n) for(int i=0; i<n; i++)
#define REPi(i, a, b) for(int i=int(a); i<int(b); i++)
#define MEMS(a,b) memset(a,b,sizeof(a))
#define mp make_pair
#define MOD(a, m) ((a % m + m) % m)
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
const ll MOD = 1e9+7;

vector<int> G[100100];

void dfs(int v, int par, auto& dp){
    dp[v][1] = 1;
    dp[v][0] = 0;

    using T = tuple<ll, ll, ll>;
    vector<T> tmp;
    for(auto& nv : G[v]){
        if(nv == par) continue;
        dfs(nv, v, dp);
        dp[v][0] += max(dp[nv][0], dp[nv][1]);
        dp[v][1] += dp[nv][0];
        tmp.push_back(T(dp[nv][1] - dp[nv][0], dp[nv][0], dp[nv][1]));
    }

    if((int)tmp.size() > 0){
        sort(tmp.rbegin(), tmp.rend());
        ll val = 0;
        val += get<2>(tmp[0]);
        REPi(i,1,(int)tmp.size()){
            val += get<1>(tmp[i]);
        }
        chmax(dp[v][1], val);
    }
}

int main(){
    ll N;
    cin >> N;
    REP(i,N-1){
        ll u, v;
        cin >> u >> v;
        u--, v--;
        G[u].push_back(v);
        G[v].push_back(u);
    }

    vector<vector<ll>> dp(N, vector<ll>(2));
    dfs(0, -1, dp);

    ll ans = max(dp[0][0], dp[0][1]);
    cout << ans << endl;
    return 0;
}
0