반응형
    
    
    
  BOJ 10775 풀이 코드
import java.io.*;
import java.util.*;
public class Main {
	static int[] gate;
	
	static int find(int p) {
		if(gate[p] == p)
			return p;
		gate[p] = find(gate[p]);
		return gate[p];
	}
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		int n = Integer.parseInt(br.readLine());
		int m = Integer.parseInt(br.readLine());
		
		gate = new int[n+1];
		for(int i = 1; i <= n; i++)
			gate[i] = i;
		
		int cnt = n;
		boolean end = false;
		for(int i = 0; i < m; i++) {
			int j = Integer.parseInt(br.readLine());
			if(end) continue;
			int new_gate = find(j);
			if(new_gate == 0) {
				end = true;
				cnt = i;
			} else {
				gate[new_gate] = new_gate-1;
			}
		}
		bw.write(cnt + "\n");
		bw.flush();
		br.close();
		bw.close();
	}
}반응형
    
    
    
  '코딩 문제 풀이 > 백준' 카테고리의 다른 글
| [백준] 자바 문제 풀이 1655 : 골드2 (0) | 2023.09.23 | 
|---|---|
| [백준] 자바 문제 풀이 10868 : 골드1 (0) | 2023.09.23 | 
| [백준] 자바 문제 풀이 11505 : 골드1 (0) | 2023.09.23 | 
| [백준] 자바 문제 풀이 2109 : 골드3 (0) | 2023.09.23 | 
| [백준] 자바 문제 풀이 17822 : 골드2 (0) | 2023.09.22 |