강의로 돌아가기
김영실

모든 노드는 연결되어있다라는.................제약사항이 있어야 할것 같습니다

제목이 곧 내용...................

작성중인 코드―Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import java.util.HashSet;
import java.util.ArrayList;
import java.util.Arrays;

class Solution {
    public int solution(int n, int[][] edge) {
        int answer = 0;
        int length = edge.length;
        int[][] line = new int[n][n];
        HashSet<Integer> hs = new HashSet<Integer>();
        ArrayList<Integer> al = new ArrayList<Integer>();
        int[] arr = new int[n];

        for(int i=0; i<length; i++){
            int a = edge[i][0] -1;
            int b = edge[i][1] -1;
            line[a][b] =1;
            line[b][a] =1;
        }

        hs.add(0);
        al.add(0);
        while(hs.size()<n){
            int v = al.remove(0);
            for(int i=0; i<n; i++){
                if(line[v][i] == 1 && !hs.contains(i)){
                    hs.add(i);
                    al.add(i);
                    arr[i] = arr[v]+1;
                }
            }
        }

        int max =0;
        int count = 0;
        for(int i=0; i<n; i++){
            if(max < arr[i]){
                max = arr[i];
                count = 1;
            }else if(max == arr[i]){
                count++;
            }
        }
        answer = count;

        return answer;
    }
}
  • -

    동감합니다! 방문할 수 없는 노드가 가장 먼 노드로 정의되는 것이 아니라면, 제목의 조건이 추가돼야합니다.

    -―Jun 06, 2021 20:35
1 개의 답변
-

간선이 1개 이상이라고 이미 주어져 있습니다

  • -

    노드의 개수가 (간선의 개수 + 1)보다 크면 모든 노드가 연결되지 않습니다.

    -―May 31, 2022 19:52
답변 쓰기
This input form supports markdown syntax. Please refer to 마크다운 가이드.