QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#466896#7684. Sweet Sugarmango09#TL 5ms22124kbC++141.7kb2024-07-08 11:46:392024-07-08 11:46:39

Judging History

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

  • [2024-07-08 11:46:39]
  • 评测
  • 测评结果:TL
  • 用时:5ms
  • 内存:22124kb
  • [2024-07-08 11:46:39]
  • 提交

answer

#include<bits/stdc++.h>
#define pii pair<int,int>
#define A first
#define B second
using namespace std;
const int N=1e6+10;
int n,k,c[N],ans,f[N],sum[N],in[N];
int fst[N],nxt[2*N],to[2*N],ecnt;
int q1[N],q2[N],r1,r2;
void adde(int u,int v){
	ecnt++;
	to[ecnt]=v;
	nxt[ecnt]=fst[u];
	fst[u]=ecnt;
}
void kick(int u){
	if(c[u]==1&&q1[r1]==u)r1--;
	if(c[u]==2&&q2[r2]==u)r2--;
	sum[u]=0;
	int fa=f[u];
	in[fa]--;
	if(!in[fa]){
		if(c[fa]==1)q1[++r1]=fa;
		if(c[fa]==2)q2[++r2]=fa;
	}
}
void dfs(int u,int fa,int l1,int l2){
	int c1=0,c2=0;
	sum[u]=c[u];f[u]=fa;in[u]=0;
	if(!fst[u]){
		if(c[u]==1){
			if(k==1){
				ans++,kick(u);
				return;
			}
			q1[++r1]=u;
			return;
		}else if(c[u]==2){
			if(k==1){
				kick(u);
				return;
			}
			if(k==2){
				ans++;kick(u);
				return;
			}
			q2[++r2]=u;
			return;
		}
		kick(u);
		return;
	}
	for(int i=fst[u];i;i=nxt[i]){
		int v=to[i];
		if(v!=fa)in[u]++;
	}
	for(int i=fst[u];i;i=nxt[i]){
		int v=to[i];
		if(v==fa)continue;
		dfs(v,u,r1+1,r2+1);
		sum[u]+=sum[v];
	}
	if(!in[u]&&!c[u]){
		kick(u);
		r1=l1-1;
		r2=l2-1;
		return;
	}
	if(sum[u]==k){
		ans++;
		kick(u);
		r1=l1-1;
		r2=l2-1;
		return;
	}if(sum[u]<k)return;
	while(sum[u]>k){
		if(r2>=l2){
			kick(q2[r2]);
			sum[u]-=2;
		}else if(r1>=l1){
			kick(q1[r1]);
			sum[u]-=1;
		}
	}
	if(sum[u]==k){
		ans++;
		kick(u);
		r1=l1-1;
		r2=l2-1;
		return;
	}
	return;
}
void solve(){
	ecnt=r1=r2=ans=0;
	memset(fst,0,sizeof(fst));
	cin>>n>>k;
	for(int i=1;i<=n;i++)cin>>c[i];
	for(int i=1,u,v;i<n;i++){
		cin>>u>>v;
		adde(u,v);
		adde(v,u);
	}
	dfs(1,0,1,1);
	cout<<ans<<"\n";
}
int main(){
	int T;cin>>T;
	while(T--)solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 22124kb

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: 5ms
memory: 17872kb

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
Time Limit Exceeded

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

result: