其他分享
首页 > 其他分享> > PAT2020春3月份 1094 The Largest Generation (25 分) 经验分享和心路历程

PAT2020春3月份 1094 The Largest Generation (25 分) 经验分享和心路历程

作者:互联网

做了25分钟,满分

// A1094.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <bits/stdc++.h>
using namespace std;
unordered_map<int, vector<int>> tree;
vector<int> most(110, 0);
int n, m;

struct node {
    int id;
    int level;
};

int level_order(int root) {
    int level = 1;
    queue<node> q;
    q.push({ root ,level});
    while (!q.empty())
    {
        node top = q.front();
        most[top.level]++;
        q.pop();
        for (int i = 0; i < tree[top.id].size(); i++) {
            q.push({ tree[top.id][i],top.level + 1 });
        }
    }
    int max = -1;
    int pos = -1;
    for (int i = 1; i <= n; i++) {
        if (most[i] > max) {
            max = most[i];
            pos = i;
        }
    }
    return pos;
}
int main()
{
#ifndef ONLINE_JUDGE
    FILE* S;
    freopen_s(&S, "in.txt", "r", stdin);//debug in,txt in.txt
#endif // !ONLINE_JUDGE
    cin >> n >> m;
    int father, son,num;
    string t;
    for (int i = 0; i < m; i++) {
        cin >> father>>num;
        for (int j = 0; j < num; j++) {
            cin >> son;
            tree[father].push_back(son);
        }
    }//input
    int a = level_order(1);
    cout << most[a]<<" "<< a;
    return 0;
}

 

标签:PAT2020,1094,level,int,top,tree,son,++,25
来源: https://blog.csdn.net/qq_30753921/article/details/114015924