This page is still under construction.

Parts of this page are still being built. What you see may change.

Minimum Median Spanning Tree

Time limit8sMemory limit256 MB

Summary
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 nn is even, a spanning tree has n−1n-1 edges, an odd count. Sort those costs in non-decreasing order; the median is the value in position n/2n/2.

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 nn (2≤n≤10002 \le n \le 1000) and an integer mm (n−1≤m≤10000n-1 \le m \le 10000). Here nn is the number of nodes and mm is the number of edges.

Each of the next mm lines holds sis_i, tit_i and cic_i (1≤si≤n1 \le s_i \le n, 1≤ti≤n1 \le t_i \le n, si≠tis_i \ne t_i, 1≤ci≤10001 \le c_i \le 1000). This means an edge of cost cic_i joins nodes sis_i and tit_i. No two edges join the same pair of nodes. The graph of every dataset is connected.

The input ends with a line where nn and mm are both 00. Print nothing for that line.

Output

For each dataset, print the median on its own line.

Examples1

  1. Example 1

    Input
    2 1
    1 2 5
    4 6
    1 2 1
    1 3 2
    1 4 3
    2 3 4
    2 4 5
    3 4 6
    8 17
    1 4 767
    3 1 609
    8 3 426
    6 5 972
    8 1 607
    6 4 51
    5 1 683
    3 6 451
    3 4 630
    8 7 912
    3 7 43
    4 7 421
    3 5 582
    8 4 538
    5 7 832
    1 6 345
    8 2 608
    0 0
    
    Expected output
    5
    2
    421