小米2017校园招聘编程题
来源:互联网 发布:linux5.5 yum安装lnmp 编辑:程序博客网 时间:2024/04/27 15:55
题目:
代码:
#include<iostream> #include<vector> #include<map> #include<sstream> #include<fstream> #include<set>#include<algorithm> using namespace std;int total_level = 0;void BFS(int root,vector<vector<int>>&data,int level){for (int i = 0; i < data[root].size();i++){int new_root = data[root][i];int new_level = level + 1;if (new_level>total_level)total_level = new_level;BFS(new_root, data, new_level);}}void find_root(vector<vector<int>>&data, int num, int row,vector<int>node){int re = 0,root,total=0;int size = node.size();int max = *(--node.end());vector<int>::iterator iter;vector<int>flag(size, 1);for (int i = 0; i < max; i++){if (data[i].size() != 0){for (int j = 0; j < data[i].size(); j++){iter = find(node.begin(), node.end(), data[i][j]);if (iter != node.end()){*iter = -1;}}}}//寻找根节点for (int i = 0; i < node.size(); i++)if (node[i] != -1)root = node[i];total = 0;BFS(root,data,1);}int main(){ifstream fin("C:\\Users\\Dell\\Desktop\\data.txt");string str;int size, root, temp;int num, son;int node, row;vector<int>nodes;vector<int>tmp;vector<vector<int>>data;stringstream mystream;int count = 0;for (int i = 0; i < 1001; i++)data.push_back(tmp);while (fin >> num){row = 0;while (fin >> root >> son){data[root].push_back(son);row++;if(find(nodes.begin(),nodes.end(),root)==nodes.end())nodes.push_back(root);if (find(nodes.begin(), nodes.end(), son) == nodes.end())nodes.push_back(son);}}total_level = 0;find_root(data, num, row, nodes);cout << total_level << endl;}
5
1 2
2 3
3 4
4 5
结果:
测试用例2:
2
1 2
结果:
测试用例3:
算法思想:
因为树的编号最大值为999,所以新建一个容器的容器,我们将编号为n的根结点的所有子节点存放在二维容器的第n个元素里,所以在BFS深度搜索的时候就可以直接
data[root]寻找到它的所有根结点,然后利用深度优先搜索的方法递归回溯。题目不难!!!!!
0 0
- 小米2017校园招聘编程题
- 小米2017校园招聘笔试题
- 2012年小米校园招聘实习生笔试编程题
- 小米2017年校园招聘笔试题第三题
- 小米2017校园招聘(服务端开发类)
- 小米2013校园招聘笔试题
- 校园招聘:小米2013 最新笔试题
- 小米2013校园招聘笔试题[转贴]
- 2013年小米校园招聘笔试题
- 2013年小米校园招聘笔试题
- 小米2013校园招聘笔试题
- 2014小米校园招聘笔试题
- 2013年小米校园招聘笔试题
- 2013年小米校园招聘笔试题
- 2013年小米校园招聘笔试题
- 2015小米校园招聘笔试题
- 滴滴2017校园招聘在线编程题
- 阿里校园招聘2017编程题
- bzoj 3589: 动态树 (树链剖分+线段树)
- Leetcode240: Search a 2D Matrix II
- Android视图绘制流程完全解析,带你一步步深入了解View(二)
- Laravel 框架 ajax多条件搜索后 分页
- [网站搭建] 阿里云虚拟主机搭建及FTP文件上传
- 小米2017校园招聘编程题
- 第四周项目6—— 多项式求和
- 认识JAVA数组
- 【BZOJ1057】【codevs1428】棋盘制作,悬线法
- centos安装git源码包
- log4j的8个日志级别(OFF、FATAL、ERROR、WARN、INFO、DEBUG、TRACE、 ALL)
- Leetcode378: Kth Smallest Element in a Sorted Matrix
- MySQL创建用户和授权
- 【Mybatis学习总结一】Mybatis的helloworld