結果

問題 No.763 Noelちゃんと木遊び
ユーザー bonKotsubonKotsu
提出日時 2022-07-16 11:35:48
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 73 ms / 2,000 ms
コード長 940 bytes
コンパイル時間 2,014 ms
コンパイル使用メモリ 201,132 KB
実行使用メモリ 17,432 KB
最終ジャッジ日時 2023-09-10 19:31:18
合計ジャッジ時間 5,411 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
17,432 KB
testcase_01 AC 27 ms
7,052 KB
testcase_02 AC 58 ms
8,828 KB
testcase_03 AC 40 ms
7,844 KB
testcase_04 AC 34 ms
7,240 KB
testcase_05 AC 40 ms
7,912 KB
testcase_06 AC 73 ms
9,496 KB
testcase_07 AC 69 ms
9,352 KB
testcase_08 AC 41 ms
8,008 KB
testcase_09 AC 29 ms
7,208 KB
testcase_10 AC 13 ms
6,596 KB
testcase_11 AC 73 ms
9,560 KB
testcase_12 AC 64 ms
9,192 KB
testcase_13 AC 64 ms
9,136 KB
testcase_14 AC 57 ms
8,764 KB
testcase_15 AC 39 ms
7,928 KB
testcase_16 AC 9 ms
6,136 KB
testcase_17 AC 40 ms
7,868 KB
testcase_18 AC 73 ms
9,492 KB
testcase_19 AC 65 ms
9,172 KB
testcase_20 AC 66 ms
9,168 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)
#define P pair<int, int>
#define LP pair<ll, ll>
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define all(s) s.begin(), s.end()
#define rall(s) s.rbegin(), s.rend()
template<class T>
void chmax(T& a, T b) { a = max(a, b); };
template<class T>
void chmin(T& a, T b) { a = min(a, b); };
int n;
vector<int> g[100005];
int dp[100005][2];

void dfs(int v, int p = -1) {
  dp[v][1] = 1;
  int tmp0 = 0, tmp1 = 0;
  for (int nv : g[v]) {
    if (nv == p) continue;
    dfs(nv,v);
    tmp0 += max(dp[nv][0],dp[nv][1]);
    tmp1 += max(dp[nv][0], dp[nv][1]-1);
  }
  dp[v][0] += tmp0;
  dp[v][1] += tmp1;
}

int main() {
  cin >> n;
  rep(i,n-1) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    g[u].pb(v);
    g[v].pb(u);
  }

  dfs(0);
  cout << max(dp[0][0], dp[0][1]) << endl;
  return 0;
}
0