QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#673522#7684. Sweet Sugari0streamWA 47ms12144kbC++141.7kb2024-10-24 23:17:442024-10-24 23:17:45

Judging History

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

  • [2024-10-24 23:17:45]
  • 评测
  • 测评结果:WA
  • 用时:47ms
  • 内存:12144kb
  • [2024-10-24 23:17:44]
  • 提交

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) return;
	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=dp[u][0],u1=dp[u][1];
		if (dp[v][1]){
			u1=max(u1,dp[u][0]+dp[v][1]);
			if (dp[u][1]) u0=max(u0,dp[u][1]+dp[v][1]);
		}
		u0=max(u0,dp[u][0]+dp[v][0]);
		if (dp[u][1]) u1=max(u1,dp[u][1]+dp[v][0]);
		dp[u][0]=u0;dp[u][1]=u1;
	}
	int u0=dp[u][0],u1=dp[u][1];
	if (a[u]&1){
		u1=max(u1,dp[u][0]+a[u]);
		if (dp[u][1]) u0=max(u0,dp[u][1]+a[u]);
	}else{
		u0=max(u0,dp[u][0]+a[u]);
		if (dp[u][1]) u1=max(u1,dp[u][1]+a[u]);
	}
	dp[u][0]=u0;dp[u][1]=u1;
	//cout<<u<<' '<<dp[u][0]<<" "<<dp[u][1]<<endl;
	if (dp[u][k&1]>=k){
		//cout<<u<<endl;
		ans++;
		dp[u][0]=0;
		dp[u][1]=0;
	}
}

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]=0,dp[i][1]=0;
		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;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 11920kb

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: 47ms
memory: 12144kb

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
1
1
1
1
2
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 73rd numbers differ - expected: '1', found: '2'