結果
| 問題 | No.763 Noelちゃんと木遊び | 
| コンテスト | |
| ユーザー |  peroon | 
| 提出日時 | 2019-04-11 16:25:23 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 59 ms / 2,000 ms | 
| コード長 | 1,711 bytes | 
| コンパイル時間 | 898 ms | 
| コンパイル使用メモリ | 98,516 KB | 
| 実行使用メモリ | 17,920 KB | 
| 最終ジャッジ日時 | 2025-03-22 10:35:52 | 
| 合計ジャッジ時間 | 2,711 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 22 | 
ソースコード
#include<algorithm>
#include<complex>
#include<ctype.h>
#include<iomanip>
#include<iostream>
#include<fstream>
#include<map>
#include<math.h>
#include<numeric>
#include<queue>
#include<set>
#include<stack>
#include<stdio.h>
#include<string>
#include<string>
#include<vector>
using namespace std;
typedef long long ll;
#define FOR(i,a,b) for(ll i=(a);i<(b);++i)
#define ALL(v) (v).begin(), (v).end()
#define p(s) cout<<(s)<<endl
#define p2(s, t) cout << (s) << " " << (t) << endl
#define br() p("")
#define pn(s) cout << (#s) << " " << (s) << endl
#define p_yes() p("Yes")
#define p_no() p("No")
const ll mod = 1e9 + 7;
const ll inf = 1e18;
template < typename T >
void vprint(T &V){
	for(auto v : V){
    	cout << v << " ";
	}
	cout << endl;
}
const int N_MAX = 100010;
vector<vector<ll> > G;
ll dp[N_MAX][2]; 
// dp[i][0] : iを削除する場合の最大値
// dp[i][0] : iを削除しない場合の最大値
// dfs(i)を呼ぶと
// dp[i][0]
// dp[i][1]
// が埋まるとする
void dfs(ll i, ll parent){
    // leaf
    if(G[i].size()==1 && G[i][0]==parent){
        dp[i][0] = 0;
        dp[i][1] = 1;
        return;
    }
    dp[i][0] = 0;
    dp[i][1] = 1;
    
    for(auto to : G[i]){
        if(to==parent) continue;
        dfs(to, i);
        dp[i][0] += max(dp[to][0], dp[to][1]);
        dp[i][1] += max(dp[to][0], dp[to][1]-1);
    }
}
int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    // input
    ll N;
    cin >> N;
    G.resize(N);
    FOR(i, 0, N-1){
        ll u, v;
        cin >> u >> v;
        u--;
        v--;
        G[u].push_back(v);
        G[v].push_back(u);
    }
    dfs(0, -1);
    ll ans = max(dp[0][0], dp[0][1]);
    p(ans);
    return 0;
}
            
            
            
        