-
Notifications
You must be signed in to change notification settings - Fork 0
/
690.员工的重要性.cpp
52 lines (48 loc) · 1.02 KB
/
690.员工的重要性.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
* @lc app=leetcode.cn id=690 lang=cpp
*
* [690] 员工的重要性
*
* 深度优先搜索,其实可以使用map来保存id对Employee对象的映射。
*
*/
// @lc code=start
/*
// Definition for Employee.
class Employee {
public:
int id;
int importance;
vector<int> subordinates;
};
*/
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
// class Employee {
// public:
// int id;
// int importance;
// vector<int> subordinates;
// };
class Solution {
public:
unordered_map<int, Employee*> map;
int getImportance(vector<Employee*> employees, int id) {
for(Employee* e: employees)
map[e->id] = e;
Employee* employee = map[id];
int sum = 0;
BFS(employee, sum);
return sum;
}
void BFS(Employee* employee, int& sum){
sum += employee->importance;
for(int id: employee->subordinates){
Employee* child = map[id];
BFS(child, sum);
}
}
};
// @lc code=end