PAT (Advanced Level) Practise 1004 Counting Leaves
来源:互联网 发布:unity3d能做帧动画吗 编辑:程序博客网 时间:2024/05/16 08:00
1004. Counting Leaves (30)
Input
Each input file contains one test case. Each case starts with a line containing 0 < N < 100, the number of nodes in a tree, and M (< N), the number of non-leaf nodes. Then M lines follow, each in the format:
ID K ID[1] ID[2] ... ID[K]where ID is a two-digit number representing a given non-leaf node, K is the number of its children, followed by a sequence of two-digit ID's of its children. For the sake of simplicity, let us fix the root ID to be 01.
Output
For each test case, you are supposed to count those family members who have no child for every seniority level starting from the root. The numbers must be printed in a line, separated by a space, and there must be no extra space at the end of each line.
The sample case represents a tree with only 2 nodes, where 01 is the root and 02 is its only child. Hence on the root 01 level, there is 0 leaf node; and on the next level, there is 1 leaf node. Then we should output "0 1" in a line.
Sample Input2 101 1 02Sample Output
0 1
解题思路:dfs
#include <iostream>#include <cstdio>#include <cmath>#include <cstring>#include <algorithm>#include <stack>#include <queue>#include <vector>#include <map>using namespace std;int n,m,k,f[109],sum;vector<int>G[109];void dfs(int x,int y){ sum=max(sum,y); if (!G[x].size()) f[y]++; for (int i=0; i<G[x].size(); i++) dfs(G[x][i],y+1);}int main(){ scanf("%d%d",&n,&m); memset(f,0,sizeof f); for(int i=0;i<m;i++) { int u,v; scanf("%d %d",&u,&k); for(int j=0;j<k;j++) { scanf("%d",&v); G[u].push_back(v); } } sum=0; dfs(1,0); printf("%d",f[0]); for (int i=1; i<=sum; i++) printf(" %d",f[i]); printf("\n"); return 0;}
- PAT (Advanced Level) Practise 1004 Counting Leaves
- PAT (Advanced Level) Practise 1004 Counting Leaves (30)
- 浙大PAT (Advanced Level) Practise 1004. Counting Leaves (30)
- 1004. Counting Leaves (30)——PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise 1004. Counting Leaves (30)
- 浙大PAT (Advanced Level) Practise 1004. Counting Leaves (30)
- PAT (Advanced Level) Practise 1004. Counting Leaves (30)
- PAT (Advanced Level) Practise 1004. Counting Leaves (30)
- Pat(Advanced Level)Practice--1004(Counting Leaves)
- PAT Advanced Level 1004:Counting Leaves
- PAT(Advanced Level) 1004 Counting Leaves 解题报告
- 【PAT Advanced Level】1004. Counting Leaves (30)
- 浙大 PAT Advanced level 1004. Counting Leaves
- PAT (Advanced Level) 1004. Counting Leaves (30)
- 【PAT】【Advanced Level】1004. Counting Leaves (30)
- [PAT (Advanced Level) ]1004. Counting Leaves解题文档
- PAT (Advanced Level) 1004. Counting Leaves (30) 解题报告
- [PAT] (Advanced Level) Practise
- openssl的证书申请
- Android Studio 出现Failed to open zip file的问题
- 欢迎使用CSDN-markdown编辑器
- 在一个千万级的数据库中查询,如何提高查询效率
- 第一个task
- PAT (Advanced Level) Practise 1004 Counting Leaves
- [BZOJ4278][ONTAK2015]Tasowanie(后缀数组+贪心)
- css----总结1
- L1-006. 连续因子
- kalman
- 敏捷团队中测试人员比率
- Android之用tcpdump常用抓包命令使用总结
- React Native 之 TextInput使用
- hdu2553 n皇后问题 dfs搜索 记忆化