結果

問題 No.763 Noelちゃんと木遊び
ユーザー poohbear
提出日時 2020-08-08 10:12:03
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 99 ms / 2,000 ms
コード長 1,149 bytes
コンパイル時間 1,985 ms
コンパイル使用メモリ 198,700 KB
実行使用メモリ 18,812 KB
最終ジャッジ日時 2025-03-22 10:38:27
合計ジャッジ時間 4,458 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(a,n) for (ll a = 0; a < (n); ++a)
using namespace std;
typedef long long ll;
using ll = long long;
typedef pair<int,int> P;
typedef pair<P,ll> PP;
using Graph = vector<vector<ll> >;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const ll INF = 1e18;

Graph g;
ll n;
bool ok[100010];
ll dp[100010];

void dfs(int v,int p=-1){
    int deg = g[v].size();
    if(p!=-1&&deg==1){
        dp[v]=1;
        return;
    }
    int res = 0;
    for(int i=0;i<deg;i++){
        int u = g[v][i];
        if(u==p)continue;
        dfs(u,v);
        if(ok[u]){
            res += dp[u];
            ok[v]=false;
        }
        else{
            res += dp[u];
        }
    }
    if(ok[v])res++;
    dp[v]=res;
    return;
}


int main(){
    cin >> n;
    g.resize(n);
    rep(i,n)ok[i]=true;
    rep(I,n-1){
        int u,v;
        cin >> u >> v;
        u--;v--;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    dfs(0,-1);
    cout << dp[0] << endl;

    return 0;
}
0