题意:给出一棵树,共有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. org.springframework.orm.hibernate3.LocalSessionFactoryBean的疑惑解决办法

    在项目中使用了SSH框架(Struts2 + Spring3+ Hibernate3),applicationContext中配置了sessionFactory <bean id="s ...

  2. 写给iOS程序员的命令行使用秘籍

    http://www.jianshu.com/p/44d3b8f713f2 Mac OS是Unix系统的分支,有着强大的命令行功能.很多事情在命令行下处理会事半功倍,所以我就iOS程序员可能会用到的功 ...

  3. httpd设置HTTPS双向认证

    去年用tomcat.jboss配置过HTTPS双向认证,那时候主要用的是JDK自带的keytool工具.这次是用httpd + openssl,区别比较大 在网上搜索了很多文章,发现全面介绍的不多,或 ...

  4. while练习:输入一个班级的人数,然后依次输入学员成绩,计算班级学员的平均成绩和总成绩。

    Console.WriteLine("请输入班级的总人数:"); int count = int.Parse(Console.ReadLine()); ;//声明一个循环变量来记录 ...

  5. Oracle 11g ORA-00845: MEMORY_TARGET not supported on this system

    启动Oracle 11gR2后报错:ORA-00845 rac1:/home/oracle> sqlplus / as sysdba; SQL*Plus: Release 11.2.0.3.0 ...

  6. Java初学(六)

    一.final(最终)可以修饰类.方法.变量 特点:final修饰类,该类不能被继承 final修饰方法,该方法不能被重写(覆盖.重载.复写)        final修饰变量,该变量不能被重新赋值. ...

  7. 使用prompt输入一句英文句子和排序方式(升/降),将所有单词按排序方式排序后在网页上输出

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. spring bean id和bean name的区别

    今天在分析问题时发现一个大家平时都不太注意的spring 配置问题,发出来分享下: 首先澄清一个概念: 同名bean:多个bean 有相同的 name 或者 id,称之为同名bean <bean ...

  9. [转载]如何在Ubuntu上安装LAMP服务器系统

    [2013年7月25日 51CTO外电头条]为何应该在Ubuntu上安装LAMP服务器?从事Web开发工作时,我更偏爱在不受干扰的情况下,在我那台计算机上的开发环境下进行开发.我宁愿所犯的错误大部分是 ...

  10. R-squared是什么意思

    在回归分析中,R-squared值应该为多大? 就像经常被问到,在回归分析中,R平方应该为多大才表示回归模型是好的?我经常能够听到这类问题,在没回答这个问题之前,我会解释如 何来解释R平方值,我也会阐 ...