結果

問題 No.763 Noelちゃんと木遊び
コンテスト
ユーザー fura
提出日時 2020-05-15 10:47:46
言語 C++17
(gcc 15.2.0 + boost 1.90.0)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 41 ms / 2,000 ms
コード長 1,003 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,098 ms
コンパイル使用メモリ 219,520 KB
実行使用メモリ 21,504 KB
最終ジャッジ日時 2026-07-07 19:41:41
合計ジャッジ時間 3,019 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

template<class T> struct edge{
	int to;
	T wt;
	edge(int to,const T& wt):to(to),wt(wt){}
};
template<class T> using weighted_graph=vector<vector<edge<T>>>;

weighted_graph<int> T;

int memo[200000][2];

int dfs(int u,int p,int id,bool b){ // id: 辺 p-u の番号, b: 頂点 u を残すかどうか
	if(~memo[id][b]) return memo[id][b];

	int res;
	if(b){
		res=1;
		for(auto e:T[u]) if(e.to!=p) {
			res+=dfs(e.to,u,e.wt,false);
		}
	}
	else{
		res=0;
		for(auto e:T[u]) if(e.to!=p) {
			res+=max(dfs(e.to,u,e.wt,true),dfs(e.to,u,e.wt,false));
		}
	}
	return memo[id][b]=res;
}

int main(){
	int n; scanf("%d",&n);
	T.resize(n);
	rep(i,n-1){
		int u,v; scanf("%d%d",&u,&v); u--; v--;
		T[u].emplace_back(v,i);
		T[v].emplace_back(u,i+n);
	}

	int ans=0;
	memset(memo,-1,sizeof memo);
	rep(u,n){
		int tmp=0;
		for(auto e:T[u]) tmp+=dfs(e.to,u,e.wt,true);
		ans=max(ans,tmp);
	}
	printf("%d\n",ans);

	return 0;
}
0