1004. Counting Leaves (30)
来源:互联网 发布:淘宝第五大道是正品吗 编辑:程序博客网 时间:2024/06/05 05:26
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
对于我这种小白来说,每次都被题目的意思弄崩溃。所以……
这道题的意思就是这是一个家族树,给的N,M是树上所有的结点数,和不是叶子结点的结点数(non-leaf :不能翻译为没有叶子的结点 别问我是怎么知道的)
这里 ID 就是这个结点的代号 K就是他孩子的个数 后面就是这k个孩子的代号
输出要求:求出每一层鳏夫寡妇的个数(孩子结点的喽)
代码还是仿照网上大牛的方法写的:
代码核心就是深搜一下,深搜的层数就是当前结点的层数,定义一个全局数组用来保存每一层寡妇的个数……
#include<cstdio>#include<map>#include<vector> #include<iostream>using namespace std;int levels[101];map<int,vector<int> > tree;//map :关联容器(注释给和我一样的小白瞅~不谈) void dfs(int id,int level)//深搜 {if(tree[id].empty())levels[level]++;else{vector<int>::iterator it =tree[id].begin();//迭代器 for(;it!=tree[id].end();it++)dfs(*it,level+1);}}int main(){int n,m,i,j,id1,id2,k,count;cin>>n>>m;for(i=0;i<m;i++){cin>>id1>>k;for(j=0;j<k;j++){cin>>id2;tree[id1].push_back(id2);//关联容器赋值的最懒法子~ }}dfs(1,0);cout<<levels[0];count=levels[0];for(i=1;count!=(n-m);i++){cout<<" "<<levels[i];count+=levels[i];}return 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)
- wordpress首页无法看到添加的目录
- springboot+bootstrap fileupinput 插件实现文件上传
- js父子页面相互调用变量及方法
- leetcode刷题-堆栈3
- 利用HttpUrlConnection 上传 接收文件的实现方法
- 1004. Counting Leaves (30)
- Etherlab源码解析----同步管理器SM配置(邮箱通信)
- 11.2笔记整理及作业
- input type=file 如何达到去除输入框效果
- java访问https请求
- 两个栈实现一个队列
- 服务器
- Unity3d 插件研究之EasyTouch插件
- jfinal之使用模板导出excel文件