-
Notifications
You must be signed in to change notification settings - Fork 3
/
Day-21
42 lines (41 loc) · 1.26 KB
/
Day-21
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
class Solution
{
void getTanks(int n, vector<vector<int>> &adj, unordered_map<int,int> &tanks) {
for(int j=0;j<n;j++) {
int i=0;
while(i<n) {
if(adj[i][j]!=0) break;
i++;
}
if(i==n) tanks[j]++;
}
}
pair<int,int> minDFS(int curr, vector<vector<int>> &adj) {
for(int i=0;i<adj.size();i++) {
if(adj[curr][i]!=0) {
pair<int,int> temp=minDFS(i,adj);
return {min(adj[curr][i],temp.first),temp.second};
}
}
return {INT_MAX,curr};
}
public:
vector<vector<int>> solve(int n,int p,vector<int> a,vector<int> b,vector<int> d)
{
// code here
vector<vector<int>> adj(n, vector<int>(n,0));
for(int i=0;i<p;i++) {
adj[a[i]-1][b[i]-1]=d[i];
}
unordered_map<int,int> tanks;
getTanks(n,adj,tanks);
vector<vector<int>> res;
for(int i=0;i<n;i++) {
if(tanks.find(i)!=tanks.end()) {
pair<int,int> t=minDFS(i,adj);
if(t.first!=INT_MAX) res.push_back(vector<int>{i+1,t.second+1,t.first}); //single house with no outgoing and no incoming
}
}
return res;
}
};