Codeforces Round #321 (Div. 2) 580C - Kefa and Park
2015-09-24 20:45
323 查看
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 consecutive vertices with cats.
Your task is to help Kefa count the number of restaurants where he can go.
介个题目 ,窝错了很多很多次;
最开始的时候以为这个树是直接按图的顺序来的,也就是说不会出现 V 连 1 只有1 连 v ,可是 窝还是太native。
然后wrong了,蓝后,窝决定记双向联通,以为这样酱紫就不怕,蓝后,窝在标记点的时候有逗逼了,果断wrong。在这期间各种小错误不断,wrong了不止多少发
介个题目其实就是给个树, 树上不能出现连续超过M只猫,于是乎 建好图 ,收索就好 ,不深搜还是广搜都行#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct Edge{
int u,v,next;
};
Edge g[200200];
int a[100100];
int ans;
int first[100100];
int vis[100100];
int num[100100];
int n,m;
void addedge(int u,int v,int i)
{
g[i].u = u;
g[i].v = v;
g[i].next = first[u];
first[u] = i;
num[u]++;
}
void dfs(int u,int c)
{
vis[u] = 1;
if(num[u] == 1 && c <= m && u!=1){
// printf("%d\n",u);
ans ++;
return;
}
for(int i = first[u];i!=-1; i = g[i].next)
{
if(vis[g[i].v]) continue;
// printf(" %d %d %d\n",u,g[i].v,c);
vis[g[i].v] = 1;
if(a[g[i].v] == 0)
dfs(g[i].v,0);
else
{
if((c + 1 )<= m){
dfs(g[i].v,c+1);
}
}
}
return;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i = 1; i <= n; i++) { first[i] = -1; vis[i] = 0;num[i] = 0;}
for(int i = 1; i <= n; i++)
scanf("%d",&a[i]);
for(int i = 1; i <= n - 1; i++)
{
int u ,v;
scanf("%d%d",&u,&v);
addedge(u,v,i);
addedge(v,u,i+n-1);
}
ans = 0;
dfs(1,a[1]);
printf("%d\n",ans);
}
return 0;
}
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 consecutive vertices with cats.
Your task is to help Kefa count the number of restaurants where he can go.
介个题目 ,窝错了很多很多次;
最开始的时候以为这个树是直接按图的顺序来的,也就是说不会出现 V 连 1 只有1 连 v ,可是 窝还是太native。
然后wrong了,蓝后,窝决定记双向联通,以为这样酱紫就不怕,蓝后,窝在标记点的时候有逗逼了,果断wrong。在这期间各种小错误不断,wrong了不止多少发
介个题目其实就是给个树, 树上不能出现连续超过M只猫,于是乎 建好图 ,收索就好 ,不深搜还是广搜都行#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct Edge{
int u,v,next;
};
Edge g[200200];
int a[100100];
int ans;
int first[100100];
int vis[100100];
int num[100100];
int n,m;
void addedge(int u,int v,int i)
{
g[i].u = u;
g[i].v = v;
g[i].next = first[u];
first[u] = i;
num[u]++;
}
void dfs(int u,int c)
{
vis[u] = 1;
if(num[u] == 1 && c <= m && u!=1){
// printf("%d\n",u);
ans ++;
return;
}
for(int i = first[u];i!=-1; i = g[i].next)
{
if(vis[g[i].v]) continue;
// printf(" %d %d %d\n",u,g[i].v,c);
vis[g[i].v] = 1;
if(a[g[i].v] == 0)
dfs(g[i].v,0);
else
{
if((c + 1 )<= m){
dfs(g[i].v,c+1);
}
}
}
return;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i = 1; i <= n; i++) { first[i] = -1; vis[i] = 0;num[i] = 0;}
for(int i = 1; i <= n; i++)
scanf("%d",&a[i]);
for(int i = 1; i <= n - 1; i++)
{
int u ,v;
scanf("%d%d",&u,&v);
addedge(u,v,i);
addedge(v,u,i+n-1);
}
ans = 0;
dfs(1,a[1]);
printf("%d\n",ans);
}
return 0;
}
相关文章推荐
- POJ 3318 Matrix Multiplication(JAVA生成随机数)
- 【UI初级--连载八】-------------表视图(UITableView)
- 关于使用百度地图过程中遇到的一些问题(一)
- 间隔K翻转链表
- 不是每个人都适合linux
- Intel 的面试经历中国研究院
- Java基础知识强化68:基本类型包装类之Character概述和Character常见方法
- 继承Thread类创建多线程
- 注解驱动的springMVC与参数绑定
- Redis 命令 - Lists
- 自定义Dialog,并设定Dialog的大小以及位置
- MBProgressHUD的简单应
- c# 基本知识 ref 和 out
- 编译原理之常数处理机(一)
- 互联网创业必备工具盘点
- Fork and Join: Java也可以轻松地编写并发程序
- CDOJ 92 Journey LCA乱搞
- 关于通过get方法请求json数据出现数据不全的解决方案
- MAT分析dump文件
- HDU 2955 Robberies (01背包dp)