yosako's blog

By yosako, history, 3 years ago, In English

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.

  • Vote: I like it
  • -7
  • Vote: I do not like it

| Write comment?
»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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 years ago, # ^ |
      Vote: I like it +1 Vote: I do not like it

    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).

»
2 years ago, # |
  Vote: I like it +16 Vote: I do not like it

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

»
2 years ago, # |
  Vote: I like it 0 Vote: I do not like it

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

Solution