题目链接:
http://codeforces.com/contest/580/problem/C
题意:
给你一棵树,然后每个叶子节点会有一家餐馆
你讨厌猫,就不会走有连续超过m个节点有猫的路
然后问你最多去几家饭店
题解:
dfs 不能写成单向边 会WA9
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MS(a) memset(a,0,sizeof(a))
#define MP make_pair
#define PB push_back
const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
inline ll read(){
ll x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
//////////////////////////////////////////////////////////////////////////
const int maxn = 1e5+10;
int n,m,cat[maxn],vis[maxn],ans;
vector<int> G[maxn];
void dfs(int u,int fa,int cnt){
for(auto v : G[u]){
if(v == fa) continue; // 不能往回走 也就是 不能等于父节点
if(cat[v]==0){
if(G[v].size()==1)
ans++;
dfs(v,u,0);
}else if(cnt+1<=m){
if(G[v].size()==1)
ans++;
dfs(v,u,cnt+1);
}
}
}
int main(){
n=read(),m=read();
for(int i=1; i<=n; i++){
cat[i] = read();
}
for(int i=1; i<n; i++){
int u=read(),v=read();
G[u].push_back(v);
G[v].push_back(u);
}
dfs(1,0,cat[1]);
cout << ans << endl;
return 0;
}