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
- 1094.The Largest Generation
- 1094. The Largest Generation
- 1094. The Largest Generation
- 1094. The Largest Generation
- 1094. The Largest Generation
- 1094. The Largest Generation
- 1094. The Largest Generation
- 1094. The Largest Generation
- 1094. The Largest Generation
- 1094. The Largest Generation (25)
- PAT 1094. The Largest Generation
- 1094. The Largest Generation (25)
- 1094. The Largest Generation (25)
- 1094. The Largest Generation (25)
- 1094. The Largest Generation (25)
- 1094. The Largest Generation (25)
- 1094. The Largest Generation (25)
- 1094. The Largest Generation (25)
- Java设计模式——外观模式(门面模式)(Facade)
- HTML5之Canvas画圆形
- access 偏移注入 案例
- 1093.Count PAT's
- Java的ClassLoader
- 1094.The Largest Generation
- spring学习笔记13(注解@Autowired方式注入)
- 1095.Cars on Campus
- linux内核驱动学习(七)----驱动分类 | 学习方法 | 硬件访问
- FLV封装格式分析器
- java模式 之工厂方法模式(Factory Method)
- OSI7层模型与TCP/IP协议栈4层
- Ubuntu终端教程03:文件操作
- 机器学习笔记