-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path690.cpp
More file actions
39 lines (38 loc) · 1.04 KB
/
690.cpp
File metadata and controls
39 lines (38 loc) · 1.04 KB
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
/*
// Definition for Employee.
class Employee {
public:
int id;
int importance;
vector<int> subordinates;
};
*/
class Solution {
public:
int getImportance(vector<Employee*> employees, int id) {
unordered_map<int,pair<int,unordered_set<int>>> graph;
for(int i=0; i<employees.size(); i++){
graph[employees[i]->id].first = employees[i]->importance;
for(int j=0; j<employees[i]->subordinates.size(); j++)
graph[employees[i]->id].second.insert(employees[i]->subordinates[j]);
}
unordered_set<int> visited;
queue<int> q;
int res = 0;
q.push(id);
visited.insert(id);
while(!q.empty()){
int u = q.front();
q.pop();
if(graph.count(u))
res += graph[u].first;
for(auto& v: graph[u].second){
if(!visited.count(v)){
q.push(v);
visited.insert(v);
}
}
}
return res;
}
};