Page 2 of 3

Posted: Wed Dec 03, 2003 4:22 pm
by Bilash
Someone sent me some offline messages to know about Shorir's algo. Unfortunately, i deleted his/her name accidentally from my list :( . Can (s)he plz send his/her add again so that i can mail him/her?
Thanx!

Posted: Mon Jan 19, 2004 6:51 am
by horape
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 :cry:
Can anybody tell me the correct approach to solving this problem?
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.

Saludos,
HoraPe

Posted: Thu Feb 19, 2004 6:05 am
by junbin
horape wrote:
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 :cry:
Can anybody tell me the correct approach to solving this problem?
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.

Saludos,
HoraPe

I'm not very good at graph theory... what do you mean by cross/forward edge?

Posted: Thu Feb 19, 2004 1:14 pm
by horape
junbin wrote:
horape wrote:
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 :cry:
Can anybody tell me the correct approach to solving this problem?
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)
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)

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

Posted: Thu Feb 19, 2004 1:29 pm
by Examiner

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

Posted: Fri Feb 20, 2004 12:50 pm
by CDiMa
Examiner 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 problem states that there aren't loops so your second test is incorrect
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

Posted: Tue Mar 02, 2004 5:24 pm
by CDiMa
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 edge-disjoint (otherwise we could not remove some cycle)). I think, that by careful implementation, this could be done in O(n+m), too.
I don't think this is correct. This condition is necessary but not sufficient for a directed graph to be a cactus:

Code: Select all

1
3
6
0 1
1 0
1 2
2 1
2 0
0 2
Ciao!!!

Claudio

Posted: Tue Feb 22, 2005 11:01 am
by paulmcvn
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?

Posted: Sun Sep 25, 2005 6:02 am
by Martin Macko
Watch the special case, that for n=0 and n=1 the answer should be YES.

Cactus

Posted: Thu Jan 19, 2006 8:44 pm
by KvaLe
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:

Code: Select all

1
4
5
0 1
1 2
2 1
2 3
3 0
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.

AC

Posted: Wed Jan 25, 2006 5:57 pm
by KvaLe
I've got AC in 0.172 sec, I'd mistake in Tarjan's algorithm.

10510

Posted: Wed Jul 12, 2006 9:34 pm
by Navid666
my code seems to be correct but still gettin WA
please give me some input/output... please !!!
thanks a lot...

Re: 10510

Posted: Sun Jul 23, 2006 11:25 pm
by Martin Macko
Navid666 wrote:my code seems to be correct but still gettin WA
please give me some input/output... please !!!
thanks a lot...
If you post some test cases here, I can generate the correct outputs for you 8)

Posted: Sat Mar 24, 2007 6:22 pm
by helloneo
Hello..~

What is the correct output for this input..?

Code: Select all

1
4
4
0 1
1 0
2 3
3 2
Thanks.. :-)

Posted: Fri Jun 01, 2007 6:23 am
by yiuyuho
What is the correct output for this input..?

Code: Select all

1
4
4
0 1
1 0
2 3
3 2
NO

Is is not strongly connected.