BAEKJOON_13023) ABCDE
BAEKJOON
# 2019 SW역량테스트준비-기초
# 그래프와 BFS
13023) ABCDE (19.10.04)
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 
49 
50 
51 
52 
53 
54 
55 
 | 
import java.util.*; 
import java.io.*; 
class Main{ 
    public static boolean[] ch; 
    public static int[][] arr; 
    public static void go(int start, int n) { 
        System.out.println("start : " + start + " \n"); 
        if(ch[start]) return; 
        ch[start] = true; 
        for(int j=0;j<n;j++) { 
            System.out.println( "  j :" +  j ); 
            if(arr[start][j]==0) continue; 
            if(arr[start][j]==1 && ch[j]==false) { 
                go(j, n); 
                break; 
            } 
        } 
    } 
    public static void main(String args[]) throws IOException{ 
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); 
        StringTokenizer tk = new StringTokenizer(br.readLine()); 
        int n = Integer.parseInt(tk.nextToken()); 
        int m = Integer.parseInt(tk.nextToken()); 
        ch = new boolean[n]; 
        arr = new int[n][n]; 
        for(int i=0;i<m;i++) { 
            tk = new StringTokenizer(br.readLine()); 
            int u = Integer.parseInt(tk.nextToken()); 
            int v = Integer.parseInt(tk.nextToken()); 
            arr[u][v] = 1; 
            arr[v][u] = 1; 
        } 
        int ans = 0; 
        go(0, n); 
        for(boolean i : ch) { 
            bw.write(i+"  "); 
        } 
        bw.flush(); 
        bw.close(); 
    } 
} 
 | cs | 
* 문제를 이해 못하겠다...
* a-b-c-d-e 이렇게 다 연결된 거 구하는줄 알았는데,
* 두번째 예제는 a-b-c-d 하고 d는 b에만 연결되어있다.
* 그냥 떨어진거 없는 연결그래프는 아닌게
* 예제3은 a에 b, c, d, e, f가 연결된건데 이건 0이니까
* 뭐를 구하는 건지 모르겠다 ㅠㅠㅠ
* 일단 위 코드는 a-b-c-d-e 이렇게 연결된거로 구해보려고 작성한건데 맞는지는...
댓글
댓글 쓰기