【CF580C Kefa and Park】题解
题目链接
题目
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.
给定一棵 N 个节点的有根树(其中根节点始终为 1 号节点),点有点权,点权只有 1 和 0 两种,求从根节点到叶子节点的路径中,有多少条路径满足:路径上最大连续点权为 1 的节点个数不超过 M。
思路
暴力dfs一遍,记录已经连续多少个1.
如果超过 \(m\) 就return,如果当前点权为0则清零。
时间复杂度 \(O(n)\)。
Code
#include<bits/stdc++.h>
using namespace std;
//#define int long long
inline int read(){int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;
ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+
(x<<3)+(ch^48);ch=getchar();}return x*f;}
#define N 100010
//#define M
//#define mo
struct node
{
int x, y, n;
}d[N<<1];
int n, m, i, j, k, T;
int u, v, h[N], ans, a[N];
void cun(int x, int y)
{
d[++k].x=x; d[k].y=y;
d[k].n=h[x]; h[x]=k;
}
void dfs(int x, int fa, int s)
{
if(s>m) return ;
int g, y, f=1;
for(g=h[x]; g; g=d[g].n)
{
y=d[g].y;
if(y==fa) continue;
dfs(y, x, (a[y] ? s+1 : 0)); f=0;
}
if(f) ++ans;
}
signed main()
{
// freopen("tiaoshi.in","r",stdin);
// freopen("tiaoshi.out","w",stdout);
n=read(); m=read();
for(i=1; i<=n; ++i) a[i]=read();
for(i=2; i<=n; ++i)
{
u=read(); v=read();
cun(u, v); cun(v, u);
}
dfs(1, 0, a[1]);
printf("%d", ans);
return 0;
}
总结
很模板的一道题,不知洛谷为什么评蓝
此题运用的是边dfs边记录当前情况,不满足条件直接停掉。
这种思想可以在其他题目里应用。
本文来自博客园,作者:zhangtingxi,转载请注明原文链接:https://www.cnblogs.com/zhangtingxi/p/16195913.html