Блог пользователя NO..ONE..CARES

Автор NO..ONE..CARES, история, 9 лет назад, По-английски

I am getting Time Limit Exceeded for that problem. If anyone can please help. Thanks in advance :)

My approach: By calling N times BFS i got all pairs shortest path. Then by recursively i took all possible 4 nodes as there is no sub-problems so only backtracking can handle this.

Problem Link: http://mirror.codeforces.com/contest/667/problem/D

My solution Link: http://mirror.codeforces.com/contest/667/submission/17699475

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

»
9 лет назад, # |
Rev. 2   Проголосовать: нравится +1 Проголосовать: не нравится

I guess your solution's time complexity is actually O(N ^ 4), because you are calling your recursion O(N ^ 3) times and recursion itself works for O(N), so it's obviously getting TLE.

The main idea of one of the correct solutions are : bruteforce the vertices B and C. Now lets find such vertices A and D so that the way A -> B -> C -> D is the longest one (for fixed vertices B and C). We can do that by holding two sorted arrays for each vertex V0 : one will store lenghts U -> V0, other will store lenghts V0 -> U.

Now the solution's time complexity is only O((N ^ 2) + (N * M)), because we need to run N DFS's (or BFS's, whatever) and brute all vertices pairs B and C (we can find A and D for O(1)).

Code : http://pastebin.com/aSvmNR6s

Sorry for my bad english :/