如果说要A和B相连,要把这个图储存,那么用到vector 数组,如果没有权,直接vectorG[maxn];G[x]push_back(y);G[y].push_back(x).如果有权,先定义一个结构体数组,vector《node>G[maxn];G[x].push_back(node(y,cost);G[y].push_baxk(node(x,cost);
#include <stdio.h>
#include <iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<cstring>
using namespace std;
const int maxn = 2e5;
int m,n;
int a[maxn];
int ans;
int vis[maxn];
vector<int>G[maxn];
int dfs(int cur,int sum)
{
vis[cur] = 1;
if(sum > n)
return 0;
int flag = 0;
for(int i = 0; i < G[cur].size(); i++)
{
int v = G[cur][i];
if(vis[v] == 0)
{
flag = 1;
if(a[v] == 1)
dfs(v,sum+a[v]);
else
dfs(v,0);
}
}
if(flag == 0)
ans++;
}
int main()
{
while(scanf("%d%d",&m,&n)!=EOF)
{
memset(vis,0,sizeof(vis));
for(int i = 1; i <= m; i++)
{ G[i].clear();
scanf("%d",&a[i]);
}
for(int i = 1; i < m; i++)
{
int x,y;
scanf("%d%d",&x,&y);
G[x].push_back(y);
G[y].push_back(x);
}
ans = 0;
dfs(1,a[1]);
printf("%d\n",ans);
}
return 0;
}