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.
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
9 | nor | 153 |
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.
Название |
---|
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.