网易游戏2016笔试题(三)
来源:互联网 发布:淘宝代销邮费算谁的 编辑:程序博客网 时间:2024/04/29 03:33
网易游戏2016笔试题(三)
算法思路:利用了拓扑排序的思想,预先将输入的测试数据存储在邻接表中,首先遍历入度(即无依赖的编译文件),将其存放入优先队列中(之所以不用栈或者Vector保存是为了保证输出的顺序是按照字典序从小到大排列的);
1.队列不为空,取出队列top元素,加入结果集中,所有依赖该编译文件的文件入度减一,如果减一后入度为0,则将改元素加入优先队列中;
2.队列为空,跳出循环;
3.结果集元素个数小于编译文件个数,说明有循环依赖的,输出ERROR;相等则依次输出结果集;
算法复杂度为O(n+e)
#include <iostream>#include "vector"#include "queue"#include <string>#include<algorithm>using namespace std;struct EdgeNode{ int ID; string filename; EdgeNode *next; bool operator < (const EdgeNode &a) const { return a.filename.compare(filename) >= 0 ? false:true; }};struct VertexNode{ string filename; int ID; int indegree; EdgeNode *next;};void OutPutCompileOrder(vector<VertexNode> adjGraph){ int i; vector<string> res; priority_queue<EdgeNode, vector<EdgeNode>> array; for (i = 0; i < adjGraph.size(); i++) { if (!adjGraph[i].indegree) { EdgeNode temp; temp.filename = adjGraph[i].filename; temp.ID = i; temp.next = NULL; array.push(temp); } } while (!array.empty()) { int temp = array.top().ID; res.push_back(adjGraph[temp].filename); EdgeNode *p = adjGraph[temp].next; array.pop(); while (p) { adjGraph[p->ID].indegree--; if (adjGraph[p->ID].indegree == 0) { EdgeNode temp; temp.filename = adjGraph[p->ID].filename; temp.ID = p->ID; temp.next = NULL; array.push(temp); } p = p->next; } } if (res.size() < adjGraph.size()) { cout << "ERROR" << endl; return; } for (i = 0; i < res.size(); i++) cout << res[i] << endl;}int main(){ int t,n,m; cin >> t; for (int i = 0; i < t; i++) { cin >> n; VertexNode initadj; initadj.next = NULL; vector<VertexNode> adjGraph(n, initadj); for (int j = 0; j < n; j++) { string filename; int m; cin >> filename >> m; VertexNode adjList; adjGraph[j].filename = filename; adjGraph[j].ID = j; adjGraph[j].indegree = m; for (int k = 0; k < m; k++) { EdgeNode *pNode = new EdgeNode; EdgeNode *temp; int ID; cin >> ID; pNode->ID = j; pNode->next = NULL; cout << pNode << endl; temp = adjGraph[ID].next; adjGraph[ID].next = pNode; pNode->next = temp; } } OutPutCompileOrder(adjGraph); } return 0;}
1 0
- 网易游戏2016笔试题(三)
- 网易游戏2016校园招聘笔试题(一)
- 数字游戏----网易2016研发工程师笔试题(二)
- 网易游戏笔试题(1) 20171209
- 网易游戏笔试题(2) 20171209
- 网易游戏笔试题(3) 20171209
- 网易游戏2016在线笔试
- 2018网易游戏笔试题
- 名企笔试:网易游戏2016招聘笔试题(双核处理)
- 网易2018笔试题三
- 宝石商人(2016网易游戏校招笔试)
- 网易游戏2014年校招笔试题(回忆版)
- Difficult Player Grouping(2015网易游戏笔试题)
- Amusing Digits(2015网易游戏笔试题)
- Best Compression Algorithms(网易游戏2015笔试题)
- 网易游戏2015 在线笔试题(2)
- 网易游戏2015 在线笔试题(5)
- 网易游戏2015 在线笔试题(1)
- python常用函数(zip,map,filter,reduce)
- 50个程序猿笑话
- iOS开发基础 - UISearchController
- pssh使用指南
- 【开源框架】EventBus 消息总线使用
- 网易游戏2016笔试题(三)
- linux服务器时间同步——ntpdate 172.20.0.191
- oracle存储过程语法二
- java 版 A*寻路算法具体过程
- JavaScript中的call和apply
- HashMap深度解析(一)
- java 求连续子数组的最大和
- linux下安装配置redis服务
- iOS 9 新特性之实现 3D Touch 就是 So easy -- OC版