티스토리 뷰
풀이
1) 인접리스트를 활용한다
2) dfs 탐색
3) 최댓값 구하기
주의사항
1) 방문체크 및 복구
package com.SWEA;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;
/**
* @author yhs
* @date 2020. 12. 3
* @see
* @mem
* @time
* @caution
* [고려사항]
* 1) 경로의 길이는 경로 상에 등장하는 정점의 개수
* [입력사항]
* [출력사항]
*/
public class SWEA_D3_2814_최장경로 {
static BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer tokens;
static int T,N,M,arr[], max = Integer.MIN_VALUE;
static boolean isVisited[];
static List<Integer> graph[];
public static void main(String[] args) throws NumberFormatException, IOException {
input = new BufferedReader(new StringReader(src));
T = Integer.parseInt(input.readLine());
for(int t=1; t<=T; t++) {
tokens = new StringTokenizer(input.readLine());
N = Integer.parseInt(tokens.nextToken());
M = Integer.parseInt(tokens.nextToken());
graph = new List[N+1];
isVisited = new boolean[N+1];
for(int i=1; i<N+1; i++) {
graph[i] = new ArrayList<Integer>();
}
for(int m=0; m<M; m++) {
tokens = new StringTokenizer(input.readLine());
int from = Integer.parseInt(tokens.nextToken());
int to = Integer.parseInt(tokens.nextToken());
graph[from].add(to);
graph[to].add(from);
}
for(int i=1; i<N+1; i++) {
isVisited[i] = true;
dfs(i,1);
isVisited[i] = false;
}
System.out.println("#"+t+" "+max);
max = Integer.MIN_VALUE;
}
}
private static void dfs(int start, int cnt) {
if(cnt > max) {
max = cnt;
}
List<Integer> childs = graph[start];
for(int i=0; i<childs.size(); i++) {
Integer child = childs.get(i);
if(!isVisited[child]) {
isVisited[child] = true;
dfs(child,cnt+1);
isVisited[child] = false;
}
}
}
static String src =
"2\r\n" +
"1 0\r\n" +
"3 2\r\n" +
"1 2\r\n" +
"3 2";
}
'Algorithm' 카테고리의 다른 글
[SWEA] D4 8382 방향 전환 (java) (0) | 2020.12.16 |
---|---|
[SWEA] D3 6808 규영이와 인영이의 카드게임 (java) (0) | 2020.12.16 |
[SWEA] 1949 등산로 조성 (java) (0) | 2020.12.16 |
[SWEA] 5658 보물상자 비밀번호 (java) (0) | 2020.12.16 |
[백준] G4 15685 드래곤 커브 (java) (0) | 2020.12.16 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- S2
- 알고리즘
- 백트래킹
- map
- 다익스트라
- G5
- 리액트
- 자바
- 구현
- 현꾸라지
- DFS
- react
- BFS
- Spring Boot
- g4
- SWEA
- PriorityQueue
- 문자열
- 코딩새내기
- laugh4mile
- java
- 객체지향
- 백준
- 리액트 네이티브
- react native
- Spring
- 시뮬레이션
- 그리디
- S3
- 우선순위큐
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함