You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
2.6 KiB

2 years ago
/*/*******************************************************************************
** **
** Jiedi(China nanjing)Ltd. **
** **
*******************************************************************************/
/*****************************FILE INFOMATION***********************************
**
** Project : ---leetcode
** Contact : xiacaojun@qq.com
** : http://blog.csdn.net/jiedichina
** : http://study.163.com/u/xiacaojun
https://jiedi.ke.qq.com/
csdn https://edu.csdn.net/course/detail/25037
** 51cto http://edu.51cto.com/lecturer/index/user_id-100013755.html
** http://www.laoxiaketang.com
**
** ---leetcode 296249312
** : jiedi2007
** :
**
*****************************************************************************
// 算法设计与编程实践---基于leetcode的企业真题库 课程 QQ群296249312 下载代码和交流*/
/*
* @lc app=leetcode.cn id=690 lang=cpp
*
* [690]
*/
/*
// Employee info
class Employee {
public:
// It's the unique ID of each node.
// unique id of this employee
int id;
// the importance value of this employee
int importance;
// the id of direct subordinates
vector<int> subordinates;
};
*/
#include <vector>
#include <queue>
using namespace std;
class Solution {
public:
int getImportance(vector<Employee*> employees, int id) {
int res = 0;
for(auto k :employees){
if(k->id == id){
BFS(employees,k,res);
}
}
return res;
}
void BFS(vector<Employee*> employees,Employee* k,int& res){
queue<Employee*> q;
q.push(k);
while(!q.empty()){
Employee* top = q.front();
q.pop();
res += top->importance;
if(top->subordinates.size()!=0){
for(auto k:top->subordinates){
for(int i=0;i<employees.size();i++){
if(k==employees[i]->id){
q.push(employees[i]);
break;
}
}
}
}
}
}
};