-
Notifications
You must be signed in to change notification settings - Fork 10
/
HackerEarth- Permutation.cpp
60 lines (53 loc) · 1.4 KB
/
HackerEarth- Permutation.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
#include<bits/stdc++.h>
using namespace std;
int finally_visited[102];
int ans;
int arr[102];
vector<int>adj[102];
bool vis[102];
void dfs(int index,bool*vis)
{
vis[index]=true;
if(!finally_visited[index]) ans =min(arr[index],ans); // if that index has been replaced already by minimum number of that , so leave it
for(int i=0;i<adj[index].size();i++)
{
if(!vis[adj[index][i]])
{
dfs(adj[index][i],vis);
}
}
}
int main()
{
int n;
cin>>n;
int position[n+1];
for(int i=0;i<n;i++)
{
cin>>arr[i];
position[arr[i]]=i; // taking position of a number !!
}
char c;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cin>>c;
if(c=='Y')
{
adj[i].push_back(j); // taking the indices which (i) may be replaced by j indices
}
}
}
memset(finally_visited,false,sizeof(finally_visited));
for(int i=0;i<n;i++)
{
memset(vis,false,sizeof(vis));
ans=INT_MAX;
dfs(i,vis); // passing index and visited array .
finally_visited[position[ans]]=true; // that index has been replaced already by minimum number of that .so finally_visited !!
cout<<ans<<" "; // printing ans , minimum number of that index .
}
cout<<endl;
return 0;
}