結果

問題 No.763 Noelちゃんと木遊び
ユーザー fura
提出日時 2020-05-15 10:47:46
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 81 ms / 2,000 ms
コード長 1,003 bytes
コンパイル時間 2,000 ms
コンパイル使用メモリ 198,500 KB
実行使用メモリ 19,712 KB
最終ジャッジ日時 2025-03-22 10:37:46
合計ジャッジ時間 4,097 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 22
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:38:21: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   38 |         int n; scanf("%d",&n);
      |                ~~~~~^~~~~~~~~
main.cpp:41:31: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   41 |                 int u,v; scanf("%d%d",&u,&v); u--; v--;
      |                          ~~~~~^~~~~~~~~~~~~~

ソースコード

diff #

#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