QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#778172#2596. Even ForestniumachaorenTL 9ms61960kbC++141.3kb2024-11-24 13:05:382024-11-24 13:05:38

Judging History

你现在查看的是最新测评结果

  • [2024-11-24 13:05:38]
  • 评测
  • 测评结果:TL
  • 用时:9ms
  • 内存:61960kb
  • [2024-11-24 13:05:38]
  • 提交

answer

#include<bits/stdc++.h>
inline void read(int &x){
	x=0;char ch=getchar();
	while(!isdigit(ch))ch=getchar();
	while(isdigit(ch)){
		x=(x<<1)+(x<<3)+(ch^48);
		ch=getchar();
	}
}
using namespace std;
const int maxn=1e6+10;
int n,dp[maxn][2],g[maxn][2],rt;
vector<int>e[maxn<<1];
void dfs(int u,int fa){
	if(e[u].size()==1&&u!=rt){
		// cout<<rt<<'\n';
		dp[u][0]=0;
		return ;
	}
	int siz=0;
	for(int v:e[u]){
		if(v==fa)continue;
		siz++;
		dfs(v,u);
	}
	for(int i=0;i<=siz;i++){
		g[i][0]=g[i][1]=0x3f3f3f3f;
	}
	g[0][0]=g[0][1]=0;
	for(int i=1;i<=e[u].size();i++){
		int v=e[u][i-1];
		if(v==fa)continue;
		for(int j=i;j>=0;j--){
			g[j][0]=min(g[j][0]+dp[v][1],g[j-1][0]+dp[v][0]);//选j个偶的留下
			g[j][1]=min(g[j][1]+dp[v][0],g[j-1][1]+dp[v][1]);//选j个奇的留下
		}
	}
	for(int j=0;j<=siz;j++){
		dp[u][0]=min(dp[u][0],g[j][1]+siz-j);
		dp[u][1]=min(dp[u][1],g[j][0]+siz-j);
	}
}
int main(){
	read(n);
	for(int i=1;i<n;i++){
		int u,v;
		read(u),read(v);
		e[u].push_back(v);
		e[v].push_back(u);
	}
	int cnt=0,cnt2=0;
	for(int i=1;i<=n;i++){
		if(e[i].size()==1)cnt++;
	}
	if(cnt==2){
		cout<<((n&1)^1)<<'\n';
		return 0;
	}
	int ans=0;
	memset(dp,0x3f,sizeof(dp));
	rt=1;
	dfs(rt,0);
	ans=min(dp[rt][1],dp[rt][0]);
	cout<<ans<<'\n';
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 9ms
memory: 51560kb

input:

4
1 2
2 3
3 4

output:

1

result:

ok 1 number(s): "1"

Test #2:

score: 0
Accepted
time: 0ms
memory: 61960kb

input:

4
1 2
1 3
1 4

output:

0

result:

ok 1 number(s): "0"

Test #3:

score: 0
Accepted
time: 8ms
memory: 59964kb

input:

6
1 2
2 3
4 2
4 5
6 4

output:

1

result:

ok 1 number(s): "1"

Test #4:

score: 0
Accepted
time: 4ms
memory: 59928kb

input:

1

output:

0

result:

ok 1 number(s): "0"

Test #5:

score: 0
Accepted
time: 4ms
memory: 51180kb

input:

2
1 2

output:

1

result:

ok 1 number(s): "1"

Test #6:

score: 0
Accepted
time: 7ms
memory: 51976kb

input:

3
2 3
3 1

output:

0

result:

ok 1 number(s): "0"

Test #7:

score: 0
Accepted
time: 8ms
memory: 59872kb

input:

4
2 3
3 1
3 4

output:

0

result:

ok 1 number(s): "0"

Test #8:

score: 0
Accepted
time: 4ms
memory: 50596kb

input:

5
4 2
2 3
4 1
5 3

output:

0

result:

ok 1 number(s): "0"

Test #9:

score: 0
Accepted
time: 8ms
memory: 59940kb

input:

6
4 3
1 4
2 4
5 2
4 6

output:

1

result:

ok 1 number(s): "1"

Test #10:

score: 0
Accepted
time: 4ms
memory: 60036kb

input:

50
21 22
22 27
22 7
22 12
22 13
43 22
36 22
35 22
6 22
10 22
22 38
19 22
34 22
8 22
22 44
22 3
9 22
22 16
23 22
18 22
22 25
22 5
22 2
22 15
46 22
37 22
48 22
33 22
22 17
31 22
22 29
22 28
22 49
4 22
22 26
41 22
22 32
22 50
22 20
22 30
24 22
40 22
22 45
14 22
22 11
42 22
39 22
1 22
47 22

output:

0

result:

ok 1 number(s): "0"

Test #11:

score: -100
Time Limit Exceeded

input:

1000000
851841 325834
455962 325834
135775 325834
525341 325834
265267 325834
868520 325834
834325 325834
867971 325834
325834 879726
325834 242607
325834 106951
122113 325834
325834 499633
727580 325834
325834 200171
325834 178877
325834 493841
957118 325834
325834 809324
325834 641895
325834 33338...

output:


result: