PAT甲級真題(並查集)——1004 Counting Leaves (30 分)
1004 Counting Leaves (30 分)
A family hierarchy is usually presented by a pedigree tree. Your job is to count those family members who have no child.
Input Specification:
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.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 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 Input:
2 1
01 1 02
Sample Output:
0 1
題目大意:
輸出樹每一行的非葉子節點數
題目解析:
拿到這道題,想到可以用並查集做,開闢三個陣列
- bool nonleaf[MAXN]//記錄節點是否為葉子節點,初值全為false
- int parent[MAXN]//記錄每個節點的父節點序號
- int count[MAXN]//儲存每一行的非葉子節點數目,初值為0
每一行讀入的資料,開頭的ID為非葉子節點,故nonleaf[ID]設為true,後面的節點全為ID的子節點,parent[序號]=ID;
遍歷節點陣列,遞迴獲取所有nonleaf值為false所在的行號line,對應的count[line]++,同時記錄最大行號maxline;
然後輸出即可。
具體程式碼:
#include<iostream>
using namespace std;
#define MAXN 110
bool nonleaf[MAXN]={false};//noleaf[節點序號]==true表示為非葉子節點
int parent[MAXN];//記錄每個節點的父節點序號
int count[MAXN];//儲存每一行的非葉子節點數目
int getline(int n){//計算節點所在行號
if(parent[n]==0)
return 1;
return getline(parent[n])+1;
}
int main() {
int n,m;//n(0,100)為節點總數
parent[1]=0;//節點1為根節點
cin>>n>>m;
while(m--){
int pnode,num;//pnode為父節點,num為子節點個數
cin>>pnode>>num;
nonleaf[pnode]=true;//有子節點 ,所以為非葉子節點
while(num--){
int cnode;
cin>>cnode;
parent[cnode]=pnode;//pnode是cnode的父節點
}
}
fill(count,count+MAXN,0);//初始化
int maxline=1;//記錄最大行數
for(int i=1;i<=n;i++){
if(nonleaf[i]==false){//如果i是葉子節點,則計算它屬於哪一行
int line=getline(i);
count[line]++;//對應行的葉子結點數目加一
if(line>maxline)//若所在行數大於最大行,更新最大行
maxline=line;
}
}
//按要求輸出
for(int i=1;i<=maxline;i++){
cout<<count[i];
if(i!=maxline)
cout<<" ";
}
return 0;
}
發現此題可直接用vector陣列+dfs做,正向思維,程式碼如下:
#include<iostream>
#include<vector>
#define MAXN 110
using namespace std;
vector<int> v[MAXN];
int count[MAXN];
int maxdepth=-1;
void dfs(int index,int depth){
if(v[index].size()==0)//若是葉子節點
{
count[depth]++;
if(depth>maxdepth)
maxdepth=depth;
return;
}
for(int i=0;i<v[index].size();i++)
dfs(v[index][i],depth+1);
}
int main()
{
int n,m;
cin>>n>>m;
while(m--){
int ID,num;
cin>>ID>>num;
while(num--)
{
int k;
cin>>k;
v[ID].push_back(k);
}
}
fill(count,count+MAXN,0);
dfs(1,0);
for(int i=0;i<=maxdepth;i++)
{
cout<<count[i];
if(i!=maxdepth)
cout<<" ";
}
return 0;
}