1004. Counting Leaves (30)
来源:互联网 发布:我的世界mac版材质包 编辑:程序博客网 时间:2024/04/28 11: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
#include<iostream>#include<vector>using namespace std;struct node{int ID;int K;vector< int> child;};int main(){// 0 < N < 100, the number of nodes in a tree// M (< N), the number of non-leaf nodesint N,M;cin>>N>>M;vector<node>nodes;nodes.clear();node tempnode;for(int i=0;i<M;++i){tempnode.child.clear();cin>>tempnode.ID>>tempnode.K;int t;for(int j=0;j<tempnode.K;++j){cin>>t;tempnode.child.push_back(t);}nodes.push_back(tempnode);}vector< int>level;level.clear();level.push_back(1);//count the number of non-leaf nodes for every levelwhile(!level.empty()){int num=level.size();vector< int>temp(level);level.clear();//judge if they are non-leaf nodes for all nodes in this levelfor(int i=0;i<temp.size();++i){//seach if this node has childfor(int j=0;j<nodes.size();++j){if(nodes.at(j).ID==temp.at(i)){--num;for(int k=0;k<nodes.at(j).K;++k)level.push_back(nodes.at(j).child.at(k));}}}if(level.empty())cout<<num;else cout<<num<<' ' ;}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)
- Android学习笔记(三)之带有侧边索引的快速查找(跟带字母索引查找的通讯录差不多)
- poj--2706--Connect(极限大模拟)
- 解析SoapObject对象的一点心得发布:
- Oracle命名规范归纳
- 鸡兔同笼
- 1004. Counting Leaves (30)
- LeetCode --- 19. Remove Nth Node From End of List
- 谱聚类算法(Spectral Clustering)
- LeetCode --- 20. Valid Parentheses
- 请保持光棍的节操 寒假手册一封颇具内涵的长信`
- UItableViewCell选中时的颜色及tableviewCell的select和deselect
- Project-Euler problem 1-50
- 记一次log4j日志文件小事故
- WV.21-大数阶乘算法1-序