强烈推荐,刷PTA的朋友都认识一下柳神–PTA解法大佬
本文由参考于柳神博客写成
还有就是非常非常有用的 算法笔记 全名是
算法笔记 上级训练实战指南 //这本都是PTA的题解
算法笔记
PS 今天也要加油鸭
题目原文
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
生词如下:
PS:大体看懂了.但是不知道要我干啥.
就知道给了你一棵树.然后不知道要算啥
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
201/5000
对于每个测试用例,在一行中打印最大的人口数量和相应代的级别。 假定这样的世代是唯一的,并且根级别定义为1
这句话的意思,是你要算出每个层级之间的最多数
输出这个层级的数量,然后输出这个层级
题目大意:
就是给你一个树
然后是你要算出每个层级之间的最多数
输出这个层级的数量,然后输出这个层级
测试点2 的错误
就是只有2个节点的情况没有考虑到
测试点如下:
2 1
1 1 2
结果因该是1 1
代码如下:
#include<iostream>
#include <vector>
#include<algorithm>
using namespace std;
struct node {
int data;
int level;
vector<int> child;
};
vector<node> v;
vector<int> Nums;
void dfs(int index, int depth) {
Nums[depth]++;
if (v[index].child.size() == 0) return ;
for (int i = 0; i < v[index].child.size(); i++)
dfs(v[index].child[i], depth + 1);
}
int main() {
int n, m=0,t=0,num,num_node=0,root=0,fin_id=1,fin_num=1;
scanf("%d%d", &n, &m);
v.resize(n + 1); Nums.resize(n + 1,0);
for (int i = 0; i < m; ++i) {
scanf("%d%d",&num,&num_node);
for (int j = 0; j < num_node; ++j) {
scanf("%d", &t);
v[num].child.push_back(t);
}
}
dfs(1, 1);
for (int i = 1; i <=n; i++) {
if (Nums[i] > fin_num) {
fin_num = Nums[i];
fin_id = i;
}
}
printf("%d %d\n", fin_num,fin_id);
return 0;
}
柳神的代码:
#include <cstdio>
#include <vector>
using namespace std;
vector<int> v[100];
int book[100];
void dfs(int index, int level) {
book[level]++;
for(int i = 0; i < v[index].size(); i++)
dfs(v[index][i], level+1);
}
int main() {
int n, m, a, k, c;
scanf("%d %d", &n, &m);
for(int i = 0; i < m; i++) {
scanf("%d %d",&a, &k);
for(int j = 0; j < k; j++) {
scanf("%d", &c);
v[a].push_back(c);
}
}
dfs(1, 1);
int maxnum = 0, maxlevel = 1;
for(int i = 1; i < 100; i++) {
if(book[i] > maxnum) {
maxnum = book[i];
maxlevel = i;
}
}
printf("%d %d", maxnum, maxlevel);
return 0;
}
总结:
柳神的代码就非常的简洁:
该不要的就通通不要.
还有就是找错误的能力,我还不是很OK,还需要多加的练习
如果这篇文章对你有张帮助的话,可以用你高贵的小手给我点一个免费的赞吗
相信我,你也能变成光.
如果你有任何建议,或者是发现了我的错误,欢迎评论留言指出.