#P4234. 最小差值生成树

    ID: 3161 远端评测题 3000ms 256MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>图论O2优化枚举,暴力深度优先搜索,DFS生成树Link-Cut Tree,LCT

最小差值生成树

Description

Given an undirected graph with vertices labeled from 11 to nn and mm edges, find a spanning tree that minimizes the difference between the maximum and minimum edge weights in the tree. The graph may contain self-loops.

Input Format

The first line contains two integers, the number of vertices nn and the number of edges mm.

Then follow mm lines. Each line contains three integers u,v,wu, v, w, indicating there is an edge between uu and vv with weight ww.

Output Format

Output a single integer on one line, which is the answer.

4 6 
1 2 10 
1 3 100 
1 4 90 
2 3 20 
2 4 80 
3 4 40 

20

Hint

Constraints and Conventions

  • For 30%30\% of the testdata, it is guaranteed that n100n \leq 100, m103m \leq 10^3.
  • For 97%97\% of the testdata, it is guaranteed that n500n \leq 500, m105m \leq 10^5.
  • For 100%100\% of the testdata, it is guaranteed that 1n5×1041 \leq n \leq 5 \times 10^4, 1m2×1051 \leq m \leq 2 \times 10^5, 1u,vn1 \leq u, v \leq n, 1w1041 \leq w \leq 10^4.

Translated by ChatGPT 5