57642023-09-20 23:57:18lalalaÁtvágás (nehéz)cpp17Accepted 75/75208ms84484 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/06ms11264 KiB
2Accepted0/06ms11504 KiB
3Accepted0/09ms12864 KiB
4Accepted4/46ms11984 KiB
5Accepted4/46ms12064 KiB
6Accepted4/46ms12068 KiB
7Accepted4/46ms12076 KiB
8Accepted4/46ms12088 KiB
9Accepted4/47ms12320 KiB
10Accepted4/496ms37176 KiB
11Accepted4/4104ms37000 KiB
12Accepted4/4108ms36160 KiB
13Accepted4/4108ms36280 KiB
14Accepted4/4123ms35088 KiB
15Accepted4/4137ms34204 KiB
16Accepted4/4141ms34024 KiB
17Accepted4/4140ms34012 KiB
18Accepted5/5165ms34984 KiB
19Accepted6/6160ms56432 KiB
20Accepted6/6208ms84484 KiB
21Accepted2/26ms15776 KiB