<p>There is a <strong>directed graph</strong> of <code>n</code> colored nodes and <code>m</code> edges. The nodes are numbered from <code>0</code> to <code>n - 1</code>.</p>

<p>You are given a string <code>colors</code> where <code>colors[i]</code> is a lowercase English letter representing the <strong>color</strong> of the <code>i<sup>th</sup></code> node in this graph (<strong>0-indexed</strong>). You are also given a 2D array <code>edges</code> where <code>edges[j] = [a<sub>j</sub>, b<sub>j</sub>]</code> indicates that there is a <strong>directed edge</strong> from node <code>a<sub>j</sub></code> to node <code>b<sub>j</sub></code>.</p>

<p>A valid <strong>path</strong> in the graph is a sequence of nodes <code>x<sub>1</sub> -&gt; x<sub>2</sub> -&gt; x<sub>3</sub> -&gt; ... -&gt; x<sub>k</sub></code> such that there is a directed edge from <code>x<sub>i</sub></code> to <code>x<sub>i+1</sub></code> for every <code>1 &lt;= i &lt; k</code>. The <strong>color value</strong> of the path is the number of nodes that are colored the <strong>most frequently</strong> occurring color along that path.</p>

<p>Return <em>the <strong>largest color value</strong> of any valid path in the given graph, or </em><code>-1</code><em> if the graph contains a cycle</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet1.png" style="width: 400px; height: 182px;" /></p>

<pre>
<strong>Input:</strong> colors = &quot;abaca&quot;, edges = [[0,1],[0,2],[2,3],[3,4]]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The path 0 -&gt; 2 -&gt; 3 -&gt; 4 contains 3 nodes that are colored <code>&quot;a&quot; (red in the above image)</code>.
</pre>

<p><strong class="example">Example 2:</strong></p>

<p><img alt="" src="https://assets.leetcode.com/uploads/2021/04/21/leet2.png" style="width: 85px; height: 85px;" /></p>

<pre>
<strong>Input:</strong> colors = &quot;a&quot;, edges = [[0,0]]
<strong>Output:</strong> -1
<strong>Explanation:</strong> There is a cycle from 0 to 0.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == colors.length</code></li>
	<li><code>m == edges.length</code></li>
	<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
	<li><code>0 &lt;= m &lt;= 10<sup>5</sup></code></li>
	<li><code>colors</code> consists of lowercase English letters.</li>
	<li><code>0 &lt;= a<sub>j</sub>, b<sub>j</sub>&nbsp;&lt; n</code></li>
</ul>
class Solution {
public:

    bool dfsCheckCycle(int node, vector<int> adj[], vector<int>& visited, vector<int>& recStack) {
        visited[node] = 1;      // Mark node as visited
        recStack[node] = 1;     // Mark node as part of the recursion stack

        // Visit all neighbors
        for (int neighbor : adj[node]) {
            // If neighbor is not visited, recurse on it
            if (!visited[neighbor]) {
                if (dfsCheckCycle(neighbor, adj, visited, recStack)) {
                    return true; // Cycle found
                }
            }
            // If neighbor is already in the recursion stack, we found a cycle
            else if (recStack[neighbor]) {
                return true;  // Cycle found
            }
        }

        recStack[node] = 0;  // Remove the node from the recursion stack after processing
        return false;
    }

    // Function to check if a cycle exists in the graph
    bool isCyclic(vector<int> adj[], int n) {
        vector<int> visited(n, 0);  // Visited array
        vector<int> recStack(n, 0); // Recursion stack

        // Perform DFS for every unvisited node
        for (int i = 0; i < n; ++i) {
            if (!visited[i]) {
                if (dfsCheckCycle(i, adj, visited, recStack)) {
                    return true;  // Cycle detected
                }
            }
        }

        return false;  // No cycle found
    }

    void dfs(int node, vector<int> adj[], vector<int>& visited, stack<int>& topoStack) {
        visited[node] = 1;  // Mark the node as visited

        // Visit all neighbors
        for (int neighbor : adj[node]) {
            if (!visited[neighbor]) {
                dfs(neighbor, adj, visited, topoStack);
            }
        }

        // After all neighbors are processed, push the current node to the stack
        topoStack.push(node);
    }

    void dfs2(int node, vector<int> adj[], vector<int>& visited, string &colors, vector<vector<int>> &dp){
        visited[node]=1;
        int curr_color = colors[node]-'a';
        dp[node][curr_color] = 1;
        for(int i=0;i<26;i++){
            for(int j=0;j<adj[node].size();j++){
                
                int neighbour = adj[node][j];
                // cout<<node<<" "<<neighbour<<endl;
                int temp = 0;
                if(i==curr_color) temp=1;
                if(visited[neighbour]==0) dfs2(neighbour, adj, visited, colors, dp);
                dp[node][i] = max(dp[neighbour][i] +temp, dp[node][i]);
            }
        }
        return;
    }

    int largestPathValue(string colors, vector<vector<int>>& edges) {
        vector<int> adj[colors.size()];
        vector<int> visited(colors.size(),0);
        for (const auto& edge : edges) {
            int u = edge[0]; // from node
            int v = edge[1]; // to node
            adj[u].push_back(v);
            // adj[v].push_back(u);
        }
        if(isCyclic(adj, colors.size())) return -1;
        stack<int> topoStack;      // Stack to store the topological sort

        // Perform DFS on all nodes
        for (int i = 0; i < colors.size(); ++i) {
            if (!visited[i]) {
                dfs(i, adj, visited, topoStack);
            }
        }

        // Create a vector to store the result in topological order
        vector<int> topoOrder;
        while (!topoStack.empty()) {
            topoOrder.push_back(topoStack.top());
            // cout<<topoStack.top()<<endl;
            topoStack.pop();
        }
        vector<vector<int>> dp(colors.size(),vector<int>(26,0));
        vector<int> visited2(colors.size(),0);
        int ans=1;
        for(int i= topoOrder.size()-1;i>=0;i--){
            
            if(visited2[topoOrder[i]]==0){
                // cout<<topoOrder[i]<<endl;
                dfs2(topoOrder[i], adj, visited2, colors, dp);
                
                for(int j=0;j<26;j++){
                    ans=max(ans, dp[topoOrder[i]][j]);
                }
            }
        }
        return ans;

    }
};
165514