1094.The Largest Generation

来源:互联网 发布:阿里云香港vps 编辑:程序博客网 时间:2024/05/22 02:26
【题意】
        给出一个树,找到节点数最多的一层以及相应的节点数
【思路】

        先将输入读入一个map中,然后用队列层先遍历即可


#include <iostream>#include <string>#include <queue>#include <vector>#include <map>using namespace std;int main(){int n, m;cin >> n >> m;map<int, vector<int>> nonLeafNodes;vector<int> levelCnt(n + 1, 0);for (int i = 0; i < m; ++i){int index, k;cin >> index >> k;for (int j = 0; j < k; ++j){int tmp;cin >> tmp;nonLeafNodes[index].push_back(tmp);}}queue<pair<int, int>> qq;qq.push(make_pair(1, 1));int resLevel, maxCnt = 0;while (!qq.empty()){auto tmp = qq.front();qq.pop();if (++levelCnt[tmp.second] > maxCnt){maxCnt = levelCnt[tmp.second];resLevel = tmp.second;}if (nonLeafNodes.find(tmp.first) != nonLeafNodes.end()){for (auto i : nonLeafNodes[tmp.first]){qq.push(make_pair(i, tmp.second + 1));}}}cout << maxCnt << " " << resLevel;system("pause");return 0;}


0 0