<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 {
    #define pb push_back
    vector<int> g[100007];
    stack<int> st;
    void dfs(int vertex, vector<int>& vis){
        vis[vertex]=1;
        for(int child : g[vertex]){
            if( vis[child]==1) continue;
            dfs(child,vis);
        }
        st.push(vertex);
    }
    
    
    // int ans=0;
    // //  map<pair<int,int>,int> val;
    // void func(int vertex, string& color , vector<int>& out_degree, vector<vector<int>>& val ){
    //     int a=int(color[vertex])-97;
    //     val[vertex][a] += 1;
    //     for(int child : g[vertex]){
    //         for(int i=0; i<26; i++) val[child][i] += val[vertex][i];
    //         func(child,color, out_degree, val);
    //         for(int i=0; i<26; i++) val[ child][i] -= val[vertex][i];

    //     }

    //     if( out_degree[vertex]==0){
            
    //         for(int i=0; i<26; i++) ans= max(ans, val[vertex][i]);
    //         // cout<<vertex<<" "<<ans<<" ";
    //     }

    //     val[vertex][a] -=1;
    //     return ;
    // }

public:
    int largestPathValue(string s, vector<vector<int>>& edges) {
        // check the cycle in the directed graph using the topological sort
        int n=s.size();
        vector<vector<int>> val(n+7, vector<int>(28,0));

        vector<int> indegree(n+7),out_degree(n+7);
        for( auto it : edges){
            g[it[0]].pb(it[1]);
            indegree[it[1]]++;
            out_degree[it[0]]++;
        }

        vector<int> vis(n+7,-1);
        for(int i=0; i<n; i++){
            if( vis[i]==-1) dfs(i,vis);
        }
        vector<int> tsort; int a=0;  vector<int> pos(n+7,0);
        while( !st.empty()){ 
             tsort.pb(st.top()); 
             pos[st.top()]=a; a++;
             st.pop();
        }
        for(int i=0; i<n; i++){
            for(int child : g[i]){
                if( pos[i] >= pos[child]) return -1;
            }
        }
    
        
        vector<vector<int>>count(n,vector<int>(26,0));
        queue<int>q;
        for(int i=0;i<n;i++){
            if(indegree[i]==0){
                q.push(i);
                count[i][s[i]-'a']++;   
            }
        }
        int ans=0;
        while(!q.empty()){
            auto p=q.front();
            q.pop();
            int r=*max_element(count[p].begin(),count[p].end());
            ans=max(ans,r);
            for(auto v: g[p]){
                for(int i=0;i<26;i++){
                    count[v][i]=max(count[v][i],count[p][i]+(i==s[v]-'a'));
                }
                if(--indegree[v]==0){
                    q.push(v);
                }
            }
        }
        return ans;
    }
};
248179