오늘 풀어볼 문제는 백준 1325번 문제이다.
문제
해커 김지민은 잘 알려진 어느 회사를 해킹하려고 한다.
이 회사는 N개의 컴퓨터로 이루어져 있다.
김지민은 귀찮기 때문에, 한 번의 해킹으로 여러 개의 컴퓨터를 해킹 할 수 있는 컴퓨터를 해킹하려고 한다.
이 회사의 컴퓨터는 신뢰하는 관계와, 신뢰하지 않는 관계로 이루어져 있는데, A가 B를 신뢰하는 경우에는 B를 해킹하면, A도 해킹할 수 있다는 소리다.
이 회사의 컴퓨터의 신뢰하는 관계가 주어졌을 때, 한 번에 가장 많은 컴퓨터를 해킹할 수 있는 컴퓨터의 번호를 출력하는 프로그램을 작성하시오.
입력
첫째 줄에, N과 M이 들어온다. N은 10,000보다 작거나 같은 자연수, M은 100,000보다 작거나 같은 자연수이다.
둘째 줄부터 M개의 줄에 신뢰하는 관계가 A B와 같은 형식으로 들어오며, "A가 B를 신뢰한다"를 의미한다. 컴퓨터는 1번부터 N번까지 번호가 하나씩 매겨져 있다.
출력
첫째 줄에, 김지민이 한 번에 가장 많은 컴퓨터를 해킹할 수 있는 컴퓨터의 번호를 오름차순으로 출력한다.
📌 도전 📌
BFS를 활용해서 각 노드를 방문하고 이어진 노드의 수를 계산하며 가장 많이 연관된 노드를 찾는 방법을 활용했다. 하지만... 시간 초과가 떴다..
public class Main {
static int n,m;
static boolean visited[];
static ArrayList<Integer>[] graph;
static int ans[];
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());
graph = new ArrayList[n+1];
ans = new int[n+1];
for (int i=1; i<=n; i++){
graph[i] = new ArrayList<Integer>();
}
for (int i=1; i<=m; i++){
st = new StringTokenizer(br.readLine());
int s = Integer.parseInt(st.nextToken());
int e = Integer.parseInt(st.nextToken());
graph[s].add(e);
}
for(int i=1; i<=n; i++) {
visited = new boolean[n+1]; // 방문 배열 초기화
BFS(i);
}
int max = 0;
for (int i=1; i<=n; i++) {
max = Math.max(max, ans[i]);
}
for (int i=1; i<=n; i++) {
if(ans[i]==max) {
System.out.print(i + " ");
}
}
}
private static void BFS(int node) {
Queue<Integer> queue = new LinkedList<Integer>();
queue.add(node);
visited[node] = true;
while (!queue.isEmpty()) {
int now_node = queue.poll();
for(int i : graph[now_node]) {
if(visited[i] == false) {
visited[i] = true;
ans[i]++;
queue.add(i);
}
}
}
}
}
📌 재도전 📌
두 번째 도전이라곤 하지만... 수 많은 도전을 했다.. 시간 초과가 계속 떠서 BufferWriter로도 바꿔보고 여러 방법을 사용해 보았는데 "ArrayList[] graph" -> "List[] graph" 자료 구조 선언 방식을 바꿨더니 성공하였다...
public class Main {
static int n,m;
static boolean visited[];
static int ans[];
static List<Integer>[] graph;
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());
graph = new List[n+1];
ans = new int[n+1];
for(int i=0; i<=n; i++) {
graph[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());
graph[s].add(e);
}
for(int i=1; i<=n; i++) {
visited = new boolean[n+1]; // 방문 배열 초기화
BFS(i);
}
int max = 0;
for (int i=1; i<=n; i++) {
max = Math.max(max, ans[i]);
}
for (int i = 1; i <= n; i++) {
if (ans[i] == max) { // 최대값과 같다면 인덱스 출력
System.out.print(i + " ");
}
}
}
private static void BFS(int node) {
Queue<Integer> queue = new LinkedList<>();
queue.add(node);
visited[node] = true;
while (!queue.isEmpty()) {
int now_node = queue.poll();
for(int i : graph[now_node]) {
if(!visited[i]) {
visited[i] = true;
ans[i]++;
queue.add(i);
}
}
}
}
}
[문제 출처] : https://www.acmicpc.net/problem/1325