QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#466976#7684. Sweet Sugarmango09#TL 0ms16360kbC++141.7kb2024-07-08 12:31:172024-07-08 12:31:19

Judging History

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

  • [2024-07-08 12:31:19]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:16360kb
  • [2024-07-08 12:31:17]
  • 提交

answer

#include<bits/stdc++.h>
#define pii pair<int,int>
#define A first
#define B second
using namespace std;
const int N=4e6+10;
int n,k,c[N],ans,f[N],sum[N],in[N];
int fst[N],nxt[2*N],to[2*N],ecnt;
stack<int>q1,q2;
void adde(int u,int v){
	ecnt++;
	to[ecnt]=v;
	nxt[ecnt]=fst[u];
	fst[u]=ecnt;
}
void kick(int u){
	if(q1.top()==u)q1.pop();
	if(q2.top()==u)q2.pop();
	sum[u]=0;
	int fa=f[u];
	if(!fa)return;
	in[fa]--;
	if(!in[fa]){
		if(c[fa]==1)q1.push(fa);
		if(c[fa]==2)q2.push(fa);
		if(c[fa]==0)kick(fa);
	}
}
void dfs(int u,int fa){
	int t1=q1.top(),t2=q2.top();
	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.push(u);
			return;
		}else if(c[u]==2){
			if(k==1){
				kick(u);
				return;
			}
			if(k==2){
				ans++;kick(u);
				return;
			}
			q2.push(u);
			return;
		}
		kick(u);
		return;
	}
//	cout<<u<<" coming\n";
	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);
//		cout<<v<<" OK\n";
		sum[u]+=sum[v];
	}
	while(sum[u]>k){
		if(q2.top()!=t2){
			kick(q2.top());
			sum[u]-=2;
		}else if(q1.top()!=t1){
			kick(q1.top());
			sum[u]-=1;
		}
	}
	if(sum[u]==k){
		ans++;
		kick(u);
		while(q1.top()!=t1)q1.pop();
		while(q2.top()!=t2)q2.pop();
		return;
	}
}
void solve(){
	ecnt=ans=0;
	while(!q1.empty())q1.pop();
	while(!q2.empty())q2.pop();
	q1.push(0),q2.push(0);
	cin>>n>>k;
	for(int i=1;i<=n;i++)fst[i]=0;
	for(int i=1;i<=n;i++)scanf("%d",&c[i]);
	for(int i=1,u,v;i<n;i++){
		scanf("%d%d",&u,&v);
		adde(u,v);
		adde(v,u);
	}
	dfs(1,0);
	printf("%d\n",ans);
}
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: 0ms
memory: 16360kb

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: 0ms
memory: 14084kb

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: