회원가입
로그인
Toggle navigation
문제
문제
전체 문제
문제 출처
단계별로 풀어보기
알고리즘 분류
추가된 문제
문제 순위
문제
푼 사람이 한 명인 문제
아무도 못 푼 문제
최근 제출된 문제
최근 풀린 문제
랜덤
출처
ICPC
Olympiad
한국정보올림피아드
한국정보올림피아드시․도지역본선
전국 대학생 프로그래밍 대회 동아리 연합
대학교 대회
카카오 코드 페스티벌
Coder's High
ICPC
Regionals
World Finals
Korea Regional
Africa and the Middle East Regionals
Europe Regionals
Latin America Regionals
North America Regionals
South Pacific Regionals
문제집
대회
채점 현황
게시판
그룹
더 보기
재채점 기록
블로그
도움말
전체
공지
자유
질문
오타/오역/요청
게시판 공지
홍보
업데이트
solved.ac
글쓰기
질문 도움말
자주묻는 질문
예제는 성공하는대..
24482번 - 알고리즘 수업 - 깊이 우선 탐색 4
hwyeom
3년 전
0
어느부분을 놓치고 있는지 알수있을까요..?
import java.io.IOException; import java.util.PriorityQueue; import java.util.Scanner; public class Main { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); String[] input = sc.nextLine().split(" "); int N = Integer.parseInt(input[0]); int M = Integer.parseInt(input[1]); int R = Integer.parseInt(input[2]); Graph g = new Graph(N); for(int i=0; i < M ; i++) { String[] edges = sc.nextLine().split(" "); g.addEdge(Integer.parseInt(edges[0]), Integer.parseInt(edges[1])); } g.dfsR(R); Graph.Node[] nodes = g.nodes; for(int i=1; i < nodes.length; i++) System.out.println(nodes[i].depth); } } class Graph{ class Node{ int data; PriorityQueue<Node> adjacent; boolean marked; int depth; Node (int data){ depth = -1; this.data = data; this.marked = false; adjacent = new PriorityQueue<Graph.Node>((o1, o2) -> { return o2.data - o1.data;}); } } Node[] nodes; Graph(int size){ nodes = new Node[size+1]; for(int i=0; i < size+1; i++) { nodes[i] = new Node(i); } } void addEdge(int i1, int i2) { Node n1 = nodes[i1]; Node n2 = nodes[i2]; if(!n1.adjacent.contains(n2)) { n1.adjacent.add(n2); } if(!n2.adjacent.contains(n1)) { n2.adjacent.add(n1); } } void dfsR(Node r, int depth) { if(r == null) return; r.marked = true; r.depth = depth++; visit(r); for(Node n : r.adjacent) { if(n.marked == false) dfsR(n, depth); } } void dfsR(int index) { dfsR(nodes[index], 0); } void visit(Node node) { //System.out.println(node.data + " " + node.depth); } }
댓글을 작성하려면
로그인
해야 합니다.
hwyeom 3년 전
어느부분을 놓치고 있는지 알수있을까요..?