Help in DFS code

I have a directed graph (with possible self-loops and cycles). Each vertex has a certain value. Given the graph, I need to find the maximum value that is reachable from each vertex. I have tried applying dfs but I am stuck at the condition when a cycle occurs. I have also tried approaching it with white-grey-black visiting, but can’t get it right. I am stuck from a long time.
Please help.

ll dfs(ll s) {
    if (vis[s]==2) return rch[s];
    vis[s] = 1;
    ll m = val[s];
    for (ll x: adj[s]) {
        if (vis[x]==1) continue;
        m = max(m, dfs(x));
    }
    vis[s] = 2;
    rch[s] = m;
    return m;
}

Find Strongly Connected Components and try to solve the problem on the condensed graph. You can read more about finding SCCs here :

In DFS condition of cycle occurs when a node say A is in the adjacency list of node B and A is not the parent of B whose adjacency list is being checked.
So if such node (i.e not parent) is already visited by some other node then there is a cycle but you are not handling that case.
if(vis[x]==1) then also update the value of m by m = max (m,rch[x]).
I guess your code finds the current maximum for any node only for the tree edges. It should do the same for back edges.