-
Notifications
You must be signed in to change notification settings - Fork 0
/
Loud and Rich
50 lines (41 loc) · 1.04 KB
/
Loud and Rich
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
class Solution {
public:
int val[1000];
map<int,vector<int>>adj;
void dfs(int root, vector<int>&q)
{
for(auto itr:adj[root])
{
if(q[val[itr]]>q[val[root]])
{
val[itr]=val[root];
dfs(itr,q);
}
/*
cout<<root<<" "<<itr<<endl;
cout<<itr<<" "<<val[itr]<<envdl;
cout<<endl;
*/
}
}
vector<int> loudAndRich(vector<vector<int>>& r, vector<int>& q) {
unordered_map<int,int>in;int n=0;
for(int i=0;i<r.size();i++)
{
adj[r[i][0]].push_back(r[i][1]);
in[r[i][1]]=1;
}
n=q.size()-1;
for(int i=0;i<=n;i++)
val[i]=i;
for(int i=0;i<=n;i++)
{
dfs(i,q);
}
vector<int>res;
cout<<"n"<<n;
for(int i=0;i<=n;i++)
res.push_back(val[i]);
return res;
}
};