結果

問題 No.994 ばらばらコイン
ユーザー Mishan5055Mishan5055
提出日時 2020-03-27 17:47:59
言語 Java21
(openjdk 21)
結果
AC  
実行時間 749 ms / 2,000 ms
コード長 2,102 bytes
コンパイル時間 2,154 ms
コンパイル使用メモリ 74,380 KB
実行使用メモリ 66,992 KB
最終ジャッジ日時 2023-08-30 16:37:39
合計ジャッジ時間 12,512 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 125 ms
53,956 KB
testcase_01 AC 141 ms
55,960 KB
testcase_02 AC 694 ms
66,464 KB
testcase_03 AC 738 ms
66,848 KB
testcase_04 AC 697 ms
66,696 KB
testcase_05 AC 558 ms
60,116 KB
testcase_06 AC 749 ms
66,992 KB
testcase_07 AC 745 ms
66,812 KB
testcase_08 AC 594 ms
64,108 KB
testcase_09 AC 711 ms
66,636 KB
testcase_10 AC 621 ms
65,308 KB
testcase_11 AC 682 ms
66,348 KB
testcase_12 AC 700 ms
66,800 KB
testcase_13 AC 126 ms
55,676 KB
testcase_14 AC 128 ms
55,820 KB
testcase_15 AC 128 ms
55,984 KB
testcase_16 AC 125 ms
55,860 KB
testcase_17 AC 126 ms
55,588 KB
testcase_18 AC 124 ms
55,552 KB
testcase_19 AC 124 ms
55,444 KB
testcase_20 AC 127 ms
55,572 KB
testcase_21 AC 126 ms
55,908 KB
testcase_22 AC 126 ms
55,888 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

class Main{
    static int INF=100000007;
    static class edge implements Comparable<edge>{
        int from;
        int to;
        
        void edge(int from,int to){
            this.from=from;
            this.to=to;
        }
        
        public int compareTo(edge e){
            return this.from-e.from;
        }
    }
    
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        int N=sc.nextInt();
        int K=sc.nextInt();
        edge[] graph=new edge[N-1];
        int[] dist=new int[N];
        
        for(int i=0;i<N;i++){
            dist[i]=INF;
        }
        
        dist[0]=0;
        
        for(int i=0;i<N-1;i++){
            edge e=new edge();
            e.edge(sc.nextInt()-1,sc.nextInt()-1);
            graph[i]=e;
        }
        
        Arrays.sort(graph);
        
        boolean change=false;
        
        while(true){
            change=false;
            for(int i=0;i<N-1;i++){
                int start=graph[i].from;
                int end=graph[i].to;
                
                if(dist[start]!=INF&&dist[end]>dist[start]+1){
                    dist[end]=dist[start]+1;
                    change=true;
                }
            }
            
            if(!change){
                break;
            }
        }
        
        int count=0;
        
        for(int i=0;i<N;i++){
            if(dist[i]!=INF){
                count++;
            }
        }
        
        if(count<K){
            System.out.println("-1");
            return;
        }
        
        Arrays.sort(dist);
        int ans=0;
        int current=0;
        boolean check=false;
        
        for(int i=0;i<K;i++){
            current=dist[i];
            if(dist[i]==current&&check){
                ans++;
            }
            if(dist[i]==current&&!check){
                check=true;
            }
            if(dist[i]!=current){
                current=dist[i];
                check=false;
            }
        }
        
        System.out.println(ans);
    }
}
0