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

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

Given a graph with n(<=100) vertices and m( <=n(n-1)/2 ) edges, you are asked to find a spanning tree with the minimum difference between the biggest and the smallest edge of the tree.

The problem can be found Here (UVA 1395). I can't figure out an efficient way to solve this. Any kind of help is appreciated.

Update: Got AC using fake123_loves_me's approach. this is my code if anyone is interested.

Теги mst, dsu
  • Проголосовать: нравится
  • +8
  • Проголосовать: не нравится

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

You can do it like this. Sort edges by cost and then do something like two pointers with link-cut tree. while the vertexes will be connected cut the last edge(and make sure to add the last one you erased back). O(MlogN)

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

You can solve it with $$$O(M^2 \cdot \alpha(N))$$$ time complexity.

At the beginning let's sort edges by their cost. Now let's iterate through them and fix an edge with minimum cost that will be in our spanning tree. So now we should try to build a spanning tree using all edges from current to the last. We can do it with $$$O(M \cdot \alpha(n))$$$ time complexity with Kruskal's algorithm. When we build a spanning tree let's calculate the difference between the maximum and the minimum cost and compare it with our answer.

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

    That's brilliant! Thanks a lot.

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

    A more efficient soln does exists. Dynamic connectivity problem in $$$O(M.\alpha(N).\log(M))$$$

    Key Idea — If you want to increase weight of minimum edges you dont need to build a complete spanning tree again.

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

    Sort edges with weight We can use two pointer i = 0 for j = 0 .. n-1 while check(mini = W[i] , maxi = W[j]) //return if connected or not ans = min(ans , W[j] - W[i]) i++ //check function can be implemented in O(m) , like BFS // time Complexity : O(m^2 * 2)
»
5 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

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