PAT 1094 The Largest Generation

1094 The Largest Generation(25分)

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.node

Input Specification:

Each input file contains one test case. Each case starts with two positive integersN(<100) which is the total number of family members in the tree (and hence assume that all the members are numbered from 01 toN), andM(<N) which is the number of family members who have children. ThenMlines follow, each contains the information of a family member in the following format:git

ID K ID[1] ID[2] ... ID[K]

whereIDis a two-digit number representing a family member,K(>0) is the number of his/her children, followed by a sequence of two-digitID's of his/her children. For the sake of simplicity, let us fix the rootIDto be01. All the numbers in a line are separated by a space.spa

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.code

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

思路

  • 本題是求結點個數最多的那一層及該層的層號。

代碼

  • dfs
#include <cstdio>
#include <queue>
#include <vector>
#include <algorithm>

using namespace std;

const int maxn = 110;

struct node
{
    vector<int> child;
}Node[maxn];
int n, m;
int ans[maxn];

void dfs(int index, int depth)
{
    ans[depth] ++;
    if (Node[index].child.size() == 0)
    {
        return;
    }
    for (int i = 0; i < Node[index].child.size(); i ++)
    {
        dfs(Node[index].child[i], depth + 1);
    }
}

int main()
{
    //freopen("test.txt", "r", stdin);
    scanf("%d %d", &n, &m);
    int id, k, child;
    for (int i = 0; i < m; i ++)
    {
        scanf("%d %d", &id, &k);
        for (int j = 0; j < k; j ++)
        {
            scanf("%d", &child);
            Node[id].child.push_back(child);
        }
    }
    dfs(1, 1);
    int maxNode = -1;
    int ansId;
    for (int i = 1; i <= n; i ++)
    {
        if (ans[i] > maxNode)
        {
            maxNode = ans[i];
            ansId = i;
        }
    }
    printf("%d %d", maxNode, ansId);
    
    return 0;
}
  • bfs
#include <cstdio>
#include <queue>
#include <vector>

using namespace std;

const int maxn = 110;

struct node
{
    vector<int> child;
}Node[maxn];
int n, m;
int depth[maxn];
int ans[maxn];


void bfs(int root)
{
    queue<int> q;
    q.push(root);
    while (!q.empty())
    {
        int now = q.front();
        q.pop();
        ans[depth[now]] ++;
        for (int i = 0; i < Node[now].child.size(); i ++)
        {
            int child = Node[now].child[i];
            depth[child] = depth[now] + 1;
            q.push(child);
        }
    }
}

int main()
{
    freopen("test.txt","r",stdin);
    scanf("%d %d", &n, &m);
    int id, k, child;
    for (int i = 0; i < m; i ++)
    {
        scanf("%d %d", &id, &k);
        for(int j = 0; j < k; j ++)
        {
            scanf("%d", &child);
            Node[id].child.push_back(child);
        }
    }
    
    depth[1] = 1;
    bfs(1);
    
    int  max_node = -1, ans_depth;
    for (int i = 1; i <= n; i ++)
    {
        if (ans[i] > max_node)
        {
            max_node = ans[i];
            ans_depth = i;
        }
    }
    
    printf("%d %d", max_node, ans_depth);
    
    return 0;
}
相關文章
相關標籤/搜索