PAT A 1004. Counting Leaves (30)【vector+dfs】
题目链接:https://www.patest.cn/contests/pat-a-practise/1004
大意:输出按层次输出每层无孩子结点的个数
思路:vector存储结点,dfs遍历
#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn=1e2+;
int n,m,k,x,f[maxn],deep,y;
vector<int> t[maxn];
void init()
{
cin>>n>>m;
while(m--)
{
cin>>x>>k;
while(k--)
{
cin>>y;
t[x].push_back(y);
}
}
}
void dfs(int x,int dep)
{
deep=max(dep,deep);
if(t[x].size()==)f[dep]++;
for(int i=;i<t[x].size();i++)
dfs(t[x][i],dep+);
} int main()
{
init();
dfs(,);
for(int i=;i<=deep;i++)
printf("%s%d",i?" ":"",f[i]);
printf("\n");
return ;
}
PAT A 1004. Counting Leaves (30)【vector+dfs】的更多相关文章
- PAT Advanced 1004 Counting Leaves (30) [BFS,DFS,树的层序遍历]
题目 A family hierarchy is usually presented by a pedigree tree. Your job is to count those family mem ...
- PAT 1004 Counting Leaves (30分)
1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is t ...
- PAT 解题报告 1004. Counting Leaves (30)
1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is t ...
- 1004. Counting Leaves (30)
1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- PTA 1004 Counting Leaves (30)(30 分)(dfs或者bfs)
1004 Counting Leaves (30)(30 分) A family hierarchy is usually presented by a pedigree tree. Your job ...
- 1004 Counting Leaves (30分) DFS
1004 Counting Leaves (30分) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- PAT甲1004 Counting Leaves【dfs】
1004 Counting Leaves (30 分) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- PAT Advanced 1004 Counting Leaves
题目与翻译 1004 Counting Leaves 数树叶 (30分) A family hierarchy is usually presented by a pedigree tree. You ...
- 【PAT Advanced Level】1004. Counting Leaves (30)
利用广度优先搜索,找出每层的叶子节点的个数. #include <iostream> #include <vector> #include <queue> #inc ...
随机推荐
- spring boot redis缓存JedisPool使用
spring boot redis缓存JedisPool使用 添加依赖pom.xml中添加如下依赖 <!-- Spring Boot Redis --> <dependency> ...
- ios nsdataformatter奇怪的问题
用nsdataformatter在中文格式下测试, a 标识的是上午,下午,不是AM,pm. 我在24小时格式的机器上测试,存入了一个 时间,却解析不出来了! static NSString *Hom ...
- Django~NewProject and APP
New Project 1.新建 django-admin startproject mysite 2.运行 manage.py runserver 8080 New APP 1.manage.py ...
- ABAP 通过视图取数到内表函数
CALL FUNCTION 'VIEW_GET_DATA' EXPORTING view_name = 'V_TVBUR' TABLES data = ...
- Effective C++ -----条款06:若不想使用编译器自动生成的函数,就该明确拒绝
为驳回编译器自动提供的功能,可将相应的成员函数声明为private并且不予实现. 使用像Uncopyable这样的base class也是一种做法(即先声明一个基类,然后私有继承它).这其实有点像使用 ...
- Android studio 自定义打包APK名称
Android Studio打包应用默认生成的apk名称是:app-release.apk .如果我们要让生成的apk名跟我们版本包名有联系的话,那我们就要自定义生成的apk名了,要怎么做呢. 我们只 ...
- java获得本机IP,名称等
import java.net.InetAddress; import java.net.UnknownHostException; public class GetLocalIP { public ...
- August 28th 2016 Week 36th Sunday
What doesn't kill you makes you stronger. 那些没有彻底击败你的东西只会让你更强大. Where there is life, there is hope, a ...
- Javascript异步编程方法总结
现在我们有三个函数,f1, f2, f3 按正常的思路我们会这样写代码: function f1 (){}; function f2 (){}; function f3 (){}; //在这里调用函数 ...
- android viewPager 切换页面时防止fragment重新加载
把限制页面数设置成应用一共的页面数就可以了 ViewPager.setOffscreenPageLimit(3);