PAT (Advanced Level) 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
code
#include<cstdio>
#include<cstring>
struct Node
{
int father;
int level;
bool NoChild;
};
Node v[100]; //用以記錄樹的資訊
char level[100] = "\0"; //用以記錄樹每層葉子節點數
int main()
{
int N, M, c, ID, child, MAXLevel = 1;
scanf_s("%d%d", &N, & M);
//初始化
for (int i = 0; i<100; ++i)
{
v[i].father = 0;
v[i].level = 0;
v[i].NoChild = 1;
}
//獲取輸入第2行到第M行的資訊
for (int i = 0; i<M; ++i)
{
scanf_s("%d%d", &ID, &c);
v[ID].NoChild = 0;
for (int j = 0; j<c; ++j)
{
scanf_s("%d", &child);
v[child].father = ID;
}
}
v[1].level = 1; //根節點層數初始化為1
for (int i = 1; i <= N; ++i)
for (int j = 1; j <= N; ++j)
{
//如果一個節點的父節點是i
if (v[j].father == i)
{
v[j].level = v[i].level + 1;//那麼他的層數等於父節點層數+1
if (v[j].level>MAXLevel)
MAXLevel = v[j].level; //維護一個MAXLevel記錄最大層數
}
}
for (int i = 1; i <= N; ++i) //統計每一層的葉子節點數
if (v[i].NoChild == 1)
level[v[i].level]++;
//輸出
for (int i = 1; i<MAXLevel; ++i)
printf("%d ", level[i]);
printf("%d\n", level[MAXLevel]);
return 0;
}
思路
演算法來自於這篇部落格https://blog.csdn.net/qq278672818/article/details/54915636只對它的程式碼做了一點小改動和加了註釋,感謝博主的分享。本來是準備建一棵樹然後用廣度優先搜尋去做的,但是在測試點1和3上出現段錯誤,後來考慮到輸入的過程可能是亂序的,比如考慮構建如下一棵樹:
輸入為
7 4
01 1 02
02 3 03 04 05
05 1 06
06 1 07
這樣順序輸入就是對的,不會段錯誤。
但是輸入為
7 4
01 1 02
05 1 06
02 3 03 04 05
06 1 07
因為處理05 1 06時找不到05號節點而產生段錯誤
於是我對程式碼做了調整,在找不到父親節點時就新建一棵以這個節點為根的樹,加入trees的list,而且每次建子節點之前遍歷這個list看看是否有根節點ID與子節點ID相同的,如果有就把這兩棵樹連起來,但只對了測試點3,測試點1還是段錯誤,不知道哪裡還有問題。。
這是原始程式碼,錯測試點1和3。
#include <iostream>
#include <map>
#include <string>
#include <queue>
using namespace std;
struct Node
{
string id;
int level;
vector<Node*> children;
Node(string id, int level);
void getChild(string id, int plevel);
};
struct Tree
{
Node* root;
Node* find(string id);
Tree();
map<int, int> count();
};
Tree::Tree()
{
root = new Node("01", 0);
}
Node* Tree::find(string id)
{
queue<Node*> nodeQueue;
nodeQueue.push(root);
while (!nodeQueue.empty())
{
Node* node = nodeQueue.front();
nodeQueue.pop();
if (node->id == id)
return node;
else
{
for (size_t i = 0; i < node->children.size(); i++)
{
nodeQueue.push(node->children[i]);
}
}
}
return nullptr;
}
map<int, int> Tree::count()
{
map<int, int> countMap; // <level, num>
queue<Node*> nodeQueue;
nodeQueue.push(root);
while (!nodeQueue.empty())
{
Node* node = nodeQueue.front();
nodeQueue.pop();
if (!countMap.count(node->level))
countMap[node->level] = 0;
if (node->children.size() == 0)
countMap[node->level]++;
for (size_t i = 0; i < node->children.size(); i++)
{
nodeQueue.push(node->children[i]);
}
}
return countMap;
}
Node::Node(string id, int level)
{
this->id = id;
this->level = level;
}
void Node::getChild(string id, int plevel)
{
Node* childNode = new Node(id, plevel + 1);
children.push_back(childNode);
}
int main()
{
int numOfNode;
for (; cin >> numOfNode; )
{
if (!numOfNode) break;
Tree tree;
int numOfNonLeaf;
cin >> numOfNonLeaf;
for (size_t i = 0; i < numOfNonLeaf; i++)
{
string pid;
int k; // num of children
cin >> pid >> k;
Node* pNode = tree.find(pid);
for (int j = 0; j < k; j++)
{
string cid;
cin >> cid;
pNode->getChild(cid, pNode->level);
}
}
map<int, int> countMap = tree.count();
cout << countMap.begin()->second;
map<int, int>::iterator it = countMap.begin();
it++;
for (; it != countMap.end(); it++)
cout << " " << it->second;
cout << endl;
}
return 0;
}
這是修改版程式碼錯測試點1
#include <iostream>
#include <map>
#include <string>
#include <queue>
#include <list>
using namespace std;
struct Node
{
string id;
int level;
vector<Node*> children;
Node(string id, int level);
void getChild(string id, int plevel);
};
struct Tree
{
Node* root;
Node* find(string id);
Tree();
Tree(string id);
map<int, int> count();
void updateLevel();
};
Tree::Tree()
{
root = new Node("01", 0);
}
Tree::Tree(string id)
{
root = new Node(id, 0);
}
Node* Tree::find(string id)
{
queue<Node*> nodeQueue;
nodeQueue.push(root);
while (!nodeQueue.empty())
{
Node* node = nodeQueue.front();
nodeQueue.pop();
if (node->id == id)
return node;
else
{
for (size_t i = 0; i < node->children.size(); i++)
{
nodeQueue.push(node->children[i]);
}
}
}
return nullptr;
}
map<int, int> Tree::count()
{
map<int, int> countMap; // <level, num>
queue<Node*> nodeQueue;
nodeQueue.push(root);
while (!nodeQueue.empty())
{
Node* node = nodeQueue.front();
nodeQueue.pop();
if (!countMap.count(node->level))
countMap[node->level] = 0;
if (node->children.size() == 0)
countMap[node->level]++;
for (size_t i = 0; i < node->children.size(); i++)
{
nodeQueue.push(node->children[i]);
}
}
return countMap;
}
void Tree::updateLevel()
{
queue<Node*> nodeQueue;
nodeQueue.push(root);
while (!nodeQueue.empty())
{
Node* node = nodeQueue.front();
nodeQueue.pop();
for (size_t i = 0; i < node->children.size(); i++)
{
node->children[i]->level = node->level + 1;
nodeQueue.push(node->children[i]);
}
}
return;
}
Node::Node(string id, int level)
{
this->id = id;
this->level = level;
}
void Node::getChild(string id, int plevel)
{
Node* childNode = new Node(id, plevel + 1);
children.push_back(childNode);
}
int main()
{
int numOfNode;
for (; cin >> numOfNode; )
{
if (!numOfNode) break;
list<Tree> trees;
int numOfNonLeaf;
cin >> numOfNonLeaf;
for (size_t i = 0; i < numOfNonLeaf; i++)
{
string pid;
int k; // num of children
cin >> pid >> k;
Node* pNode = nullptr;
for (auto it : trees)
{
pNode = it.find(pid);
if (pNode != nullptr) break;
}
if (pNode == nullptr)
{
Tree tree(pid);
trees.push_back(tree);
pNode = tree.root;
for (int j = 0; j < k; j++)
{
string cid;
cin >> cid;
Node* cNode = nullptr;
list<Tree>::iterator it = trees.begin();
for (; it != trees.end(); it++)
{
if (it->root->id == cid)
{
cNode = it->root;
trees.erase(it);
break;
}
}
if (cNode != nullptr)
{
pNode->children.push_back(cNode);
}
else
{
pNode->getChild(cid, pNode->level);
}
}
}
else
{
for (int j = 0; j < k; j++)
{
string cid;
cin >> cid;
Node* cNode = nullptr;
list<Tree>::iterator it = trees.begin();
for (; it != trees.end(); it++)
{
if (it->root->id == cid)
{
cNode = it->root;
trees.erase(it);
break;
}
}
if (cNode != nullptr)
{
pNode->children.push_back(cNode);
}
else
{
pNode->getChild(cid, pNode->level);
}
}
}
}
trees.begin()->updateLevel();
map<int, int> countMap = trees.begin()->count();
cout << countMap.begin()->second;
map<int, int>::iterator it = countMap.begin();
it++;
for (; it != countMap.end(); it++)
cout << " " << it->second;
cout << endl;
}
return 0;
}
以上