-
Notifications
You must be signed in to change notification settings - Fork 749
/
6. Maximum Employees to Be Invited to a Meeting.cpp
65 lines (57 loc) · 1.73 KB
/
6. Maximum Employees to Be Invited to a Meeting.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
An amazing question and usecase of cycle detection.
But here the graph is pretty simple one so we need not apply
the cycle detection DFS.
Rest we need to fist find and all all the 2 len cycles with
their neighbouring chains
then we can find the longest len cycle
return maximum of both
*/
class Solution {
public:
int height(int n,vector<int>&vis,vector<vector<int>> &reverseFav) {
vis[n] = 1;
int ans=1;
for(auto a:reverseFav[n]) {
if(vis[a]==0)
ans=max(ans,1+height(a,vis,reverseFav));
}
return ans;
}
int maximumInvitations(vector<int>& fav) {
int n = fav.size();
vector<vector<int>> reverseFav(n);
for(int i=0;i<n;i++) {
if(fav[fav[i]]!=i) {
reverseFav[fav[i]].push_back(i);
}
}
int ans2LenCycle=0;
for(int i=0;i<n;i++) {
if(fav[fav[i]]==i) {
vector<int>vis1(n,0),vis2(n,0);
int x = height(i,vis1,reverseFav);
int y = height(fav[i],vis2,reverseFav);
ans2LenCycle+=(x+y);
}
}
vector<int> vis(n,0);
int maxLenCycle=0;
for(int i=0;i<n;i++) {
int strt = i;
int cycleLen=0;
while(vis[strt]!=1) {
vis[strt]=1;
strt=fav[strt];
cycleLen++;
}
int strt2 = i;
while(strt2!=strt) {
strt2=fav[strt2];
cycleLen--;
}
maxLenCycle=max(maxLenCycle,cycleLen);
}
return max(maxLenCycle,ans2LenCycle/2);
}
};