結果
問題 |
No.994 ばらばらコイン
|
ユーザー |
|
提出日時 | 2020-03-27 17:47:59 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 685 ms / 2,000 ms |
コード長 | 2,102 bytes |
コンパイル時間 | 2,117 ms |
コンパイル使用メモリ | 80,120 KB |
実行使用メモリ | 68,468 KB |
最終ジャッジ日時 | 2025-01-02 09:01:13 |
合計ジャッジ時間 | 11,660 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 23 |
ソースコード
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); } }