Given a graph with n nodes and m edges . n ≤ 100,000 and m ≤ 1,000,000 . We have to find any odd cycle.
How to solve this problem ? I tried in naive way. I just stored the nodes when I get any cycle of odd length . But it will obviously TLE.
# | User | Rating |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | luogu_official | 150 |
Given a graph with n nodes and m edges . n ≤ 100,000 and m ≤ 1,000,000 . We have to find any odd cycle.
How to solve this problem ? I tried in naive way. I just stored the nodes when I get any cycle of odd length . But it will obviously TLE.
Name |
---|
Check whether the Graph is Bipartite. A graph with an odd cycle can never be bipartite.
You can do this by running a BFS and coloring the odd and even levels with two colors. If you find any adjacent nodes with the same color, this means they are starting and endpoints of the odd cycle. Run another BFS/DFS to find the odd cycle.
The graph is directed; it's not exactly clear what bipartite means on directed graphs.
And the BFS method no longer works, either. Consider the graph with edges 1->2, 1->3, 2->3, and doing a BFS from vertex 1. vertex 2 (odd distance) is adjacent to vertex 3 (odd distance), but this is not a cycle.
can you check my code ? any other way to optimize it ?
I searched for a cycle in directed graph and when I found one, I checked whether it is a odd cycle or not. If it is a odd cycle then I found the cycle using the parent information. verdict is TLE
There are two things that matter for an algorithm: correctness and efficiency.
You shouldn't be thinking about optimizations at the code level unless you are convinced your algorithm is both correct and efficient.
Before thinking about efficiency, you should be thinking about correctness (and you should be trying to judge these things before you submit and get a verdict, so I'm going to ignore your TLE verdict for now). You can often optimize a correct but inefficient algorithm to make it more efficient, but usually if you have an efficient but incorrect algorithm it's just the wrong idea altogether. So, why does your algorithm work? From your description, it sounds like you've just looked for a single cycle and then exited once you've found it. What happens if the cycle you found is even length? Do you just keep going, or do you declare that there is no odd cycle?
If you keep going and looking for odd cycles, how long does your program take? Do you just do a single DFS through the graph? Is this guaranteed to find an odd cycle if there is one? Why?
These are the questions you should be asking before you even sit down and code your algorithm, let alone optimize the code.
I will say that I don't know the solution to the problem and I haven't thought about it too long. My guess is that it's probably relatively difficult if no one has given a correct solution in the comments yet, but I don't know. Perhaps you can do something with the DFS tree but it's not clear to me.