forked from v100901/hackoctoberfest2020__
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTime_to_Inform_Employees.cpp
More file actions
36 lines (33 loc) · 905 Bytes
/
Time_to_Inform_Employees.cpp
File metadata and controls
36 lines (33 loc) · 905 Bytes
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
#include <bits/stdc++.h>
using namespace std;
int numOfMinutes(int n, int headID, vector<int>& manager, vector<int>& informTime) {
vector<int> adj[n];
int head;
for(int i=0;i<n;i++){
if(manager[i]==-1) head = i;
else adj[manager[i]].push_back(i);
}
queue<pair<int, int>> qu;
qu.push({head, 0});
int ans = -1;
while(!qu.empty()){
int m = qu.front().first, time = qu.front().second;
qu.pop();
ans = max(ans, time);
for(auto v:adj[m]){
qu.push({v, time+informTime[m]});
}
}
return ans;
}
int main(){
int n;
cin>>n;
int headID;
cin>>headID;
vector<int> manager(n), informTime(n);
for(int i=0;i<n;i++) cin>>manager[i];
for(int i=0;i<n;i++) cin>>informTime[i];
int ans = numOfMinutes(n, headID, manager, informTime);
cout<<ans;
}