A family hierarchy is usually presented by a pedigree tree where all the nodes on the same level belong to the same generation. Your task is to find the generation with the largest population.
Input Specification:
Each input file contains one test case. Each case starts with two positive integers N (<100) which is the total number of family members in the tree (and hence assume that all the members are numbered from 01 to N), and M (<N) which is the number of family members who have children. Then M lines follow, each contains the information of a family member in the following format:
ID K ID[1] ID[2] ... ID[K]
where ID is a two-digit number representing a family member, K (>0) is the number of his/her children, followed by a sequence of two-digit ID's of his/her children. For the sake of simplicity, let us fix the root ID to be 01. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print in one line the largest population number and the level of the corresponding generation. It is assumed that such a generation is unique, and the root level is defined to be 1.
Sample Input:
23 13
21 1 23
01 4 03 02 04 05
03 3 06 07 08
06 2 12 13
13 1 21
08 2 15 16
02 2 09 10
11 2 19 20
17 1 22
05 1 11
07 1 14
09 1 17
10 1 18
结尾无空行
Sample Output:
9 4
结尾无空行
这一题我用BFS进行处理,一开始出错了,2、3测试点没过,原因出在BFS函数中,当队列为空时,我直接return了,跳过了对最后一层的计算。正确的应该在最后进行一次计算。
就是这里:
if(temp_population > population){
population = temp_population;
generation = cur_generation;
}
return;
测试用例:
7 3
1 2 2 3
2 2 4 5
3 2 6 7
答案:
3 4
这里发生了一个插曲,就是我在出错的时候,懒得找用例,直接把错的代码复制到牛客网对应的题目25,结果全部通过(现在已修复)
#include <iostream>
#include <vector>
#include <queue>
#include <sstream>
using namespace std;
struct node{
int depth;
vector<int> child;
};
vector<node> tree;
int population = 0,generation;
void BFS(){
queue<int> tree_index;
tree_index.push(1);
int cur_generation = 1;
int temp_population = 0;
tree[1].depth = 1;
while(!tree_index.empty()){
int cur_index = tree_index.front();
tree_index.pop();
if(tree[cur_index].depth != cur_generation){
if(temp_population > population){
population = temp_population;
generation = cur_generation;
}
cur_generation = tree[cur_index].depth;
temp_population = 0;
}
temp_population++;
if(tree[cur_index].child.size() != 0){
for(int i = 0;i < tree[cur_index].child.size();++i){
tree[tree[cur_index].child[i]].depth = cur_generation + 1;
tree_index.push(tree[cur_index].child[i]);
}
}
}
if(temp_population > population){
population = temp_population;
generation = cur_generation;
}
return;
}
int main(){
int n,m;
cin >> n >> m;
tree.resize(n + 1);
for(int i = 0;i < m;++i){
int father,children;
int num;
cin >> father >> num;
tree[father].depth = -1;
for(int j = 0;j < num;++j){
cin >> children;
tree[father].child.push_back(children);
}
}
BFS();
cout << population << ' ' << generation << endl;
return 0;
}
上面的太长了,
柳神代码,先计算每个点所在代,最后再统计
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int main(){
int n,m;
cin >> n >> m;
vector<vector<int>> tree(n + 1);
vector<int> level(n + 1,0);
vector<int> book(n + 1,0);
for(int i = 0;i < m;++i){
int father,num;
cin >> father >> num;
for(int j = 0;j < num;++j){
int child;
cin >> child;
tree[father].push_back(child);
}
}
level[1] = 1;
queue<int> tree_index;
tree_index.push(1);
while(!tree_index.empty()){
int cur_index = tree_index.front();
tree_index.pop();
book[level[cur_index]]++;
for(int i = 0;i < tree[cur_index].size();++i){
level[tree[cur_index][i]] = level[cur_index] + 1;
tree_index.push(tree[cur_index][i]);
}
}
int max_population = 0,generation;
for(int i = 0;i < book.size();++i){
if(book[i] > max_population){
max_population = book[i];
generation = i;
}
}
cout << max_population << ' ' << generation << endl;
return 0;
}