题意:给出一棵树,共有n个节点,其中根节点是Kefa的家,叶子是restaurant,a[i]....a[n]表示i节点是否有猫,问:Kefa要去restaurant并且不能连续经过m个有猫的节点有多少条路径;

思路:先用vector数组建树; 再dfs..(第一次用vector建树,还看了别人的代码,果然是zz);

代码:

 #include <bits/stdc++.h>
#define ll long long
#define MAXN 100000+10
using namespace std; vector<int>tree[MAXN];
int a[MAXN], vis[MAXN], n, k;
ll ans; void dfs(int cnt, int num)
{
if(vis[cnt]||num>k) return;
if(tree[cnt].size()==&&cnt!=) ans++;
vis[cnt]++;
for(int i=; i<tree[cnt].size(); i++)
{
if(!vis[tree[cnt][i]])
{
if(!a[tree[cnt][i]]) dfs(tree[cnt][i], );
else dfs(tree[cnt][i], num+);
}
}
} int main(void)
{
memset(vis, , sizeof(vis));
cin >> n >> k;
for(int i=; i<=n; i++)
{
tree[i].clear();
cin >> a[i];
}
for(int i=; i<n; i++)
{
int x, y;
cin >> x >> y;
tree[x].push_back(y);
tree[y].push_back(x);
}
ans=;
dfs(, a[]);
cout << ans << endl;
return ;
}

Codeforces Round #321 (Div. 2)C(tree dfs)的更多相关文章

  1. Codeforces Round #499 (Div. 1) F. Tree

    Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...

  2. Codeforces Round #321 (Div. 2) C. Kefa and Park dfs

    C. Kefa and Park Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/580/probl ...

  3. Codeforces Round #540 (Div. 3) F1. Tree Cutting (Easy Version) 【DFS】

    任意门:http://codeforces.com/contest/1118/problem/F1 F1. Tree Cutting (Easy Version) time limit per tes ...

  4. Codeforces Round #527 (Div. 3) F. Tree with Maximum Cost 【DFS换根 || 树形dp】

    传送门:http://codeforces.com/contest/1092/problem/F F. Tree with Maximum Cost time limit per test 2 sec ...

  5. Codeforces Round #316 (Div. 2) D. Tree Requests dfs序

    D. Tree Requests time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  6. Codeforces Round #321 (Div. 2) C dfs处理(双向边叶子节点的判断)

    C. Kefa and Park time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  7. Codeforces Round #646 (Div. 2) E. Tree Shuffling dfs

    题意: 给你n个节点,这n个节点构成了一颗以1为树根的树.每一个节点有一个初始值bi,从任意节点 i 的子树中选择任意k个节点,并按他的意愿随机排列这些节点中的数字,从而产生k⋅ai 的成本.对于一个 ...

  8. Educational Codeforces Round 6 E. New Year Tree dfs+线段树

    题目链接:http://codeforces.com/contest/620/problem/E E. New Year Tree time limit per test 3 seconds memo ...

  9. Codeforces Round #540 (Div. 3)--1118F1 - Tree Cutting (Easy Version)

    https://codeforces.com/contest/1118/problem/F1 #include<bits/stdc++.h> using namespace std; in ...

随机推荐

  1. Android 使用Parcelable序列化对象

    转:http://ipjmc.iteye.com/blog/1314145       Android序列化对象主要有两种方法,实现Serializable接口.或者实现Parcelable接口.实现 ...

  2. Codeforces 650C Table Compression

    传送门 time limit per test 4 seconds memory limit per test 256 megabytes input standard input output st ...

  3. java可变参数例子:求学生成绩信息,不确定课程数

    可变参数特点: 1)...只能出现在参数列表的最后2)...位于变量类型和变量名之间3)调用可变参数的方法时,编译器为该可变参数隐含创建一个数组,在方法体中以数组的形式访问可变参数 //可变参数也可用 ...

  4. 用过SQL语句获取织梦DedeCMS每个栏目各有多少文章

    我对SQL语句不是很精通,这个SQL调用语句是我在一个模板里面看到了,特来和大家分享,大家在制作模板的过程中有需要可以用得到.       显示效果: 共有会员:31 名       本月更新:39 ...

  5. ALTER 语句修改数据表

    1.修改数据表名:alter table 表名 rename 新表名; 2.修改列名: alter table 表名 change 列名 新列名(可以与旧的一样) 类型 默认值; 3.修改类型: al ...

  6. oracle的主键

    1.关于主键:在建表时指定primary key字句即可:create table test( id number(6) primary key, name varchar2(30));如果是对于已经 ...

  7. [整理]通过AngularJS directive对bootstrap日期控件的的简单包装

    最近项目上了AngularJS,而原来使用的日期控件的使用方式也需要改变,于是开始了倒腾,看了官方的例子,可以使用AngularJS的directive做简单的处理,这样在html里直接使用申明的的形 ...

  8. AJAX创建表格,删除数据

    主页面 <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8 ...

  9. Python打包程序

    到py2exe的官网下载程序,注意对应的python版本,比如所用的python版本为2.7,那么就下载适配2.7版本的py2exe软件 下载完成后安装(与普通软件安装方式相同) 2 将要转换的pyt ...

  10. java笔记--关于克隆技术

    关于克隆 --如果朋友您想转载本文章请注明转载地址"http://www.cnblogs.com/XHJT/p/3884817.html"谢谢-- 1.假克隆 如: ObjectA ...