본문 바로가기
코딩 문제 풀이/백준

[백준] 자바 문제 풀이 10775 : 골드2

by 코딩하는 랄로 2023. 9. 23.
728x90

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();
	}

}
728x90