QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#467080 | #7684. Sweet Sugar | mango09# | TL | 2ms | 16104kb | C++14 | 2.0kb | 2024-07-08 13:43:04 | 2024-07-08 13:43:04 |
Judging History
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,q3;
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();
if(q3.top()==u)q3.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){
if(c[f[fa]]==1)q3.push(fa);
else q2.push(fa);
}
if(c[fa]==0)kick(fa);
}
}
void dfs(int u,int fa){
int t1=q1.top(),t2=q2.top(),t3=q2.top();
sum[u]=c[u];f[u]=fa;
if(!in[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;
}
if(c[fa]==1)q3.push(u);
else 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)continue;
dfs(v,u);
// cout<<v<<" OK\n";
sum[u]+=sum[v];
}
while(sum[u]>k){
if(q1.top()!=t1&&sum[u]==k+1){
kick(q1.top());
sum[u]-=1;
}else if(q3.top()!=t3){
kick(q3.top());
sum[u]-=2;
}else 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();
while(q3.top()!=t3)q3.pop();
}
}
void solve(){
ecnt=ans=0;
while(!q1.empty())q1.pop();
while(!q2.empty())q2.pop();
while(!q3.empty())q3.pop();
q1.push(0),q2.push(0),q3.push(0);
cin>>n>>k;
for(int i=1;i<=n;i++)fst[i]=0;
for(int i=1;i<=n;i++)in[i]=-1;in[1]=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);
in[u]++,in[v]++;
}
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: 16104kb
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: 2ms
memory: 14048kb
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 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