240916 공항

Jongleee·2024년 9월 16일
0

TIL

목록 보기
679/737
public static void main(String[] args) throws IOException {
	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

	int g = Integer.parseInt(br.readLine());
	int[] parents = new int[g + 1];

	for (int i = 1; i <= g; i++) {
		parents[i] = i;
	}

	int p = Integer.parseInt(br.readLine());
	int dockedPlanes = 0;

	for (int i = 0; i < p; i++) {
		int planeGate = Integer.parseInt(br.readLine());

		int dockingGate = find(parents, planeGate);
		if (dockingGate == 0) {
			break;
		}

		dockedPlanes++;
		union(parents, dockingGate, dockingGate - 1);
	}

	System.out.println(dockedPlanes);
	br.close();
}

private static void union(int[] parents, int a, int b) {
	a = find(parents, a);
	b = find(parents, b);
	parents[a] = b;
}

private static int find(int[] parents, int x) {
	if (x == parents[x]) {
		return x;
	}
	parents[x] = find(parents, parents[x]);
	return parents[x];
}

출처:https://www.acmicpc.net/problem/10775

0개의 댓글