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

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

Hello,

I'm trying to solve the last problem in this contest.

and this is my code, my idea is to change every strongly connected component into one node because the cost of travailing between it's nodes is ZERO, this can be done by removing all bridges and then applying union find on elements of the other edges.

Then I create new adjList using elements of the removed edges "i.e bridges", the new graph will form a tree.

Then I apply DFS two times to find the longest path in a tree.

Last step is to follow the longest path and save the sum of edges on the sides of each node, take the maximum sum between the left and right sum.

Sometimes it returns TLE and sometimes RTE.

What could be the mistake, and is there a better way to solve it.

Thanks.

EDIT: now this code gets WA

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

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

You should use Tarjan's Algorithm to find all the strongly connected components instead. Compress every strongly connected component into a single node and the graph will become a tree. Then just dfs finding the longest distance of each node and choose the lowest one.

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

Auto comment: topic has been updated by OmaeWaMouShenDeiru (previous revision, new revision, compare).

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

I solved this problem using Tarjan's Algorithm .

you can modify it for this problem , see my code my code .

then I find longest path in tree just like you do .

OmaeWaMouShenDeiru