Here's the problem statement:
The author's solution proposes a method using a spanning tree, but I discovered a different approach (in contest) involving bipartite graphs, specifically revolving around this trivial observation:
We can also prove that if a simple connected graph $$$G$$$ doesn't contain a cycle with odd length then it must be bipartite by running a simple $$$BFS$$$ from $$$1$$$ and assign colors according to depth.
Therefore, we can solve the problem by first running a standard $$$BFS$$$ from $$$1,$$$ then checking if the graph is bipartite. This can be done by iterating through edges $$$(u, v)$$$ and check the parity of $$$d_u + d_v,$$$ with $$$d_i$$$ being the depth of the vertex $$$i$$$ after the $$$BFS$$$ from $$$1.$$$
If for all edges $$$(u, v),$$$ $$$d_u + d_v$$$ is odd then we can conclude that there are no odd cycles (and print $$$-1$$$).
If there exists an edge $$$(U, V)$$$ that $$$d_U + d_V$$$ is even then $$$(U, V)$$$ is an edge in a odd cycle.
This is where another array comes in, $$$from$$$. We define $$$from_i$$$ as the vertex that "adds" the node $$$i$$$ into the $$$BFS$$$ queue, and $$$from_1=-1$$$ (details are in the source code).
We can find the odd cycle like so: start with a deque with $$$U$$$ at the back and $$$V$$$ at the end and continuously assign $$$U := from_U$$$ and $$$V := from_V$$$ until $$$U=V,$$$ then we can push $$$U$$$ to the back and finish finding the odd cycle.








Cool
I too solved this problem in another way:
We can run a dfs from any arbitrary node, and store the distances of each node traversed, , which is calculated as dist[node] = dist[parent]+1. (Note that this does not represent the shortest distance from the chosen source node, since it’s not a bfs)
Let’s say we are currently at node u in the traversal, and one of its neighbours, v, is visited and (dist[u]-dist[v]) is even. Then we have found an odd length cycle, whose nodes we can find by simply backtracking on the parent array.
i had the same solution, in my biased opinion its the best method
did it in the same method as well!
ORZ.. i can not solve in contest.. you are my hero
sure bro
my solution was a lil different i first did a BFS and found the FIRST conflicting edge (u, v)
if there is none the answer is -1
we know that there exist a path from u to v alternating colors assigned using BFS since its the FIRST conflicting edge
you can do something like this
where p.first and p.second are the nodes of the conflicting edge
You have created a spanning tree too. $$$from_i$$$ is the parent of vertex $$$i$$$ in your tree created by the BFS.
I solved it in a similar way using DFS. When I found an odd cycle using bi-coloring, I stored the parent and child nodes and immediately returned from the function. Then, I backtracked from the child to the parent to get the cycle.
Brilliant solution. I solved it by Tarjan's algorithm, and Tarjan's algorithm is too much for this task, thank you for this idea!