这个 1500 的题这么水? 这还不如 1200、1300 的思维题
我开始没考虑周全,这题给出的连边没有讲都是从父节点连向子节点,所有要建双边。
#include <iostream>
#include <cstring>
using namespace std;
const int N = 1e5 + 10, M = N * 2;
typedef long long LL;
int e[M], ne[M], h[N], idx;
int n, m;
int st[N];
void add(int a, int b)
{
e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ;
}
int ans;
void dfs(int u, int dad, int sum)
{
if (sum > m) return ;
int flag = 1;
for (int i = h[u]; i != -1; i = ne[i])
{
int j = e[i];
if (j == dad) continue;
if (st[j] == 1)
{
flag = 0;
dfs(j, u, sum + 1);
}else dfs(j, u, 0), flag = 0;
}
if (flag == 1) ans ++ ;
}
int main()
{
cin >> n >> m;
for (int i = 1; i <= n; i ++ ) cin >> st[i];
memset(h, -1, sizeof h);
for (int i = 0; i < n - 1; i ++ )
{
int a, b;
cin >> a >> b;
add(a, b), add(b, a);
}
dfs(1, -1, st[1] == 1);
cout << ans;
return 0;
}
标签:idx,580C,Park,dfs,st,int,flag,节点
From: https://www.cnblogs.com/bzdydscjgw/p/17338009.html