-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #129 from wwan13/boj/5567
[S2] 5567 결혼식
- Loading branch information
Showing
1 changed file
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package boj5567; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.util.ArrayList; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.Queue; | ||
|
||
public class Main { | ||
|
||
private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); | ||
|
||
private static List<List<Integer>> tree; | ||
private static boolean[] visited; | ||
|
||
public static void main(String[] args) { | ||
int n = Integer.parseInt(readLine()); | ||
int m = Integer.parseInt(readLine()); | ||
|
||
tree = new ArrayList<>(); | ||
for (int i = 0; i <= n; i++) { | ||
tree.add(new ArrayList<>()); | ||
} | ||
|
||
for (int i = 0; i < m; i++) { | ||
String[] line = readLine().split(" "); | ||
int to = Integer.parseInt(line[0]); | ||
int from = Integer.parseInt(line[1]); | ||
|
||
tree.get(to).add(from); | ||
tree.get(from).add(to); | ||
} | ||
visited = new boolean[n + 1]; | ||
System.out.println(bfs()); | ||
} | ||
|
||
private static int bfs() { | ||
Queue<Integer> queue = new LinkedList<>(); | ||
queue.add(1); | ||
visited[1] = true; | ||
|
||
int depth = 0; | ||
int count = 0; | ||
|
||
while (!queue.isEmpty() && depth < 2) { | ||
int size = queue.size(); | ||
depth++; | ||
|
||
for (int i = 0; i < size; i++) { | ||
int current = queue.poll(); | ||
|
||
for (int next : tree.get(current)) { | ||
if (!visited[next]) { | ||
visited[next] = true; | ||
queue.add(next); | ||
count++; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return count; | ||
} | ||
|
||
private static String readLine() { | ||
try { | ||
return reader.readLine(); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |