반응형
🔺 문제
1325번: 효율적인 해킹
첫째 줄에, N과 M이 들어온다. N은 10,000보다 작거나 같은 자연수, M은 100,000보다 작거나 같은 자연수이다. 둘째 줄부터 M개의 줄에 신뢰하는 관계가 A B와 같은 형식으로 들어오며, "A가 B를 신뢰한
www.acmicpc.net
🔺 코드
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
import java.util.*;
import java.io.*;
public class Main {
static int N, M;
static boolean visited[]; // 방문 배열
static ArrayList<Integer>[] A; // 인접 리스트
static int answer[]; // 정답 배열
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine()," ");
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
A = new ArrayList[N + 1];
answer = new int[N + 1];
// 인접 리스트 채우기
for(int i = 1 ; i <= N ; i++) {
A[i] = new ArrayList<>();
}
for(int i = 0 ; i < M ; i++) {
st = new StringTokenizer(br.readLine()," ");
int S = Integer.parseInt(st.nextToken());
int E = Integer.parseInt(st.nextToken());
A[S].add(E);
}
// 🔔 모든 노드에 대해 BFS 수행 🔔
for(int i = 1 ; i <= N ; i++) {
visited = new boolean[N + 1];
BFS(i);
}
int maxVal = 0;
for(int i = 1 ; i <= N ; i++) {
maxVal = Math.max(maxVal, answer[i]);
}
for(int i = 1 ; i <= N ; i++) {
if(answer[i] == maxVal) {
System.out.print(i + " ");
}
}
}
public static void BFS(int node) {
visited[node] = true;
Queue<Integer> queue = new LinkedList<>();
queue.add(node);
while(!queue.isEmpty()) {
int now_node = queue.poll();
for(int i : A[now_node]) {
if(!visited[i]) {
visited[i] = true;
answer[i]++; // 신규 노드 인덱스의 정답 배열 값 증가시킴
queue.add(i);
}
}
}
}
}
|
cs |
✅ 해결 아이디어
✔ BFS (단방향. 인접 리스트 사용)
💥 유의사항
⇨ 필요한 것
1) 인접 리스트
2) 정답 배열 (int형)
3) 방문 여부 배열 (boolean형)
🔺 다른 풀이들
- 복습용!
[알고리즘-PS] 백준 1325번 효율적인 해킹 자바 문제풀이 (시간 초과 해결)
문제 해당 포스팅은 백준의 1325번 효율적인 해킹 의 접근과 해결 방법을 설명한 글 입니다. 정답 소스 코드를 확인하시려면 solve url 에서 확인 가능합니다. 이 문제를 해결하기 위해 어떤 방식으
wonit.tistory.com
[백준] 1325번 효율적인 해킹 - Java, 자바
실버 1https://www.acmicpc.net/problem/1325이 문제는 DFS와 BFS로 풀 수 있는 그래프 탐색 문제로, 나는 BFS로 풀었다. 처음에 인접행렬을 활용해 문제를 풀었는데 메모리 초과가 나왔다. N은 10,000보다 작거
velog.io
💬 느낀 점
바로 BFS를 생각하긴 했다만
구현은 아직 완벽하지 못 함.
1회독 | 2회독 | 3회독 | 4회독 | 5회독 |
V | 6/21 |
(+6/21 2회독)
정답 배열 만들기 & 모든 정점에 대해 bfs 수행
이걸 놓쳤다!
모든 정점에 대해 bfs 수행하는 아이디어를 자주 생각해내지 못 하는 것 같다..ㅠ
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
import java.util.*;
import java.io.*;
public class Main {
static int N, M;
static ArrayList<Integer>[] A; // 인접 리스트
static boolean[] visited; // 방문 배열
static int[] answer; // 🔔 신뢰도 배열 🔔
static int max = 0; // 최대값
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
A = new ArrayList[N + 1];
for(int i = 1 ; i <= N ; i++) {
A[i] = new ArrayList<>();
}
visited = new boolean[N + 1];
answer = new int[N + 1];
while(M --> 0) {
st = new StringTokenizer(br.readLine(), " ");
int s = Integer.parseInt(st.nextToken());
int e = Integer.parseInt(st.nextToken());
A[s].add(e);
}
// 🔔 모든 노드에 대해 bfs 수행 🔔
for(int i = 1 ; i <= N ; i++) {
visited = new boolean[N + 1];
bfs(i);
}
// 출력
StringBuilder sb = new StringBuilder();
for(int i = 1 ; i <= N ; i++) {
if(answer[i] == max) {
sb.append(i).append(" ");
}
}
System.out.println(sb);
}
static void bfs(int num) {
Queue<Integer> queue = new LinkedList<>();
queue.add(num);
visited[num] = true;
while(!queue.isEmpty()) {
int now = queue.poll();
for(int next : A[now]) {
if(!visited[next]) {
visited[next] = true;
answer[next]++; // 신뢰도 배열 값 업데이트
queue.add(next);
max = Math.max(max, answer[next]); // 최대값 탐색
}
}
}
}
}
|
cs |
반응형
'코테 > 백준' 카테고리의 다른 글
[백준/JAVA] 2251번: 물통 (0) | 2023.04.28 |
---|---|
[백준/JAVA] 1707번: 이분 그래프 (0) | 2023.04.27 |
[백준/JAVA] 18352번: 특정 거리의 도시 찾기 (0) | 2023.04.27 |
[백준/JAVA] 1850번: 최대공약수 (0) | 2023.04.26 |
[백준/JAVA] 1934번: 최소공배수 (0) | 2023.04.26 |