I think that:
First, check if the graph is strongly connected (I use Tarjan's algorithm)
Then use this observation:
A strongly connected graph is not a cactus if and only exist a pair u,v such that there are two of more paths from u to v (could anyone prove this clearly?)
we can prove: there is a pair u,v such that there are two of more paths from u to v if and only if:
(1) DFS tree has cross edge or forward edge
or (2) DFS tree has a vertex v such that there is two of more back edge to v or any ancestor of v
proof
if part: easy to see
only if part:
suppose there is a pair u,v suct that there are two of more paths from u to v
consider the time DFS enters u
- if v is exited, then there is a cross edge
- if v is not visited , then there is a cross edge or a forward edge
- if v is visited, but not exited, there is there is two of more back edge to v or any ancestor of v
I'm not sure my observation and my proof is correct. Could anyone have some comment.
(2) can check by : there exist a vertex v such that there is a back edge to v and low[v]<d[v]
but with the test cases of the judge, we don't need to care of back edge, just check if there is no cross/forward edge, we can got AC
Bilash wrote:
At last I got it AC
What i did is check that:
1. The indegree is equal to the out degree for each vertex
2. There is no forward or cross edge in the dfs tree of the graph
3. There is at most one back edge pointing to any particular vertex in the dfs tree.
By the way, i checked strong connectivity using Shorir's algo whose complexity is O(n).
I can't understand why
The indegree is equal to the out degree for each vertex?
Could anyone help me?