結果

問題 No.763 Noelちゃんと木遊び
ユーザー poohbearpoohbear
提出日時 2020-08-08 10:12:04
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 99 ms / 2,000 ms
コード長 1,149 bytes
コンパイル時間 2,384 ms
コンパイル使用メモリ 207,148 KB
実行使用メモリ 19,072 KB
最終ジャッジ日時 2024-04-08 15:04:17
合計ジャッジ時間 4,524 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 80 ms
19,072 KB
testcase_01 AC 30 ms
6,548 KB
testcase_02 AC 77 ms
8,704 KB
testcase_03 AC 51 ms
7,040 KB
testcase_04 AC 36 ms
6,548 KB
testcase_05 AC 49 ms
6,912 KB
testcase_06 AC 98 ms
9,856 KB
testcase_07 AC 91 ms
9,600 KB
testcase_08 AC 52 ms
7,168 KB
testcase_09 AC 34 ms
6,548 KB
testcase_10 AC 13 ms
6,548 KB
testcase_11 AC 99 ms
9,984 KB
testcase_12 AC 87 ms
9,216 KB
testcase_13 AC 84 ms
9,216 KB
testcase_14 AC 73 ms
8,576 KB
testcase_15 AC 49 ms
6,912 KB
testcase_16 AC 8 ms
6,548 KB
testcase_17 AC 49 ms
7,040 KB
testcase_18 AC 98 ms
9,856 KB
testcase_19 AC 85 ms
9,344 KB
testcase_20 AC 85 ms
9,344 KB
権限があれば一括ダウンロードができます

ソースコード

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