Lady CA and the graph

Accepts: 1
Submissions: 18
Time Limit: 12000/6000 MS (Java/Others)
Memory Limit: 262144/262144 K (Java/Others)
Problem Description
Lady CA has a tree with $n$ points numbered $1,2,...,n$, and each edge has its weight. The unique route connecting two points is called a chain, and the length of a chain equals the sum value of the weights of the edges passed. The point number $m$ is called the root. Lady CA defines a special kind of chain called folded chain, the chain connecting the points numbered $x,y\left(x\neq y\right)$ is called a folded chain, if and only if the chain connecting the point numbered $x$ and the root doesn't pass the point numbered $y$, and the chain connecting the point numbered $y$ and the root doesn't pass the point numbered $x$. Lady CA wants to find the length of the $k$th longest folded chain. Notice that the chain connecting the points numbered $x,y$ and the chain connecting the points numbered $y,x$ are the same.
Input
The first line contains an integer $T\left(1\leq T\leq3\right)$¡ª¡ªThe number of the test cases. For each test case: The first line contains three integers $n\left(2\leq n \leq50,000\right),m\left(1\leq m \leq n\right),k\left(1\leq k \leq\frac{n\times\left(n-1\right)}{2}\right)$. Between each two adjacent integers there is a white space separated. The second line to the $n$th line describes the $n-1$ edges in the graph. Each line contains three integers $u,v\left(1\leq u,v\leq n,u\neq v\right),w\left(1\leq w\leq10,000\right)$, which means there is an edge which has a weight $w$ connecting the points numbered $u,v$. Between each two adjacent integers there is a white space separated.
Output
For each test case, the only line contains the only integer that is the length of the $k$th longest folded chain. If the $k$th longest folded chain doesn't exist, print NO.
Sample Input
2
5 1 3
1 2 8
1 5 4
2 3 5
2 4 6
5 1 5
1 2 8
1 5 4
2 3 5
2 4 6
Sample Output
12
NO
Hint
When and only when $x=2,y=5$ or $x=3,y=5$ or $x=4,y=5$ or $x=3,y=4$, the chain connecting the point numbered $x,y$ is a folded chain. The only 4 folded chains have lengths of 12,17,18,11, so the answer of the first test case, the length of the third longest folded chain is 12.