QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#467038 | #7684. Sweet Sugar | mango09# | WA | 255ms | 16128kb | C++14 | 1.8kb | 2024-07-08 13:07:24 | 2024-07-08 13:07:25 |
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;
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;
}else return;
}
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: 16128kb
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: 14052kb
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: 255ms
memory: 16104kb
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'