結果

問題 No.1488 Max Score of the Tree
ユーザー shojin_proshojin_pro
提出日時 2021-04-23 22:30:27
言語 Java21
(openjdk 21)
結果
AC  
実行時間 197 ms / 2,000 ms
コード長 5,292 bytes
コンパイル時間 3,467 ms
コンパイル使用メモリ 77,760 KB
実行使用メモリ 130,288 KB
最終ジャッジ日時 2023-09-17 12:37:16
合計ジャッジ時間 8,443 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 164 ms
126,012 KB
testcase_01 AC 164 ms
123,780 KB
testcase_02 AC 168 ms
126,064 KB
testcase_03 AC 171 ms
130,120 KB
testcase_04 AC 173 ms
130,268 KB
testcase_05 AC 49 ms
49,424 KB
testcase_06 AC 97 ms
74,700 KB
testcase_07 AC 135 ms
107,956 KB
testcase_08 AC 113 ms
82,596 KB
testcase_09 AC 99 ms
76,172 KB
testcase_10 AC 134 ms
105,832 KB
testcase_11 AC 197 ms
130,032 KB
testcase_12 AC 52 ms
49,444 KB
testcase_13 AC 87 ms
64,672 KB
testcase_14 AC 121 ms
88,612 KB
testcase_15 AC 101 ms
74,648 KB
testcase_16 AC 74 ms
56,740 KB
testcase_17 AC 88 ms
64,372 KB
testcase_18 AC 136 ms
108,496 KB
testcase_19 AC 111 ms
83,368 KB
testcase_20 AC 87 ms
64,252 KB
testcase_21 AC 74 ms
56,328 KB
testcase_22 AC 100 ms
74,388 KB
testcase_23 AC 46 ms
49,404 KB
testcase_24 AC 45 ms
49,360 KB
testcase_25 AC 47 ms
49,352 KB
testcase_26 AC 91 ms
68,300 KB
testcase_27 AC 72 ms
54,568 KB
testcase_28 AC 78 ms
61,328 KB
testcase_29 AC 86 ms
64,520 KB
testcase_30 AC 156 ms
113,920 KB
testcase_31 AC 191 ms
130,288 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.io.*;

public class Main {
    static FastScanner sc = new FastScanner(System.in);
    static PrintWriter pw = new PrintWriter(System.out);
    static StringBuilder sb = new StringBuilder();
    static long mod = (long) 1e9 + 7;
    static ArrayList<ArrayList<Edge>> map;
    static int[] cnt;
    static boolean[] arrived;
    public static void main(String[] args) throws Exception {
        solve();
        pw.flush();
    }

    public static void solve() {
        int n = sc.nextInt(),K = sc.nextInt();
        Edge[] arr = new Edge[n-1];
        map = new ArrayList<>();
        for(int i = 0; i < n; i++){
            map.add(new ArrayList<>());
        }
        for(int i = 0; i < n-1; i++){
            int f = sc.nextInt()-1;
            int t = sc.nextInt()-1;
            int k = sc.nextInt();
            map.get(f).add(new Edge(t,i,k));
            map.get(t).add(new Edge(f,i,k));
            arr[i] = new Edge(t,i,k);
        }
        cnt = new int[n-1];
        arrived = new boolean[n];
        arrived[0] = true;
        dfs(0,new HashSet<Integer>());
        //pw.println(ArraysToString(cnt));
        long[][] dp = new long[n][K+1];
        for(int i = 0; i < n-1; i++){
            Edge e = arr[i];
            int dist = e.d;
            for(int j = 0; j <= K; j++){
                dp[i+1][j] = Math.max(dp[i+1][j],dp[i][j]+(cnt[i]*dist));
                if(j+dist <= K)dp[i+1][j+dist] = Math.max(dp[i+1][j+dist],dp[i][j]+(2*cnt[i]*dist));
            }
        }
        long ans = 0;
        for(int i = 0; i <= K; i++) ans = Math.max(ans,dp[n-1][i]);
        pw.println(ans);
    }
    
    private static void dfs(int now, HashSet<Integer> keep){
        for(Edge e : map.get(now)){
            int next = e.t;
            int index = e.i;
            if(!arrived[next]){
                arrived[next] = true;
                keep.add(index);
                dfs(next,keep);
                keep.remove(index);
            }
        }
        if(map.get(now).size() == 1 && now != 0){
            for(int a : keep){
                cnt[a]++;
            }
        }
    }
    
    private static String ArraysToString(int[] arr){
        String s = Arrays.toString(arr);
        s = s.replaceAll(",","");
        s = s.substring(1,s.length()-1);
        return s;
    }

    static class GeekInteger {
        public static void save_sort(int[] array) {
            shuffle(array);
            Arrays.sort(array);
        }

        public static int[] shuffle(int[] array) {
            int n = array.length;
            Random random = new Random();
            for (int i = 0, j; i < n; i++) {
                j = i + random.nextInt(n - i);
                int randomElement = array[j];
                array[j] = array[i];
                array[i] = randomElement;
            }
            return array;
        }

        public static void save_sort(long[] array) {
            shuffle(array);
            Arrays.sort(array);
        }

        public static long[] shuffle(long[] array) {
            int n = array.length;
            Random random = new Random();
            for (int i = 0, j; i < n; i++) {
                j = i + random.nextInt(n - i);
                long randomElement = array[j];
                array[j] = array[i];
                array[i] = randomElement;
            }
            return array;
        }

    }
    
    static class Edge{
    	int t,i,d;
    	public Edge(int t, int i, int d) {
    		this.t = t;
    		this.i = i;
    		this.d = d;
    	}
    }
}

class FastScanner {
    private BufferedReader reader = null;
    private StringTokenizer tokenizer = null;

    public FastScanner(InputStream in) {
        reader = new BufferedReader(new InputStreamReader(in));
        tokenizer = null;
    }

    public FastScanner(FileReader in) {
        reader = new BufferedReader(in);
        tokenizer = null;
    }

    public String next() {
        if (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(reader.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken();
    }

    public String nextLine() {
        if (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                return reader.readLine();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken("\n");
    }

    public long nextLong() {
        return Long.parseLong(next());
    }

    public int nextInt() {
        return Integer.parseInt(next());
    }

    public double nextDouble() {
        return Double.parseDouble(next());
    }

    public String[] nextArray(int n) {
        String[] a = new String[n];
        for (int i = 0; i < n; i++)
            a[i] = next();
        return a;
    }

    public int[] nextIntArray(int n) {
        int[] a = new int[n];
        for (int i = 0; i < n; i++)
            a[i] = nextInt();
        return a;
    }

    public long[] nextLongArray(int n) {
        long[] a = new long[n];
        for (int i = 0; i < n; i++)
            a[i] = nextLong();
        return a;
    }
}
0