Repository navigation
Expand file tree
/
Copy pathmostBeautifulPath.cpp
More file actions
85 lines (59 loc) · 1.42 KB
/
Copy pathmostBeautifulPath.cpp
File metadata and controls
85 lines (59 loc) · 1.42 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include<bits/stdc++.h>
using namespace std;
void print(int x){cout<<x<<endl;}
template <class X> void print(X &x) {cout<<x<<endl;}
string ans;
int maxfreq;
void cnt_max_freq(string &path){
unordered_map<char, int> mp;
int mx = INT_MIN;
for(auto c: path){
mp[c]++;
mx = max(mx, mp[c]);
}
if(mx > maxfreq) {
maxfreq = mx;
ans = path;
}
}
bool dfs(unordered_map<int, vector<int>> &edges, int node, vector<bool> &visited, string &path, string &s){
if(visited[node]) return true;
path.push_back(s[node-1]);
visited[node] = true;
for(auto i: edges[node]){
if(dfs(edges, i, visited, path, s)) return true;
}
cnt_max_freq(path);
path.pop_back();
return false;
}
void solve(){
int n, m;
cin>>n>>m;
string s;
cin>>s;
vector<int> x(m), y(m);
for(int i=0; i<m; i++)cin>>x[i];
for(int i=0; i<m; i++)cin>>y[i];
unordered_map<int, vector<int>> edges;
map<vector<unordered_map<char, int>>, int> mp;
for(int i=0; i<m; i++){
edges[x[i]].push_back(y[i]);
}
vector<bool> visited(n+1, false);
for(int i=0; i<m; i++){
string path="";
if(!visited[x[i]]){
dfs(edges, x[i], visited, path, s);
}
}
cout<<ans<<endl;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
ans = "-1";
maxfreq = INT_MIN;
solve();
return 0;
}