結果

問題 No.1752 Up-Down Tree
ユーザー 沙耶花沙耶花
提出日時 2021-11-19 22:26:50
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,859 bytes
コンパイル時間 4,283 ms
コンパイル使用メモリ 263,916 KB
実行使用メモリ 19,172 KB
最終ジャッジ日時 2023-08-30 09:11:19
合計ジャッジ時間 7,063 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 130 ms
19,148 KB
testcase_01 AC 136 ms
19,172 KB
testcase_02 AC 74 ms
10,364 KB
testcase_03 AC 76 ms
10,200 KB
testcase_04 AC 113 ms
14,648 KB
testcase_05 AC 119 ms
16,328 KB
testcase_06 AC 108 ms
13,256 KB
testcase_07 AC 104 ms
13,044 KB
testcase_08 AC 64 ms
7,328 KB
testcase_09 AC 101 ms
9,496 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace atcoder;
using mint = modint998244353;
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf 1000000001
int N;
vector<vector<int>> E;
vector<int> sz,dpA,dpB;

void dfs(int cur,int p){
	sz[cur] = 1;
	rep(i,E[cur].size()){
		int to = E[cur][i];
		if(to==p)continue;
		dfs(to,cur);
		sz[cur] += sz[to];
	}
}

void dfs1(int cur,int p){
	dpA[cur] = 1;
	rep(i,E[cur].size()){
		int to = E[cur][i];
		if(to==p)continue;
		dfs1(to,cur);
	}
	
	rep(i,E[cur].size()){
		int to = E[cur][i];
		if(to==p)continue;
		dpB[cur] = max(dpB[cur],dpA[to] + 1);
		dpB[cur] = max(dpB[cur],dpB[to]);
		dpA[cur] = max(dpA[cur],dpA[to]);
		if(dpA[to]!=sz[to]){
			dpA[cur] = max(dpA[cur],dpA[to]+2);
		}
	}
	vector<int> dp(4,0);
	rep(i,E[cur].size()){
		int to = E[cur][i];
		if(to==p)continue;
		vector<int> ndp(4,0);
		rep(j,4){
			ndp[j] = max(ndp[j],dp[j]);
			rep(k,2){
				if((j>>k)&1)continue;
				ndp[j|(1<<k)] = max(ndp[j|(1<<k)], dp[j] + dpA[to]);
			}
		}
		swap(dp,ndp);
	}
	dpA[cur] = max(dpA[cur],dp[3] + 1);
	dp.assign(4,0);
	rep(i,E[cur].size()){
		int to = E[cur][i];
		if(to==p)continue;
		vector<int> ndp(4,0);
		rep(j,4){
			ndp[j] = max(ndp[j],dp[j]);
			rep(k,2){
				if((j>>k)&1)continue;
				if(k==0)ndp[j|(1<<k)] = max(ndp[j|(1<<k)], dp[j] + dpA[to]);
				else ndp[j|(1<<k)] = max(ndp[j|(1<<k)], dp[j] + dpB[to]);
			}
		}
		swap(dp,ndp);
	}
	dpB[cur] = max(dpB[cur],dp[3] + 1);
}
		

int main(){
	
	cin>>N;
	E.resize(N);
	rep(i,N-1){
		int u,v;
		cin>>u>>v;
		u--;v--;
		E[u].push_back(v);
		E[v].push_back(u);
	}
	
	sz.resize(N,0);
	dpA.resize(N,0);
	dpB.resize(N,0);
	dfs(0,-1);
	dfs1(0,-1);
	
	int ans = 0;
	rep(i,N){
		ans = max(ans,dpA[i]);
		ans = max(ans,dpB[i]);
	}
	/*
	rep(i,N){
		cout<<dpA[i]<<endl;
	}
	*/
	cout<<ans<<endl;
	
	return 0;
}
0