Minimum Median Spanning Tree
Time limit8sMemory limit256 MB
For each connected graph with an even node count, find the spanning tree whose median edge cost is smallest and report that median.
- Level
Medium7 of 10
- Topics
- Minimum spanning tree, Union-find, Sorting
- Solved
- No attempts yet
Problem
You are given a connected undirected graph whose number of nodes is even. In a connected graph every node reaches every other node along edges, either directly or through other nodes.
Among all spanning trees of the graph, find the one whose median edge cost is smallest and report that median. A spanning tree is a tree that contains every node of the graph.
Because is even, a spanning tree has edges, an odd count. Sort those costs in non-decreasing order; the median is the value in position .
Input
The input consists of several datasets. Each dataset has this format.
n m
s1 t1 c1
...
sm tm cm
The first line holds an even number () and an integer (). Here is the number of nodes and is the number of edges.
Each of the next lines holds , and (, , , ). This means an edge of cost joins nodes and . No two edges join the same pair of nodes. The graph of every dataset is connected.
The input ends with a line where and are both . Print nothing for that line.
Output
For each dataset, print the median on its own line.