10510  Cactus
Moderator: Board moderators
This is what I did... DFS, checking that there is exactly one back edge from any node that isn't root, no cross/forward edges (and that for each tree edge, dfsmin of the other end of the edge is the current node.Bilash wrote:Is finding cactus related to finding articulation points in a directed graph anyway? i think in a cactus, the simple cycles are connected to each other only through articulation vertices. i tried in this way, but still WA
Can anybody tell me the correct approach to solving this problem?
Saludos,
HoraPe
horape wrote:This is what I did... DFS, checking that there is exactly one back edge from any node that isn't root, no cross/forward edges (and that for each tree edge, dfsmin of the other end of the edge is the current node.Bilash wrote:Is finding cactus related to finding articulation points in a directed graph anyway? i think in a cactus, the simple cycles are connected to each other only through articulation vertices. i tried in this way, but still WA
Can anybody tell me the correct approach to solving this problem?
Saludos,
HoraPe
I'm not very good at graph theory... what do you mean by cross/forward edge?
Read on DFS, basically, when you run the DFS the edges get classified on tree edges (ones on the DFS tree), forward (from a node to a descendant on the DFS tree), back edges (from a node to an ascentor on the DFS tree) and cross edges (from a node on a dfs tree to a node on a different DFS tree, only exist on directed graphs)junbin wrote:I'm not very good at graph theory... what do you mean by cross/forward edge?horape wrote:This is what I did... DFS, checking that there is exactly one back edge from any node that isn't root, no cross/forward edges (and that for each tree edge, dfsmin of the other end of the edge is the current node)Bilash wrote:Is finding cactus related to finding articulation points in a directed graph anyway? i think in a cactus, the simple cycles are connected to each other only through articulation vertices. i tried in this way, but still WA
Can anybody tell me the correct approach to solving this problem?
dfsmin(v) is the vertex nearest to root for what there is a path from v that has only one back edge.
Hope this helps you,
HoraPe
10510 Test Case
Code: Select all
3
6
7
0 1
3 0
1 2
2 3
1 3
4 5
2 4
1
1
0 0
5
7
0 1
1 2
2 3
3 0
0 2
2 4
4 0
Re: 10510 Test Case
The problem states that there aren't loops so your second test is incorrectExaminer wrote:Code: Select all
3 6 7 0 1 3 0 1 2 2 3 1 3 4 5 2 4 1 1 0 0 5 7 0 1 1 2 2 3 3 0 0 2 2 4 4 0
The other two tests aren't cacti.
The first one isn't strongly connected.
Both of them have edges that belong to two simple cycles
(for example edge 3 0 belongs to cycles 0 1 2 3 0 and 0 1 3 0 for the first one
and 0 1 2 3 0 and 0 2 3 0 for the last one)
Ciao!!!
Claudio
I don't think this is correct. This condition is necessary but not sufficient for a directed graph to be a cactus:marian wrote:Second possible solution can be repeatedly remove simple directed cycles. If graph contains some edges, but does not contain a cycle, it is not a cactus. If we successfully remove all the edges, given graph is a cactus. (because removed cycles had to be edgedisjoint (otherwise we could not remove some cycle)). I think, that by careful implementation, this could be done in O(n+m), too.
Code: Select all
1
3
6
0 1
1 0
1 2
2 1
2 0
0 2
Claudio
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
I can't understand why
The indegree is equal to the out degree for each vertex?
Could anyone help me?
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?

 A great helper
 Posts: 481
 Joined: Sun Jun 19, 2005 1:18 am
 Location: European Union (Slovak Republic)
Cactus
I've written this problem, but I think my program is wrong.
When I give this input it returns that given graph is cactus, but I think it isn't. Here is input:
Here edge 0 > 1 is in two cycle (0 > 1 > 2 > 3 > 0 and 1 > 2 > 1).
My algorithm is such:
1) Determine if the given graph is strongly connected. I'm doing it using Tarjan's algorithm. If graph isn't strongly connected answer is NO.
2) For each edge determine it's class (back, forward, cross or tree edge).
3) If we get one or more forward or cross edges the answer is NO, otherwise I'm checking if for any vertex u exists vertecies v and w, such that edge (u > v) and edge (u > w) is back edge answer is NO.
4) Then I'm checking if I haven't just printed NO I'm printing YES.
This is my algorithm, please check it and if u find bug please say me.
In that test, which I've wrote above there isn't any forward and cross edges, and there are only two back edges (2 > 1 and 3 > 0), so answer must be NO, but with my algorithm it's YES.
When I give this input it returns that given graph is cactus, but I think it isn't. Here is input:
Code: Select all
1
4
5
0 1
1 2
2 1
2 3
3 0
My algorithm is such:
1) Determine if the given graph is strongly connected. I'm doing it using Tarjan's algorithm. If graph isn't strongly connected answer is NO.
2) For each edge determine it's class (back, forward, cross or tree edge).
3) If we get one or more forward or cross edges the answer is NO, otherwise I'm checking if for any vertex u exists vertecies v and w, such that edge (u > v) and edge (u > w) is back edge answer is NO.
4) Then I'm checking if I haven't just printed NO I'm printing YES.
This is my algorithm, please check it and if u find bug please say me.
In that test, which I've wrote above there isn't any forward and cross edges, and there are only two back edges (2 > 1 and 3 > 0), so answer must be NO, but with my algorithm it's YES.
Giorgi Lekveishvili
10510
my code seems to be correct but still gettin WA
please give me some input/output... please !!!
thanks a lot...
please give me some input/output... please !!!
thanks a lot...
=;GOOD LUCK=;
NAvidOP
NAvidOP

 A great helper
 Posts: 481
 Joined: Sun Jun 19, 2005 1:18 am
 Location: European Union (Slovak Republic)
Re: 10510
If you post some test cases here, I can generate the correct outputs for youNavid666 wrote:my code seems to be correct but still gettin WA
please give me some input/output... please !!!
thanks a lot...
Hello..~
What is the correct output for this input..?
Thanks..
What is the correct output for this input..?
Code: Select all
1
4
4
0 1
1 0
2 3
3 2
What is the correct output for this input..?
NO
Is is not strongly connected.
Code: Select all
1
4
4
0 1
1 0
2 3
3 2
Is is not strongly connected.