2492021-03-06 12:15:28mraronÁtvágás (nehéz)cpp14Accepted 75/75254ms102912 KiB
#include<bits/stdc++.h>
using namespace std;

vector<int> adj[200001];

int dp[200001][2], st[200001];

void dfs(int x) {
	st[x]=1;
	int f=0;
	
	vector<int> lst;
	for(auto i:adj[x]) {
		if(st[i]) {
			continue ;
		}
		
		f++;
		
		dfs(i);
		lst.push_back(i);
	}
	
	if(0==f) {
		dp[x][1]=dp[x][0]=0;
		return ;
	}
	
	dp[x][1]=dp[x][0];
	
	int sum=0;
	for(int i:lst) {
		sum+=dp[i][1];
	}
	
	dp[x][0]=max(dp[x][0], sum);
	for(int i:lst) {
		dp[x][0]=max(dp[x][0], sum-dp[i][1]+dp[i][0]+1);
	}
	
	sort(lst.begin(), lst.end(), [&](int x, int y) -> bool {
		return dp[x][0]-dp[x][1]>dp[y][0]-dp[y][1];
	});

	dp[x][1]=max(dp[x][1], sum);
	if(f>1) {
		//~ cerr<<sum<<"\n";
		dp[x][1]=max(dp[x][1], dp[lst[0]][0]+dp[lst[1]][0]+2+sum-(dp[lst[0]][1]+dp[lst[1]][1]));
	}
	dp[x][1]=max(dp[x][1], dp[lst[0]][0]+1+sum-(dp[lst[0]][1]));
	
	//~ cerr<<x<<" "<<dp[x][0]<<" "<<dp[x][1]<<"\n";
}


int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int n;
	cin>>n;
	vector<int> deg(n+1);
	for(int i=1;i<n;++i) {
		int a,b;
		cin>>a>>b;
		deg[a]++;
		deg[b]++;
		adj[a].push_back(b);
		adj[b].push_back(a);
	}
	
	
	int res2=n-1-*max_element(deg.begin(), deg.end());
	
	dfs(1);
	int res1=n-1-dp[1][1];
	
	cout<<res1<<" "<<res2<<"\n";
	
	
	
	return 0;
}
SubtaskSumTestVerdictTimeMemory
base75/75
1Accepted0/06ms11184 KiB
2Accepted0/04ms11228 KiB
3Accepted0/010ms12292 KiB
4Accepted4/46ms11256 KiB
5Accepted4/46ms11264 KiB
6Accepted4/46ms11260 KiB
7Accepted4/46ms11400 KiB
8Accepted4/46ms11280 KiB
9Accepted4/44ms11416 KiB
10Accepted4/4129ms34516 KiB
11Accepted4/4146ms35228 KiB
12Accepted4/4158ms35100 KiB
13Accepted4/4146ms37488 KiB
14Accepted4/4177ms39120 KiB
15Accepted4/4212ms40672 KiB
16Accepted4/4232ms42860 KiB
17Accepted4/4238ms45416 KiB
18Accepted5/5231ms48768 KiB
19Accepted6/6254ms72412 KiB
20Accepted6/6238ms102912 KiB
21Accepted2/26ms34100 KiB