Блог пользователя yosako

Автор yosako, история, 3 года назад, По-английски

Oh hi Codeforces. I'm doing problem Longest Flight Route. At first I'm like "hum ... directed graph but with no circle, gotta find the longest path from 1 to n using normal BFS and I'm gonna be find ...". So it turned out that I didn't get AC because somehow I got TLE verdict on 2 test cases ... I checked the runtime of other test and saw that longest runtime for other test that got AC is like 0.09 sec so it must my algorithm's fault. I did some more check and found that something has caused my BFS to run like forever. However, it's not possible I think. My solution is just the reversed version of normal BFS : with current node u and it's child v, I check if (d[v] < d[u]+1) then update d[v], push v in the queue and I'm done (Oh sorry, I should have mention that d[i] is the longest path from 1 to i). This graph is guaranteed to have no circle, so how the heck did my code got 2 TLE

Here is my code ...

Pls leave some of your thought about this problem and what should I do to fix my code ... Can't see why BFS run forever.

  • Проголосовать: нравится
  • -7
  • Проголосовать: не нравится

»
3 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

if your sz(path) isn't typecasting to int. Then it will TLE in your last line where you are printing if size of path is 0. I don't think it should be the case here but just something to always take care of.

  • »
    »
    3 года назад, # ^ |
      Проголосовать: нравится +1 Проголосовать: не нравится

    I wish the problem is just that simple but ... In my code : ~~~~~

    define sz(x) (int)(x).size()

    ~~~~~

    My template is a little bit long so posting the entire code is unnecessary (I thought so).

»
3 года назад, # |
  Проголосовать: нравится +16 Проголосовать: не нравится

You can increase d[v] at most n times. So total complexity could be O(n^2)

»
3 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

If you are still looking for solutions, I might have one, but I didn't test it.

Solution