ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1416. Confidential

O(N^2) solution...
Posted by Dima_Philippov 2 Dec 2011 02:23
What is the O(N^2) solution of this problem?

I wrote simply Kraskal & DFS with assimptoty O(E * Log(E) + V * E) and got Accepted in 1.156... I want to know O(N^2) solution of this problem...
Re: O(N^2) solution...
Posted by die_young 20 Jul 2018 21:41
My 0.093  N square solution:

1) Compute MST with Kruskal in E logE.
2) For each pair of vertices in MST compute dp[u][v] the cost of largest edge on path from u to v. This can be done by calling N depth first searches. DFS runs in O(E) and there is N - 1 edge in a MST. So this step takes (N^2) time.
3) For each edge (u, v) that isn't in a MST try to relax answer with { MST_COST - dp[u][v] + weight(u, v) }. This step is done in O(E).

So, the running time of this solution is O(ElogE + E + N^2) = O(N^2).