-
Notifications
You must be signed in to change notification settings - Fork 139
/
Graphs
80 lines (76 loc) · 1.53 KB
/
Graphs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <bits/stdc++.h>
using namespace std;
int ans=0;
class Graph
{
int nodes;
list<int> *adj;
public:
Graph(int n)
{
nodes=n;
adj=new list<int>[nodes];
}
void edge(int u,int v)
{
adj[u].push_back(v);
adj[v].push_back(u);
}
void DFS(int v,bool visited[],int landmine[],int l,int c)
{
visited[v]=true;
if(v==0&&landmine[v])
c=c+1;
list<int>::iterator i;
if(c>l)
return;
if(adj[v].size()==1&&c<=l&&visited[*(adj[v].begin())])
{
ans++;
if(landmine[v])
c=c-1;
}
for(i=adj[v].begin();i!=adj[v].end();i++)
{
if(!visited[*i]&&landmine[*i])
{
list<int>:: iterator j;
int flag=0;
for(j=adj[*i].begin();j!=adj[*i].end();j++)
{
if(landmine[*j])
{
flag=1;
DFS(*i,visited,landmine,l,c+1);
break;
}
}
if(flag==0)
DFS(*i,visited,landmine,l,c);
}
else if(!visited[*i]&&landmine[*i]==0)
DFS(*i,visited,landmine,l,0);
}
}
};
int main()
{
int n,l;
cin>>n>>l;
int* landmine=new int[n];
Graph g(n);
for(int i=0;i<n;i++)
cin>>landmine[i];
for(int i=0;i<n-1;i++)
{
int u,v;
cin>>u>>v;
g.edge(u,v);
}
bool* visited= new bool[n];
for(int i=0;i<n;i++)
visited[i]=false;
g.DFS(0,visited,landmine,l,0);
cout<<ans<<endl;
return 0;
}