链式前向星

Nodes from the Root

Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other)

Total Submission(s) : 40   Accepted Submission(s) : 9

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

Problem Description

There is a binary tree with N nodes indexing from 0 to N-1, where 0 is the root. Each edge of the tree has an integer weight W. At first all the nodes could be reached from the root, through a sequence of edges.
An integer threshold X (X >= 0) is used to close the edge, which means that all the edges whose weights are less than X will be closed.
Given the tree and an integer Y, please find the minimum threshold X so that the number of nodes reachable from the root (including the root itself) is no more than Y.

Input

The first line is an interger T, indicating the number of test cases.
Then in each case:
The first line contains one integer N (N ≤ 2e4), representing the number of nodes in the tree.
The second line contains one integer Y (0 ≤ Y ≤ N), representing the maximum number of nodes allowed to be reachable from the root.
Each of the following N-1 lines contains three integers U, V (0 ≤ U, V < N), W(0 ≤ W ≤ 1e7), representing that the edge between node U and node V has a weight W. The integers are separated by a space.
The total sum of N in all cases is less than 1e5.

Output

T lines and each line contains an integer, representing the minimum threshold X (X≥0).

Sample Input

2
3 2
0 1 2
0 2 3
6 3
0 1 8
0 2 3
1 3 2
1 4 5
2 5 6

Sample Output

3
4

用到了链式前向星见:https://www.cnblogs.com/Emcikem/p/11541489.html

链式前向星_第1张图片

package geeksun;


import java.lang.reflect.Array;
import java.util.*;

public class Main{
    static class Node{
        int next;
        int to;
        int w;
    }

    static int top;
    static Node[] nodes;
    static int[] head;
    static int[] minW;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int T = scanner.nextInt();
        while (T-- > 0){
            int N = scanner.nextInt();
            int Y = scanner.nextInt();
            top = 0;
            nodes = new Node[N * 2];
            head = new int[N];
            Arrays.fill(head, -1);
            minW = new int[N];
            for(int i = 0;i < N - 1;i++){
                int U = scanner.nextInt();
                int V = scanner.nextInt();
                int W = scanner.nextInt();
                addEdge(U, V, W);
            }
            dfs(0, -1, Integer.MAX_VALUE);
            Arrays.sort(minW);
            if(N == Y){
                System.out.println(0);
                continue;
            }
            System.out.println(minW[N - Y - 1] + 1);
        }
    }

    public static void dfs(int u, int pre, int m){
        minW[u] = m;
        for(int i = head[u];i != -1;i = nodes[i].next){
            int v = nodes[i].to;
            if(v == pre){
                continue;
            }
            dfs(v, u, Math.min(m, nodes[i].w));
        }
    }

    public static void addEdge(int u, int v, int w){
        nodes[top] = new Node();
        nodes[top].to = v;
        nodes[top].w = w;
        nodes[top].next = head[u];
        head[u] = top++;

        nodes[top] = new Node();
        nodes[top].to = u;
        nodes[top].w = w;
        nodes[top].next = head[v];
        head[v] = top++;
    }

}

 

你可能感兴趣的:(数据结构与算法)