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;
}
本文介绍了一种算法,用于帮助角色Kefa在一个充满猫的树形公园中选择合适的餐厅。公园由一系列节点组成,部分节点有猫居住,而叶子节点则是各餐厅所在位置。任务是在从餐厅到家的路上不超过特定数量的连续猫节点的情况下,计算Kefa可以前往的餐厅数量。
1343

被折叠的 条评论
为什么被折叠?



