結果

問題 No.1718 Random Squirrel
ユーザー tentententen
提出日時 2021-10-28 10:06:51
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,646 ms / 2,000 ms
コード長 4,989 bytes
コンパイル時間 2,845 ms
コンパイル使用メモリ 80,492 KB
実行使用メモリ 111,924 KB
最終ジャッジ日時 2024-04-16 13:01:48
合計ジャッジ時間 24,672 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 53 ms
37,204 KB
testcase_01 AC 54 ms
37,236 KB
testcase_02 AC 53 ms
37,076 KB
testcase_03 AC 54 ms
37,072 KB
testcase_04 AC 53 ms
37,264 KB
testcase_05 AC 52 ms
36,872 KB
testcase_06 AC 54 ms
36,924 KB
testcase_07 AC 53 ms
37,312 KB
testcase_08 AC 54 ms
36,840 KB
testcase_09 AC 56 ms
37,076 KB
testcase_10 AC 54 ms
37,076 KB
testcase_11 AC 705 ms
62,552 KB
testcase_12 AC 363 ms
51,388 KB
testcase_13 AC 675 ms
59,532 KB
testcase_14 AC 760 ms
65,744 KB
testcase_15 AC 1,062 ms
68,996 KB
testcase_16 AC 591 ms
55,808 KB
testcase_17 AC 636 ms
60,524 KB
testcase_18 AC 1,355 ms
84,452 KB
testcase_19 AC 955 ms
67,288 KB
testcase_20 AC 345 ms
51,892 KB
testcase_21 AC 964 ms
71,600 KB
testcase_22 AC 1,646 ms
91,764 KB
testcase_23 AC 1,117 ms
76,564 KB
testcase_24 AC 979 ms
71,708 KB
testcase_25 AC 1,606 ms
92,264 KB
testcase_26 AC 699 ms
73,524 KB
testcase_27 AC 755 ms
74,220 KB
testcase_28 AC 686 ms
73,172 KB
testcase_29 AC 602 ms
70,840 KB
testcase_30 AC 1,070 ms
111,924 KB
testcase_31 AC 583 ms
70,856 KB
testcase_32 AC 611 ms
71,100 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
    static ArrayList<ArrayList<Integer>> part = new ArrayList<>();
    static int[] counts;
    static int k;
    static int base;
    static int total = 0;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        k = sc.nextInt();
        counts = new int[n];
        for (int i = 0; i < n; i++) {
            graph.add(new ArrayList<>());
            part.add(new ArrayList<>());
        }
        for (int i = 0; i < n - 1; i++) {
            int a = sc.nextInt() - 1;
            int b = sc.nextInt() - 1;
            graph.get(a).add(b);
            graph.get(b).add(a);
        }
        int single = 0;
        for (int i = 0; i < k; i++) {
            single = sc.nextInt() - 1;
            counts[single] = 1;
        }
        PriorityQueue<Path> queue = new PriorityQueue<>();
        if (k == 1) {
            queue.add(new Path(single, 0));
        } else {
            getCount(0, 0);
            int[] costsA = new int[n];
            queue.add(new Path(base, 0));
            doBTS(queue, costsA);
            int maxA = 0;
            int maxAIdx = 0;
            for (int i = 0; i < n; i++) {
                if (costsA[i] < Integer.MAX_VALUE && maxA < costsA[i]) {
                    maxA = costsA[i];
                    maxAIdx = i;
                }
            }
            int[] costsB = new int[n];
            queue.add(new Path(maxAIdx, 0));
            doBTS(queue, costsB);
            int maxB = 0;
            int maxBIdx = 0;
            for (int i = 0; i < n; i++) {
                if (costsB[i] < Integer.MAX_VALUE && maxB < costsB[i]) {
                    maxB = costsB[i];
                    maxBIdx = i;
                }
            }
            int[] costsC = new int[n];
            queue.add(new Path(maxBIdx, 0));
            doBTS(queue, costsC);
            int maxC = 0;
            int maxCIdx = 0;
            for (int i = 0; i < n; i++) {
                if (costsC[i] < Integer.MAX_VALUE && maxC < costsC[i]) {
                    maxC = costsC[i];
                    maxCIdx = i;
                }
            }
            for (int i = 0; i < n; i++) {
                if (costsA[i] == Integer.MAX_VALUE) {
                    continue;
                }
                queue.add(new Path(i, total * 2 - Math.max(costsB[i], costsC[i])));
            }
        }
        int[] ans = new int[n];
        Arrays.fill(ans, Integer.MAX_VALUE);
        while (queue.size() > 0) {
            Path p = queue.poll();
            if (ans[p.idx] <= p.value) {
                continue;
            }
            ans[p.idx] = p.value;
            for (int x : graph.get(p.idx)) {
                queue.add(new Path(x, p.value + 1));
            }
        }
        StringBuilder sb = new StringBuilder();
        for (int x : ans) {
            sb.append(x).append("\n");
        }
        System.out.print(sb);
    }
    
    static int getCount(int idx, int p) {
        if (counts[idx] == 1) {
            base = idx;
        }
        for (int x : graph.get(idx)) {
            if (x == p) {
                continue;
            }
            int tmp = getCount(x, idx);
            if (tmp > 0 && tmp < k) {
                part.get(idx).add(x);
                part.get(x).add(idx);
                total++;
            }
            counts[idx] += tmp;
        }
        return counts[idx];
    }
    
    static void doBTS(PriorityQueue<Path> queue, int[] costs) {
        Arrays.fill(costs, Integer.MAX_VALUE);
        while (queue.size() > 0) {
            Path p = queue.poll();
            if (costs[p.idx] <= p.value) {
                continue;
            }
            costs[p.idx] = p.value;
            for (int x : part.get(p.idx)) {
                queue.add(new Path(x, p.value + 1));
            }
        }
    }
    
    static class Path implements Comparable<Path> {
        int idx;
        int value;
        
        public Path(int idx, int value) {
            this.idx = idx;
            this.value = value;
        }
        
        public int compareTo(Path another) {
            return value - another.value;
        }
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public String nextLine() throws Exception {
        return br.readLine();
    }
    
    public String next() throws Exception {
        if (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0