多叉樹の階層順は、+統計的な多叉葉っぱノード1004 Counting Leaves(30分)を通過する.
A family hierarchy is usually presented by a pedigree tree.Your job is to count those family memberss who haveのchild.
Input Specification:Each input file contains one test case.Each case starts with a line containing 0
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 sequence of two-digit ID’s of its children.For the sake of simplity,let fix the the root ID.
The input ends with N being 0.That case must NOT be processed.
Output Specification:For each test case,You are supposed to count those family members who haveのchild foreve seniorty level starting from the root.The numbers muse must printed in a line,sepated bye the spenatlast.ine.ine.ine.ine.ine.ine.spree.ine.ine.sepatest.ine.ine.ine.ine.spred.ine.spree.spree.spre
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.The n we shoutput 0 1 in a line.
Sample Input:
1.入力関数は、与えられたデータが各ノードのサブノードであるため、チェーン式に各ノードを格納するのが良い.だからvectorタイプを使います.
Input Specification:Each input file contains one test case.Each case starts with a line containing 0
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 sequence of two-digit ID’s of its children.For the sake of simplity,let fix the the root ID.
The input ends with N being 0.That case must NOT be processed.
Output Specification:For each test case,You are supposed to count those family members who haveのchild foreve seniorty level starting from the root.The numbers muse must printed in a line,sepated bye the spenatlast.ine.ine.ine.ine.ine.ine.spree.ine.ine.sepatest.ine.ine.ine.ine.spred.ine.spree.spree.spre
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.The n we shoutput 0 1 in a line.
Sample Input:
2 1
01 1 02
Sample Output:0 1
問題の解決は、マルチツリー非リーフノードのサブノードを与え、各階層のリーフノードの数を統計して出力する.1.入力関数は、与えられたデータが各ノードのサブノードであるため、チェーン式に各ノードを格納するのが良い.だからvectorタイプを使います.
#include
#include
#include
using namespace std;
vector<vector<int>> Tree(101);
int N,M;
void input()
{
cin>>N>>M;
int K;
int ID;
int tmp;
for(int i=0;i<M;i++)
{
cin>>ID>>K;
for(int j=0;j<K;j++)
{
cin>>tmp;
Tree[ID].push_back(tmp);
}
}
// Tree ;
}
2.マルチクロスツリーを遍歴することは、各層の葉節点数が必要なので、順序を使って遍歴したほうがいいです.隊列を構築し、段階的に入隊し、列を出る時にチームの要素が層尾かどうかを判断し、もしその層の葉節の点数を統計して、結果配列の中に預け入れます.列の要素にサブノードがない場合、その層のリーフノード数+1;vector<int> result;
void Traverse()
{
queue<int> T;
T.push(1);
//
int Leaf=0; //
int End=1; //
int temp; //
while(!T.empty())
{
int a=T.front();
T.pop();
if(Tree[a].size()==0) ++Leaf;
// a
else
{
for(int i=0;i<Tree[a].size();i++)
T.push(Tree[a][i]);
temp = Tree[a][Tree[a].size()-1];
}
if(a==End){
result.push_back(Leaf);
End=temp;
Leaf=0;
}
}
}
3.main関数は、規則的な暑さと結果のリストを押せばいいです.int main()
{
input();
Traverse();
if(result.size()>0)
cout<<result[0];
for(int i=1;i<result.size();i++)
cout<<" "<<result[i];
}
多叉樹の子木を総括するには、連鎖構造の保存が必要で、層が多叉樹を遍歴するのにより、層順に遍歴したほうがいいです.順番を巡回すると同時に必要です. int End=1; //
int temp; //
2つの変数のサポート;