QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#673011 | #7684. Sweet Sugar | i0stream | WA | 46ms | 12068kb | C++14 | 1.4kb | 2024-10-24 20:17:38 | 2024-10-24 20:17:45 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int N=1e6+100;
int dp[N][2],a[N],n,T,k,u,v,ans;
int E,head[N],to[N<<1],nxt[N<<1];
int read(){
int flag=1,x=0,c=getchar();
while (c<'0' || c>'9'){if (c=='-') flag=-1;c=getchar();}
while (c>='0' && c<='9') x=x*10+c-48,c=getchar();
return x*flag;
}
void addedge(int u,int v){
to[++E]=v;
nxt[E]=head[u];
head[u]=E;
}
void dfs(int u,int F){
int t=a[u]&1;
dp[u][t]=a[u];dp[u][t^1]=0;
for (int e=head[u];e;e=nxt[e]){
int v=to[e];
if (v==F) continue;
dfs(v,u);
int u0=dp[u][0]+dp[v][0];
if (dp[u][1]!=0 && dp[v][1]!=0) u0=max(u0,dp[u][1]+dp[v][1]);
//int u0=max(dp[u][0]+dp[v][0],dp[u][1]+dp[v][1]);
int u1=0;
if (dp[u][1]!=0) u1=max(u1,dp[u][1]+dp[v][0]);
if (dp[v][1]!=0) u1=max(u1,dp[u][0]+dp[v][1]);
//int u1=max(dp[u][1]+dp[v][0],dp[u][0]+dp[v][1]);
dp[u][0]=u0;dp[u][1]=u1;
}
//cout<<u<<' '<<dp[u][0]<<" "<<dp[u][1]<<endl;
if (dp[u][k&1]>=k){
ans++;
dp[u][0]=0;
dp[u][1]=0;
}
}
int main(){
T=read();
while (T--){
n=read();k=read();
E=0;ans=0;
for (int i=0;i<=n+10;i++) head[i]=0,dp[i][0]=0,dp[i][1]=0;
for (int i=0;i<=n*2+10;i++) to[i]=0,nxt[i]=0;
for (int i=1;i<=n;i++) a[i]=read();
for (int i=1;i<n;i++){
u=read();v=read();
addedge(u,v);addedge(v,u);
}
dfs(1,0);
printf("%d\n",ans);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 12068kb
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: 11984kb
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: 46ms
memory: 11964kb
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 2 0 0 2 2 0 1 0 0 0 0 3 3 0 0 1 1 2 1 2 0 4 0 1 1 0 1 0 0 1 5 0 1 1 1 0 1 1 1 1 1 2 0 1 1 1 0 3 1 0 1 0 0 4 0 0 0 1 1 0 0 1 0 2 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 1 2 0 0 2 0 0 1 0 0 0 0 0 ...
result:
wrong answer 73rd numbers differ - expected: '1', found: '2'