Question: Pokemon Trainer
Solution: In C
#include <bits/stdc++.h>
using namespace std;
const int maxn = 300011;
char s[maxn];
int deg[maxn], d[maxn];
vector<int> w[maxn];
int Find(int u, int c) {
if (d[u] != -1) return d[u];
d[u] = 0;
for (int v : w[u]) {
d[u] = max(d[u], Find(v, c));
}
d[u] += (s[u] == c + 'a');
return d[u];
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
scanf("%s", s + 1);
for (int i = 1; i <= m; ++i) {
int u, v;
scanf("%d%d", &u, &v);
w[u].push_back(v);
++deg[v];
}
int ct = 0;
queue<int> que;
for (int i = 1; i <= n; ++i) {
if (!deg[i])
que.push(i), ++ct;
}
while (!que.empty()) {
int u = que.front();
que.pop();
for (int v : w[u]) {
if (--deg[v] == 0)
que.push(v), ++ct;
}
}
if (ct < n) {
puts("-1");
} else {
int ans = 0;
for (int c = 0; c < 26; ++c) {
memset(d, 0xff, sizeof d);
for (int i = 1; i <= n; ++i)
ans = max(ans, Find(i, c));
}
printf("%dn", ans);
}
return 0;
}
Follow Us
You Missed
- UKG Hiring for Software Engineer
- Unlocking Innovation and Diversity: Accenture HackDiva Empowers Women in Tech with Cutting-Edge Solutions – codewindow.in
- QA Engineer Opportunities at Siemens Company: Apply Now – codewindow.in
- Transform Your Career with S&P Global: Apply for the Software Development Engineer Role and Lead the Future of Financial Technology Innovation – codewindow.in
- Unlock Your Potential at Accenture as an Associate Software Engineer – Elevate Your Career with Innovation and Excellence – codewindow.in
- Accelerate Your Career: Join NVIDIA’s Elite Software Engineering Internship Program and Shape the Future of Technology – codewindow.in
- Software Engineer at ABB: Unlock Innovation and Shape the Future – codewindow.in
- IBM Associate Systems Engineer Job: Boost Your Career with a Leading Technology Giant – codewindow.in
- Start Your IT Career Journey with Amazon: IT Services Support Associate I Opportunity – codewindow.in
- Shape the Future of Web: Front-End Software Engineer Opportunity at Google Cloud – codewindow.in
- Exciting Opportunity: Java Spring Boot Senior Developer Role at Infosys – codewindow.in
- Unlock Your Potential at Nokia: Software Engineer Opportunities Await – codewindow.in
- Join Microsoft’s World-Class Team as a Software Engineer and Shape the Future of Technology – codewindow.in
- Virtusa is Seeking Talented React JS Developers to Drive Digital Excellence – codewindow.in
- Join IBM Dynamic Team as a Full Stack Developer and Shape the Future – codewindow.in
- EY Welcomes Aspiring AI/ML Interns to Unlock the Future of -codewindow.in
- Wipro is Hiring Test Engineers to Elevate Quality Assurance – codewindow.in
- Deloitte Is Hiring: Analysts and Senior Analysts Wanted to Drive Innovation – codewindow.in
- Exciting Software Development Opportunity At Oracle – codewindow.in
- PayPal Hiring Data Analyst 1 – codewindow.in
- SIEMENS HEALTHINEERS Hiring Data Scientist ( ML & AI) – codewindow.in
- Programming in C++ – codewindow.in
- Programming in C++ – codewindow.in
- Unstop Hiring Challenges Internships and Hackathons – codewindow.in
- Programming in Python – codewindow.in
- Programming in Python – codewindow.in
- Programming in C++ – codewindow.in
- Programming in C++ – codewindow.in
- Programming in C++ – codewindow.in
- Programming in C++ – codewindow.in
- Programming in C++ – codewindow.in
- Data Structure – comprehensive guide – codewindow.in