Skip to content

Commit

Permalink
Reviewing F
Browse files Browse the repository at this point in the history
  • Loading branch information
Nanashii76 authored Jun 22, 2024
1 parent e9c16cc commit f67e0d8
Showing 1 changed file with 26 additions and 23 deletions.
49 changes: 26 additions & 23 deletions Codeforces/graphs/F_substring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,55 +7,58 @@ vector<vector<int>> g(N);
vector<int> toposort;

int main() {

ios_base::sync_with_stdio(false);
cin.tie(NULL);

int n, m; cin >> n >> m;
string str; cin >> str;
int n, m;
cin >> n >> m;
string str;
cin >> str;

vector<vector<int>> freq(n, vector<int>(26, 0));

while(m--) {
int u,v; cin >> u >> v;
--u,--v;
int u, v;
cin >> u >> v;
--u, --v;
g[u].emplace_back(v);
++indegree[v];
}

queue<int> q;
int count = 0;
for(int i = 0; i < n; ++i) {
if(not indegree[i])
if(not indegree[i]) {
q.push(i);
freq[i][str[i] - 'a'] = 1;
}
}

int count = 0;
int maxVal = 0;

while(not q.empty()) {
int x = q.front();
q.pop();
++count;
int x = q.front(); q.pop();
toposort.emplace_back(x);

for(auto v : g[x]) {
--indegree[v];
if(not indegree[v]) {
q.push(v);
}
for (int c = 0; c < 26; ++c) {
freq[v][c] = max(freq[v][c], freq[x][c] + (str[v] - 'a' == c ? 1 : 0));
maxVal = max(maxVal, freq[v][c]);
}
}
}

if(count < n)
cout << "-1" << endl;

else {

unordered_map<char,int> mp;
for(auto x : toposort)
mp[str[x]]++;

int maxVal = 0;
for(auto it : mp)
maxVal = max(maxVal,it.second);

if(count < n) {
cout << "-1" << endl;
} else {
cout << maxVal << endl;

}

return 0;
}
}

0 comments on commit f67e0d8

Please sign in to comment.