-
카테고리
-
세부 분야
알고리즘 · 자료구조
-
해결 여부
미해결
이 로직은 안되나요?
21.09.09 20:00 작성 조회수 111
0
어떤 부분에서 로직이 틀린 건지 잘 모르겠습니다.
import java.util.ArrayList;
import java.util.Scanner;
public class Ch9_6 {
public static ArrayList<ArrayList<Integer>> list;
public static boolean flag = false;
public static void solution(int start, int end) {
if(start == end)
flag = true;
for(int friend : list.get(start)) {
solution(friend, end);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
list = new ArrayList<>();
for(int i = 0; i <= n; i++) {
list.add(new ArrayList<Integer>());
}
for(int i = 0; i < m-1; i++) {
int a = sc.nextInt();
int b = sc.nextInt();
list.get(a).add(b);
}
int start = sc.nextInt();
int end = sc.nextInt();
solution(start, end);
if(flag)
System.out.println("YES");
else
System.out.println("NO");
}
}
답변을 작성해보세요.
0
김태원
지식공유자2021.09.11
안녕하세요^^
연결리스트를 만들때 양방향으로 만드셔야 합니다.
그리고 그래프는 회로가 있을 수 있으니 방문한 정점은 체크를 해서 방문하지 않아야 합니다.
이 문제는 유니온&파인드를 배우는 것이니 영상의 방법대로 배우셨으면 합니다.
답변 1