結果

問題 No.763 Noelちゃんと木遊び
ユーザー motakinemotakine
提出日時 2020-05-13 00:54:48
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 76 ms / 2,000 ms
コード長 2,285 bytes
コンパイル時間 1,957 ms
コンパイル使用メモリ 178,460 KB
実行使用メモリ 20,208 KB
最終ジャッジ日時 2023-10-12 05:00:29
合計ジャッジ時間 4,205 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
20,208 KB
testcase_01 AC 26 ms
5,116 KB
testcase_02 AC 61 ms
7,840 KB
testcase_03 AC 40 ms
6,316 KB
testcase_04 AC 29 ms
5,712 KB
testcase_05 AC 39 ms
6,156 KB
testcase_06 AC 73 ms
8,852 KB
testcase_07 AC 74 ms
8,324 KB
testcase_08 AC 43 ms
6,472 KB
testcase_09 AC 28 ms
5,232 KB
testcase_10 AC 11 ms
4,352 KB
testcase_11 AC 76 ms
8,764 KB
testcase_12 AC 67 ms
8,164 KB
testcase_13 AC 64 ms
8,196 KB
testcase_14 AC 58 ms
7,580 KB
testcase_15 AC 40 ms
6,248 KB
testcase_16 AC 7 ms
4,372 KB
testcase_17 AC 40 ms
6,156 KB
testcase_18 AC 73 ms
8,728 KB
testcase_19 AC 66 ms
8,012 KB
testcase_20 AC 69 ms
8,056 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, n) for (int i=0; i<(int)(n); i++)
#define all(v) v.begin(), v.end()
#define PRINT(v) for (auto x : (v)) cout <<x <<" " ; cout <<endl;
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using mat = vector<vector<ll>>;
const ll MOD = 1000000007;
const ll INF = 10000000000000000;
vector<int> x4 = {0, 1, 0, -1}, x8 = {0, 1, 1, 1, 0, -1, -1, -1};
vector<int> y4 = {1, 0, -1, 0}, y8 = {1, 1, 0, -1, -1, -1, 0, 1};
template<class T> inline bool chmin(T& a, T b){if (a>b){a = b; return true;}return false;}
template<class T> inline bool chmax(T& a, T b){if (a<b){a = b; return true;}return false;}
template<class T> inline T powerM(T a,T b){if (b==0) return 1;
T tmp = powerM(a,b/2); if (b%2==0) return tmp*tmp%MOD; else return tmp*tmp%MOD*a%MOD; }
template<class T> inline T power(T a,T b,T m){ if (b==0) return 1;
  T tmp = power(a,b/2,m); if (b%2==0) return tmp*tmp%m; else return tmp*tmp%m*a%m; }
template<class T> inline T gcd(T a, T b){if (b==0) return a; return gcd(b, a%b);}
template<class T> inline T lcm(T a, T b){return a / gcd(a,b) * b;}
// ax+by=gcd(a,b)を解く
template<class T> inline T extgcd(T a,T b,T &x,T &y){if (b==0){x=1; y=0; return a;} T d=extgcd(b,a%b,y,x); y -= a/b*x; return d;}
void hey(){ cout <<"hey" <<endl; }

template<class T> struct edge { int to; T cost;};


int N;
Graph G;
vector<int> dp;
vector<bool> del;

void dfs(int v, int p){
  // 自分の子のうち、一つでも削除されていないものがあれば自分を削除する
  // 自分の子が一つも残っていないかそもそも存在しないなら自分は削除しない
  int res = 0;
  for (int nv : G[v]){
    if (nv == p) continue;
    dfs(nv, v);
    if (!del[nv]) del[v] = true;
    res += dp[nv];
  }
  // 自分の子のうち残っているものがk個だとすると、
  // 自分を削除すればそれらが独立するけど足し算してたから関係ないね
  dp[v] = res + (!del[v]);
}

int main() {
  cin >>N;
  G.assign(N, vector<int>());
  rep(i, N-1){
    int x,y; cin >>x >>y; x--; y--;
    G[x].push_back(y);
    G[y].push_back(x);
  }
  dp.assign(N, 0);
  // dp[i] := 連結成分の個数の最大値
  del.assign(N, false);
  dfs(0, -1);
  int res = dp[0];
  cout <<res <<endl;
}
0