QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#466969 | #7684. Sweet Sugar | mango09# | WA | 256ms | 20248kb | C++14 | 1.7kb | 2024-07-08 12:14:00 | 2024-07-08 12:14:02 |
Judging History
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];
if(!fa)return;
in[fa]--;
if(!in[fa]){
if(c[fa]==1)q1[++r1]=fa;
if(c[fa]==2)q2[++r2]=fa;
if(c[fa]==0)kick(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;
}
while(sum[u]>k){
if(r2>=l2){
kick(q2[r2]);
sum[u]-=2;
}else if(r1>=l1){
kick(q1[r1]);
sum[u]-=1;
}else return;
}
if(sum[u]==k){
ans++;
kick(u);
r1=l1-1;
r2=l2-1;
return;
}
}
void solve(){
ecnt=r1=r2=ans=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,1,1);
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: 20248kb
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: 16160kb
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: 256ms
memory: 18236kb
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 2 3 0 0 2 0 0 2 1 0 0 1 1 0 0 0 0 0 0 1 0 0 0 0 0 1 1 0 0 0 2 0 1 0 0 0 0 1 3 0 0 1 0 2 1 0 0 4 0 0 1 0 1 0 0 1 5 0 1 0 1 0 1 0 1 1 1 1 0 1 0 1 0 3 1 0 0 0 0 2 0 0 0 0 1 0 0 1 0 1 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 0 2 0 0 0 0 0 0 0 0 0 0 0 ...
result:
wrong answer 6th numbers differ - expected: '3', found: '2'