結果
| 問題 |
No.994 ばらばらコイン
|
| コンテスト | |
| ユーザー |
joybeeee
|
| 提出日時 | 2020-05-14 22:28:40 |
| 言語 | Java (openjdk 23) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,137 bytes |
| コンパイル時間 | 2,537 ms |
| コンパイル使用メモリ | 79,848 KB |
| 実行使用メモリ | 93,220 KB |
| 最終ジャッジ日時 | 2024-09-16 03:16:52 |
| 合計ジャッジ時間 | 15,585 ms |
|
ジャッジサーバーID (参考情報) |
judge6 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 7 WA * 16 |
ソースコード
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
Map<Integer, List<Integer>> graph = new HashMap<>();
for(int i = 1; i <= n; i++)
graph.put(i, new ArrayList<>());
for(int i = 0; i < n-1; i++) {
int a = sc.nextInt();
int b = sc.nextInt();
graph.get(a).add(b);
graph.get(b).add(a);
}
Queue<Integer> queue = new LinkedList<>();
queue.offer(1);
Set<Integer> visited = new HashSet<>();
visited.add(1);
k--;
int steps = 0;
if(k == 0) {
System.out.println(steps);
return;
}
while(!queue.isEmpty()) {
int node = queue.poll();
List<Integer> adj = graph.get(node);
for(int adjNode : adj) {
if(visited.contains(adjNode)) continue;
visited.add(adjNode);
k--;
steps++;
if(k == 0) {
System.out.println(steps);
return;
}
}
}
System.out.println(-1);
}
}
joybeeee