회원가입
로그인
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
글쓰기
질문 도움말
자주묻는 질문
4퍼에서 시간초과 나오네요..
14218번 - 그래프 탐색 2
aa2002dcks
2년 전
0
재귀 아니면 어떻게 해야 할 지 모르겠는데..
그리고 왜 시간초과 나는 거죠...
import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Scanner; public class back_14218 { static void bfsdo(int[] check, Node map[], int count, int start) { check[start] = count++; for (int w : map[start].roadto) { if(check[w]<count) continue; bfsdo(check, map, count, w); } } static class Node { public List<Integer> roadto; public Node() { this.roadto = new ArrayList<>(); } } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int city = scanner.nextInt(); int road = scanner.nextInt(); Node map[] = new Node[city + 1]; int check[] = new int[city + 1]; for (int i = 0; i < city + 1; i++) map[i] = new Node(); for (int i = 0; i < road; i++) { int city1 = scanner.nextInt(); int city2 = scanner.nextInt(); map[city1].roadto.add(city2); Collections.sort(map[city1].roadto); } int newroad = scanner.nextInt(); for (int i = 0; i < newroad; i++) { int count = 0; int city1 = scanner.nextInt(); int city2 = scanner.nextInt(); map[city1].roadto.add(city2); Collections.sort(map[city1].roadto); Arrays.fill(check, Integer.MAX_VALUE); bfsdo(check, map, count, 1); for (int j = 1; j <= city; j++) { if (j != 1 && check[j] == Integer.MAX_VALUE) { System.out.print(-1 + " "); continue; } System.out.print(check[j] + " "); } System.out.println(); } } }
댓글을 작성하려면
로그인
해야 합니다.
aa2002dcks 2년 전
재귀 아니면 어떻게 해야 할 지 모르겠는데..
그리고 왜 시간초과 나는 거죠...