結果

問題 No.763 Noelちゃんと木遊び
ユーザー momoyuumomoyuu
提出日時 2022-10-25 11:59:15
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 87 ms / 2,000 ms
コード長 1,981 bytes
コンパイル時間 2,836 ms
コンパイル使用メモリ 251,956 KB
実行使用メモリ 21,776 KB
最終ジャッジ日時 2023-09-16 10:57:40
合計ジャッジ時間 5,225 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 79 ms
21,776 KB
testcase_01 AC 28 ms
7,132 KB
testcase_02 AC 67 ms
12,400 KB
testcase_03 AC 45 ms
9,452 KB
testcase_04 AC 32 ms
7,588 KB
testcase_05 AC 43 ms
9,244 KB
testcase_06 AC 84 ms
14,356 KB
testcase_07 AC 78 ms
13,808 KB
testcase_08 AC 48 ms
9,636 KB
testcase_09 AC 31 ms
7,584 KB
testcase_10 AC 13 ms
4,844 KB
testcase_11 AC 87 ms
14,712 KB
testcase_12 AC 74 ms
13,396 KB
testcase_13 AC 72 ms
13,088 KB
testcase_14 AC 64 ms
11,976 KB
testcase_15 AC 44 ms
9,632 KB
testcase_16 AC 8 ms
4,376 KB
testcase_17 AC 44 ms
9,300 KB
testcase_18 AC 84 ms
14,444 KB
testcase_19 AC 76 ms
13,264 KB
testcase_20 AC 75 ms
13,392 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ull = unsigned long long;
using ld = long double;
template<class t> using vc = vector<t>;
template<class t> using vvc = vc<vc<t>>;
using pi = pair<int,int>;
using pl = pair<ll,ll>;
using vi = vc<int>;
using vvi = vvc<int>;
using vl = vc<ll>;
using vvl = vvc<ll>;

#define rep(i,a,b) for (int i = (int)(a); i < (int)(b); i++)
#define irep(i,a,b) for (int i = (int)(a); i > (int)(b); i--)
#define all(a) a.begin(),a.end()
#define print(n) cout << n << '\n'
#define pritn(n) print(n)
#define printv(n,a) {copy(all(n),ostream_iterator<a>(cout," ")); cout<<"\n";}
#define printvv(n,a) {for(auto itr:n) printv(itr,a);}
#define rup(a,b) (a+b-1)/b
#define input(A,N) rep(i,0,N) cin>>A[i]
#define chmax(a,b) a = max(a,b)
#define chmin(a,b) a = min(a,b)

template< typename T >
struct edge{
    int from,to;
    T cost;
    /*
    *to:繋がってる先
    *cost:辺のコスト
    */
    edge(int from,int to, T cost):from(from),to(to),cost(cost){};
    edge &operator=(const int& x){
        to = x;
        return *this;
    }
};

template< typename T >
struct graph{
    int n;
    vector<vector<edge<T>>> e;
    graph(int n):n(n),e(n){}

    void addedge(int from,int to,T cost){
        e[from].emplace_back(from,to,cost);
    }

    vector<edge<T>> &operator[](int i) {
        return e[i];
    }
};


void dfs(int ni,int par,vvi&dp,graph<int>&g){
    for(auto e:g[ni]){
        if(par==e.to) continue;
        dfs(e.to,ni,dp,g);
        dp[ni][0] += max(dp[e.to][0],dp[e.to][1]);
        dp[ni][1] += dp[e.to][0];
    }
    dp[ni][1] ++;
    return ;

}


int main(){
    cout << fixed << setprecision(15);
    
    int n;
    cin>>n;
    graph<int> g(n);
    rep(i,0,n-1){
        int u,v;
        cin>>u>>v;
        u--;v--;
        g.addedge(u,v,1);
        g.addedge(v,u,1);
    }
    vvi dp(n,vi(2,0));
    dfs(0,-1,dp,g);
    print(max(dp[0][0],dp[0][1]));
    
    //system("pause");
    return 0;
}
0