QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#673474#7684. Sweet Sugari0streamWA 51ms11984kbC++142.2kb2024-10-24 22:40:432024-10-24 22:40:45

Judging History

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

  • [2024-10-24 22:40:45]
  • 评测
  • 测评结果:WA
  • 用时:51ms
  • 内存:11984kb
  • [2024-10-24 22:40:43]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;
const int N=1e6+100;

int dp[N][2],a[N],n,T,k,u,v,ans;
int E,head[N],to[N<<1],nxt[N<<1];

int read(){
    int flag=1,x=0,c=getchar();
    while (c<'0' || c>'9'){if (c=='-') flag=-1;c=getchar();}
    while (c>='0' && c<='9') x=x*10+c-48,c=getchar();
    return x*flag;
}

void addedge(int u,int v){
    to[++E]=v;
    nxt[E]=head[u];
    head[u]=E;
}

inline int calc(int x,int y){
	if (x==-1 && y==-1) return -1;
	if (x==-1) return y;
	if (y==-1) return x;
	return x+y;
}

void sol(int &x,int &y,int t){
	if (t&1) y=max(y,t);
	else x=max(x,t);
}

void dfs(int u,int F){
	for (int e=head[u];e;e=nxt[e]){
		int v=to[e];
		if (v==F) continue;
		dfs(v,u);
		int u0=-1,u1=-1,p;
		p=calc(dp[u][0],dp[v][0]);
		if (p!=-1) sol(u0,u1,p);
		p=calc(dp[u][0],dp[v][1]);
		if (p!=-1) sol(u0,u1,p);
		p=calc(dp[u][1],dp[v][0]);
		if (p!=-1) sol(u0,u1,p);
		p=calc(dp[u][1],dp[v][1]);
		if (p!=-1) sol(u0,u1,p);
		dp[u][0]=u0;dp[u][1]=u1;
	}
	//cout<<u<<' '<<dp[u][0]<<" "<<dp[u][1]<<endl;
	int u0=-1,u1=-1;
	if (dp[u][0]==-1 && a[u]%2==0) u0=a[u];
	if (a[u]%2==1 && dp[u][1]!=-1) u0=dp[u][1]+a[u];
	if (a[u]%2==0 && dp[u][0]!=-1) u0=max(u0,dp[u][0]+a[u]);
	if (dp[u][1]==-1 && a[u]%2==1) u1=a[u];
	if (a[u]%2==1 && dp[u][0]!=-1) u1=dp[u][0]+a[u];
	if (a[u]%2==0 && dp[u][1]!=-1) u1=max(u1,dp[u][1]+a[u]);
	dp[u][0]=u0;dp[u][1]=u1;
//	int u0=dp[u][0],u1=dp[u][1];
//	if (a[u]&1){
//		if (dp[u][1]!=-1) u0=dp[u][1]+a[u];
//		if (dp[u][0]!=-1) u1=dp[u][0]+a[u];
//	}else{
//		if (dp[u][0]!=-1) u0=dp[u][0]+a[u];
//		if (dp[u][1]!=-1) u1=dp[u][1]+a[u];
//	}
//	if (dp[u][a[u]&1]==-1) dp[u][a[u]&1]=a[u];
	//cout<<u<<' '<<dp[u][0]<<" "<<dp[u][1]<<endl;
	if (dp[u][k&1]>=k){
		//cout<<u<<endl;
		ans++;
		dp[u][0]=-1;
		dp[u][1]=-1;
	}
}

int main(){
	T=read();
	while (T--){
		n=read();k=read();
		E=0;ans=0;
		for (int i=0;i<=n+10;i++) head[i]=0,dp[i][0]=-1,dp[i][1]=-1;
		for (int i=0;i<=n*2+10;i++) to[i]=0,nxt[i]=0;
		for (int i=1;i<=n;i++) a[i]=read();
		for (int i=1;i<n;i++){
			u=read();v=read();
			addedge(u,v);addedge(v,u);
		}
		dfs(1,0);
		printf("%d\n",ans);
	}
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 11984kb

input:

4
7 5
1 2 1 2 2 1 2
1 2
2 3
3 4
3 5
5 6
5 7
2 2
1 0
1 2
1 1
1
1 2
1

output:

2
0
1
0

result:

ok 4 number(s): "2 0 1 0"

Test #2:

score: 0
Accepted
time: 1ms
memory: 11976kb

input:

12
1 1
0
1 1
1
1 1
2
1 2
0
1 2
1
1 2
2
1 3
0
1 3
1
1 3
2
1 2000000
0
1 2000000
1
1 2000000
2

output:

0
1
0
0
0
1
0
0
0
0
0
0

result:

ok 12 numbers

Test #3:

score: -100
Wrong Answer
time: 51ms
memory: 11932kb

input:

200000
5 2
1 1 0 0 1
2 4
5 2
4 1
3 2
5 1
0 0 0 0 0
5 1
1 2
3 2
5 4
5 3
1 0 0 0 1
1 4
4 2
3 4
5 2
5 9
1 0 0 0 2
4 3
2 1
3 1
5 1
5 3
0 1 1 0 1
5 4
2 1
4 3
5 1
5 1
0 2 1 1 1
5 3
2 4
3 4
1 4
5 1
1 0 1 1 0
1 5
4 2
1 3
5 2
5 7
0 2 1 1 2
5 1
2 3
2 5
5 4
5 5
0 1 0 1 0
2 4
4 3
5 2
1 5
5 1
0 0 1 0 1
4 1
4 5
2...

output:

1
0
0
0
1
3
3
0
0
2
0
0
2
1
0
0
1
1
0
2
0
1
0
2
1
0
0
0
0
0
1
2
0
0
2
2
0
1
0
0
0
0
3
3
0
0
1
1
2
1
2
0
4
0
1
1
0
1
0
0
1
5
0
1
1
1
0
1
0
1
1
1
1
0
1
1
1
0
3
1
0
1
0
0
4
0
0
0
1
1
0
0
1
0
2
0
5
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
1
0
0
0
0
1
0
0
0
0
0
1
0
0
1
1
0
1
0
1
1
1
2
0
1
2
0
0
2
0
0
1
0
0
0
0
0
...

result:

wrong answer 69th numbers differ - expected: '1', found: '0'