회원가입
로그인
Toggle navigation
문제
문제
전체 문제
문제 출처
단계별로 풀어보기
알고리즘 분류
새로 추가된 문제
새로 추가된 영어 문제
새로 추가된 문제 풀이
문제 순위
문제
푼 사람이 1명인 문제
아무도 못 푼 문제
최근 제출된 문제
최근 풀린 문제
랜덤
출처
ACM-ICPC
Olympiad
한국정보올림피아드
한국정보올림피아드시․도지역본선
전국 대학생 프로그래밍 대회 동아리 연합
대학교 대회
카카오 코드 페스티벌
Coder's High
ACM-ICPC
Regionals
World Finals
Korea Regional
Africa and the Middle East Regionals
Europe Regionals
Latin America Regionals
North America Regionals
South Pacific Regionals
문제집
대회
1
채점 현황
랭킹
게시판
그룹
블로그
강의
N
전체
공지
자유
질문
오타/오역/요청
게시판 공지
홍보
업데이트
글쓰기
만약 노드의 갯수가 적다면
1167번 - 트리의 지름
jsh5052
1년 전
0
이와 같이 코드를 짰는데 V가 10000개정도 밖에 받지 않는다 했을 때 이 코드로 구현이 가능한가요?
import java.util.*; class Main { static int[][] dist; static Vector<Integer>[] ans; static boolean[] visited; public static int treeDiameter(int totalDepth, int cur) { if(ans[cur].size()==0) return totalDepth; int ret = totalDepth; visited[cur] = true; for(int i = 0; i < ans[cur].size(); i++) { int next = ans[cur].get(i); if(visited[next]) continue; ret = Math.max(ret, treeDiameter(totalDepth+dist[cur][next], next)); } visited[cur] = false; return ret; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int V = sc.nextInt(); dist = new int[V+1][V+1]; ans = (Vector<Integer>[])new Vector[V+1]; for(int i = 1; i <= V; i++) { ans[i] = new Vector<Integer>(); } visited = new boolean[V+1]; while(V-->0) { int cur = sc.nextInt(); int ansCur = 0; int distance = 0; while(ansCur!=-1) { if(ansCur==0) ansCur = sc.nextInt(); ans[cur].add(ansCur); distance = sc.nextInt(); dist[cur][ansCur] = distance; ansCur = sc.nextInt(); } } System.out.println(treeDiameter(0, 1)); } }
댓글을 작성하려면
로그인
해야 합니다.
jsh5052 1년 전
이와 같이 코드를 짰는데 V가 10000개정도 밖에 받지 않는다 했을 때 이 코드로 구현이 가능한가요?