結果

問題 No.763 Noelちゃんと木遊び
ユーザー utouto97utouto97
提出日時 2019-02-27 11:37:44
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 73 ms / 2,000 ms
コード長 1,316 bytes
コンパイル時間 1,109 ms
コンパイル使用メモリ 146,216 KB
実行使用メモリ 16,684 KB
最終ジャッジ日時 2023-09-05 09:11:13
合計ジャッジ時間 3,762 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
16,684 KB
testcase_01 AC 26 ms
7,288 KB
testcase_02 AC 58 ms
9,420 KB
testcase_03 AC 40 ms
8,232 KB
testcase_04 AC 29 ms
7,512 KB
testcase_05 AC 38 ms
8,216 KB
testcase_06 AC 69 ms
10,268 KB
testcase_07 AC 67 ms
10,236 KB
testcase_08 AC 41 ms
8,368 KB
testcase_09 AC 29 ms
7,512 KB
testcase_10 AC 13 ms
6,400 KB
testcase_11 AC 73 ms
10,384 KB
testcase_12 AC 62 ms
9,888 KB
testcase_13 AC 61 ms
9,704 KB
testcase_14 AC 55 ms
9,464 KB
testcase_15 AC 39 ms
8,192 KB
testcase_16 AC 8 ms
6,288 KB
testcase_17 AC 39 ms
8,476 KB
testcase_18 AC 72 ms
10,424 KB
testcase_19 AC 64 ms
10,136 KB
testcase_20 AC 64 ms
10,152 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define eprintf(...) 42
#endif

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define repi(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define all(x) (x).begin(),(x).end()
#define foreach(u,v) for(auto (u) : (v))
#define pb push_back
#define mp make_pair
#define mt make_tuple

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> pll;
typedef vector<ll> vl;

const int inf = 1e9;
const ll linf = 1LL<<60;
const ll mod = 1e9 + 7;
const double eps = 1e-9;

/*{
  dp[i][j]
    iを根とする部分木で
    j=0 iを削除しない
    j=1 iを削除する
    ときの連結成分の個数
}*/

vector<int> g[100000];
ll dp[100001][2];

void dfs(int x, int par = -1)
{
  dp[x][0] = 1;
  dp[x][1] = 0;

  for(auto to : g[x]){
    if(to == par) continue;

    dfs(to, x);
    dp[x][0] += dp[to][1];
    dp[x][1] += max(dp[to][0], dp[to][1]);
  }

//  cout << x << " : " << dp[x][0] << " , " << dp[x][1] << endl;
}

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

  rep(i, n-1){
    int x, y;
    cin >> x >> y;
    x--; y--;

    g[x].pb(y);
    g[y].pb(x);
  }

  dfs(0);

  ll ans = max(dp[0][0], dp[0][1]);

  cout << ans << endl;

  return 0;
}
0