|
| 1 | +``` |
| 2 | +
|
| 3 | +import java.util.*; |
| 4 | +import java.io.*; |
| 5 | +
|
| 6 | +public class Main { |
| 7 | +
|
| 8 | + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 9 | + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 10 | + static StringTokenizer st = new StringTokenizer(""); |
| 11 | + |
| 12 | + static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());} |
| 13 | + static int nextInt() throws Exception { |
| 14 | + if(!st.hasMoreTokens()) nextLine(); |
| 15 | + return Integer.parseInt(st.nextToken()); |
| 16 | + } |
| 17 | + static long nextLong() throws Exception { |
| 18 | + if(!st.hasMoreTokens()) nextLine(); |
| 19 | + return Long.parseLong(st.nextToken()); |
| 20 | + } |
| 21 | + static void ioClose() throws Exception {bw.flush();bw.close();br.close();} |
| 22 | + |
| 23 | + /* |
| 24 | + * [solving strategy] |
| 25 | + * 친구들을 정점으로, 친구 관계를 간선으로 보고 인접 리스트 방식의 그래프 V를 생성합니다. |
| 26 | + * 1의 친구들은 모두 V[1]에 들어있고, 그 친구들까지 V에서 찾아냅니다. |
| 27 | + * |
| 28 | + * [description] |
| 29 | + * - N, M : 정점, 간선의 개수 |
| 30 | + * - V : 인접 리스트 |
| 31 | + * |
| 32 | + * - ready() : input 처리 및 인접 리스트로 그래프 생성 |
| 33 | + * - solve() : 친구, 친구의 친구 탐색 -> 정답 출력 |
| 34 | + */ |
| 35 | + |
| 36 | + static int N, M; |
| 37 | + static List<Integer>[] V; |
| 38 | + |
| 39 | + public static void main(String[] args) throws Exception { |
| 40 | + //--------------솔루션 코드를 작성하세요.-------------------------------- |
| 41 | + |
| 42 | + ready(); |
| 43 | + solve(); |
| 44 | + |
| 45 | + ioClose(); |
| 46 | + |
| 47 | + } |
| 48 | + |
| 49 | + static void ready() throws Exception { |
| 50 | + |
| 51 | + N = nextInt(); |
| 52 | + M = nextInt(); |
| 53 | + V = new List[N+1]; |
| 54 | + for(int i=1;i<=N;i++) V[i] = new ArrayList<>(); |
| 55 | + |
| 56 | + for(int i=1;i<=M;i++) { |
| 57 | + int a = nextInt(), b = nextInt(); |
| 58 | + V[a].add(b); |
| 59 | + V[b].add(a); |
| 60 | + } |
| 61 | + |
| 62 | + } |
| 63 | + |
| 64 | + static void solve() throws Exception { |
| 65 | + |
| 66 | + boolean[] chk = new boolean[N+1]; |
| 67 | + chk[1] = true; |
| 68 | + for(int i:V[1]) { |
| 69 | + chk[i] = true; |
| 70 | + for(int j:V[i]) chk[j] = true; |
| 71 | + } |
| 72 | + int cnt = 0; |
| 73 | + for(int i=2;i<=N;i++) cnt += chk[i] ? 1 : 0; |
| 74 | + bw.write(cnt + "\n"); |
| 75 | + |
| 76 | + } |
| 77 | +
|
| 78 | +} |
| 79 | +
|
| 80 | +
|
| 81 | +``` |
0 commit comments