QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#415550#7684. Sweet SugarxuzhihaodedieWA 112ms7816kbC++201.5kb2024-05-20 23:22:242024-05-20 23:22:24

Judging History

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

  • [2024-05-20 23:22:24]
  • 评测
  • 测评结果:WA
  • 用时:112ms
  • 内存:7816kb
  • [2024-05-20 23:22:24]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define PII pair<int,int>
#define lson 2*p
#define rson 2*p+1
#define x first
#define y second
#define endl "\n"
constexpr int N=1e6+10;
constexpr int mod=998244353;
const int INF=1e15;
int dp[N][2],c[N];
int n,k,ans;
vector<int> g[N];
void dfs(int x,int fa) {
	for(int y:g[x]) {
		if(y==fa) continue;
		dfs(y,x);
		if((dp[y][0]>=k&&k%2==0)||(dp[y][1]>=k&&k%2==1)) {
			ans++;
			continue;
		}
		int val0=dp[x][0],val1=dp[x][1];
		dp[x][0]=max(dp[x][0],val0+dp[y][0]);
		if(dp[y][1]!=-1) {
			dp[x][1]=max(dp[x][1],val0+dp[y][1]);
			if(val1!=-1) dp[x][0]=max(dp[x][0],val1+dp[y][1]);
		}
		if(val1!=-1) dp[x][1]=max(dp[x][0],val1+dp[y][0]);
	}
	int val0=dp[x][0],val1=dp[x][1];
	if(c[x]&1) {
		dp[x][1]=max(dp[x][1],val0+c[x]);
		if(val1!=-1) dp[x][0]=max(dp[x][0],val1+c[x]);
		else dp[x][0]=0;	
	} else {
		dp[x][0]=max(dp[x][0],val0+c[x]);
		if(val1!=-1) dp[x][1]=max(dp[x][1],val1+c[x]);
		else dp[x][1]=-1;
	}
}
void solve() {
	cin>>n>>k;
	for(int i=1;i<=n;i++) {
		cin>>c[i];
		g[i].clear();
		dp[i][0]=0,dp[i][1]=-1;
	}
	for(int i=1;i<n;i++) {
		int u,v;
		cin>>u>>v;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	ans=0;
	dfs(1,0);
	if((dp[1][0]>=k&&k%2==0)||(dp[1][1]>=k&&k%2==1)) ans++;
	cout<<ans<<endl;
}
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T=1;
    cin>>T;
    while(T--) {
        solve();
    }
}
/*

*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 7816kb

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: 7748kb

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: 112ms
memory: 7780kb

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
1
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 247th numbers differ - expected: '0', found: '1'