1004. Counting Leaves (30)
来源:互联网 发布:淘宝账号注销手机号 编辑:程序博客网 时间:2024/06/05 18:06
分析:求树中每层的叶子节点数并输出。直接DFS。
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <vector>using namespace std;const int maxn = 110;vector<int> child[maxn];bool vis[maxn];int num[maxn];int deep;int m, n, k;void DFS(int root, int d) { int nch = child[root].size(); if(nch == 0) { deep = max(deep, d); num[d]++; return; } for(int i = 0; i < nch; ++i) { int u = child[root][i]; vis[u] = true; DFS(u, d+1); }}int main() { scanf("%d%d", &m, &n); for(int i = 0; i < n; ++i) { int fa; scanf("%d %d", &fa, &k); for(int j = 0; j < k; ++j){ int x; scanf("%d", &x); child[fa].push_back(x); } } deep = 0; memset(vis, 0, sizeof(vis)); vis[1] = true; DFS(1, 0); for(int i = 0; i < deep+1; ++i) { printf("%d%c", num[i], i == deep ? '\n' : ' '); } return 0;}
0 0
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)
- redis-如何在工程中使用redis
- Mybatis入门程序一
- Linux下find指令和stat指令
- Ubuntu14.04下安装以及使用Git
- SQLAlchemy 嵌套事务的处理方式
- 1004. Counting Leaves (30)
- Hadoop学习:HDFS 机架感知
- @PathParam @QueryParam
- c++ primer 练习 1.13
- React—native+Android环境搭建和开发配置(Mac)
- python程序打包成exe
- The servlets named [x] and [y] are both mapped to the url-pattern [/JServlet] which is not permitted
- redisson实现分布式锁原理
- C++之静态成员变量和静态成员函数详解