cf 580 c(邻接表+dfs)
来源:互联网 发布:安装ubuntu双系统黑屏 编辑:程序博客网 时间:2024/06/04 17:42
Kefa decided to celebrate his first big salary by going to the restaurant.
He lives by an unusual park. The park is a rooted tree consisting of n vertices with the root at vertex 1. Vertex 1 also contains Kefa's house. Unfortunaely for our hero, the park also contains cats. Kefa has already found out what are the vertices with cats in them.
The leaf vertices of the park contain restaurants. Kefa wants to choose a restaurant where he will go, but unfortunately he is very afraid of cats, so there is no way he will go to the restaurant if the path from the restaurant to his house contains more than m consecutivevertices with cats.
Your task is to help Kefa count the number of restaurants where he can go.
The first line contains two integers, n and m (2 ≤ n ≤ 105, 1 ≤ m ≤ n) — the number of vertices of the tree and the maximum number of consecutive vertices with cats that is still ok for Kefa.
The second line contains n integers a1, a2, ..., an, where each ai either equals to 0 (then vertex i has no cat), or equals to 1 (then vertex i has a cat).
Next n - 1 lines contains the edges of the tree in the format "xi yi" (without the quotes) (1 ≤ xi, yi ≤ n, xi ≠ yi), where xi and yi are the vertices of the tree, connected by an edge.
It is guaranteed that the given set of edges specifies a tree.
A single integer — the number of distinct leaves of a tree the path to which from Kefa's home contains at most m consecutive vertices with cats.
4 11 1 0 01 21 31 4
2
7 11 0 1 1 0 0 01 21 32 42 53 63 7
2
Let us remind you that a tree is a connected graph on n vertices and n - 1 edge. A rooted tree is a tree with a special vertex called root. In a rooted tree among any two vertices connected by an edge, one vertex is a parent (the one closer to the root), and the other one is a child. A vertex is called a leaf, if it has no children.
Note to the first sample test:The vertices containing cats are marked red. The restaurants are at vertices 2, 3, 4. Kefa can't go only to the restaurant located at vertex 2.
Note to the second sample test:
The restaurants are located at vertices 4, 5, 6, 7. Kefa can't go to restaurants 6, 7
//就是搜 。。 注意无向图 和根的度为1
#include <iostream>#include <stdio.h>#include <string.h>#include <cmath>#include <algorithm>#include <map>#include <queue>using namespace std;const int maxn=100000+10;const int inf=(1<<30);int res,m;int arr[maxn];bool vis[maxn];struct Node{ int from; int next;}edge[2*maxn];int pre[maxn],in[maxn];void add(int index,int u,int v){ edge[index].from=v; edge[index].next=pre[u]; pre[u]=index;}void dfs(int v,int num){ if(num>m)return; if(in[v]==1&&v!=1)res++; if(vis[v])return; vis[v]=1; for(int i=pre[v];i!=-1;i=edge[i].next) { int t=edge[i].from; if(arr[t]==1)dfs(t,num+1); else dfs(t,0); }}int main(){ int n; while(~scanf("%d%d",&n,&m)) { memset(pre,-1,sizeof(pre)); memset(vis,0,sizeof(vis)); memset(in,0,sizeof(in)); for(int i=1;i<=n;i++) scanf("%d",arr+i); int index=1; for(int i=0;i<n-1;i++) { int u,v; scanf("%d%d",&u,&v); add(index,u,v); index++; add(index,v,u); index++; in[u]++,in[v]++; } res=0; dfs(1,arr[1]); printf("%d\n",res); } return 0;}
- cf 580 c(邻接表+dfs)
- CF 616C The Labyrinth(dfs)
- CF 893C DFS
- Another Postman Problem (邻接表+DFS)
- hdu 4707 Pet(DFS && 邻接表)
- 图的BFS,DFS(邻接表)
- 数据结构之DFS(邻接表版)
- 邻接表—DFS
- 邻接表 DFS
- hdu1269(邻接表+dfs)
- DFS遍历邻接表
- 图的遍历--使用邻接表作为存储结构的遍历(DFS、BFS)C语言
- Codeforces Round #403 (Div. 2) C. Andryusha and Colored Balloons(DFS 邻接表 数据结构)
- CF-29C - Mail Stamps(DFS+离散化)
- CF#291 C Watto and Mechanism(Trie+dfs)
- CF Round #616 (A,B,C 字符串,贪心,DFS)
- hdu4707 Pet【DFS&邻接表】
- 邻接表的dfs遍历
- 将 Sublime 3 打造成 Python/Django IDE开发利器
- apache-archiva-2.2.0 密码过期问题的解决
- Git的使用和相关配置
- SQL SERVER 2008 配置管理器出现“远程过程调用失败”【0x800706be】解决办法
- YII2中Codeception的安装
- cf 580 c(邻接表+dfs)
- spring任务调度器quartz
- LeetCode-OJ
- Codeforces Round #320 (Div. 2)
- 新浪云Git代码仓库使用入门
- spring中常被忽视的注解 @Primary
- Qt实现等待提示框(图片和movie实现)
- 操作系统学习2
- 关于java中的各种关键字