結果

問題 No.763 Noelちゃんと木遊び
ユーザー ありあり
提出日時 2023-02-24 15:46:11
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 101 ms / 2,000 ms
コード長 2,027 bytes
コンパイル時間 1,304 ms
コンパイル使用メモリ 87,712 KB
実行使用メモリ 18,876 KB
最終ジャッジ日時 2023-10-10 00:38:53
合計ジャッジ時間 4,633 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 77 ms
18,876 KB
testcase_01 AC 30 ms
5,292 KB
testcase_02 AC 77 ms
8,152 KB
testcase_03 AC 51 ms
6,688 KB
testcase_04 AC 33 ms
5,720 KB
testcase_05 AC 49 ms
6,536 KB
testcase_06 AC 95 ms
9,184 KB
testcase_07 AC 92 ms
9,020 KB
testcase_08 AC 53 ms
6,640 KB
testcase_09 AC 33 ms
5,344 KB
testcase_10 AC 12 ms
4,352 KB
testcase_11 AC 100 ms
9,480 KB
testcase_12 AC 88 ms
8,664 KB
testcase_13 AC 85 ms
8,368 KB
testcase_14 AC 75 ms
7,900 KB
testcase_15 AC 50 ms
6,308 KB
testcase_16 AC 8 ms
4,352 KB
testcase_17 AC 50 ms
6,320 KB
testcase_18 AC 101 ms
9,256 KB
testcase_19 AC 92 ms
8,740 KB
testcase_20 AC 87 ms
8,664 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <algorithm>
#include <numeric>
#include <queue>
#include <cassert>
#include <cmath>
#include <bitset>
using namespace std;

const int mod = 1000000007;
struct mint {
  typedef long long ll;
  ll x;
  mint(ll x=0):x((x%mod+mod)%mod){}
  mint operator-() const { return mint(-x);}
  mint& operator+=(const mint a) {
    if ((x += a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator-=(const mint a) {
    if ((x += mod-a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;}
  mint operator+(const mint a) const { return mint(*this) += a;}
  mint operator-(const mint a) const { return mint(*this) -= a;}
  mint operator*(const mint a) const { return mint(*this) *= a;}
  mint pow(ll t) const {
    if (!t) return 1;
    mint a = pow(t>>1);
    a *= a;
    if (t&1) a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod-2);}
  mint& operator/=(const mint a) { return *this *= a.inv();}
  mint operator/(const mint a) const { return mint(*this) /= a;}
};
istream& operator>>(istream& is, mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}

int n;
vector<vector<int>> g;
int dp[100010][2];
bool done[100010][2];

int dfs(int v, int c, int p) {
  //printf("dfs(%d, %d, %d)\n", v+1, c, p+1);
  if (done[v][c]) return dp[v][c];
  done[v][c] = true;

  if (c == 0) {
    int res = 0;
    for (int u : g[v]) if (u != p) res += max(dfs(u, 1, v), dfs(u, 0, v));
    return dp[v][c] = res;
  }
  else {
    int res = 1;
    for (int u : g[v]) if (u != p) res += max(dfs(u, 1, v) -1, dfs(u, 0, v));
    return dp[v][c] = res;
  }
}

int main() {
  cin >> n;
  g.resize(n);
  for (int i = 0; i < n-1; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }

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