結果

問題 No.763 Noelちゃんと木遊び
ユーザー poohbearpoohbear
提出日時 2020-08-08 10:12:03
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 85 ms / 2,000 ms
コード長 1,149 bytes
コンパイル時間 2,136 ms
コンパイル使用メモリ 203,948 KB
実行使用メモリ 17,280 KB
最終ジャッジ日時 2023-07-24 12:23:51
合計ジャッジ時間 5,071 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
17,280 KB
testcase_01 AC 27 ms
5,484 KB
testcase_02 AC 66 ms
8,388 KB
testcase_03 AC 44 ms
6,808 KB
testcase_04 AC 30 ms
5,668 KB
testcase_05 AC 43 ms
6,788 KB
testcase_06 AC 80 ms
9,732 KB
testcase_07 AC 81 ms
9,356 KB
testcase_08 AC 46 ms
7,004 KB
testcase_09 AC 30 ms
5,668 KB
testcase_10 AC 12 ms
4,380 KB
testcase_11 AC 84 ms
9,760 KB
testcase_12 AC 76 ms
8,788 KB
testcase_13 AC 70 ms
8,976 KB
testcase_14 AC 63 ms
8,376 KB
testcase_15 AC 43 ms
6,668 KB
testcase_16 AC 8 ms
4,376 KB
testcase_17 AC 44 ms
6,804 KB
testcase_18 AC 85 ms
9,648 KB
testcase_19 AC 76 ms
9,120 KB
testcase_20 AC 78 ms
9,172 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