I’m trying to understand the solution to this problem here Solution - Even Outdegree Edges (CSES). I have a question related to this part of the code:
if (!visited[i]) {
// visit it
} else if (visited[node] > visited[i]) {
// back edge
}
I understand that this checks if the child node has a back edge to its ancestor. But I can’t understand why we check here if (visited[node] > visited[i])
? I understand that if the child is already visited this means that this edge is a back edge all the time. So why do we use this check?
When I tried to remove the check here and make it just else
I got WA. Please anyone explain this.