๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 17616_๋“ฑ์ˆ˜ ์ฐพ๊ธฐ

๋ฟŒ์•ผ._. 2023. 8. 18. 10:57

Gold III

๋ฌธ์ œ(์ถœ์ฒ˜: https://www.acmicpc.net/problem/17616)

< ๋“ฑ์ˆ˜ ์ฐพ๊ธฐ >

 

๋ฌธ์ œ ํ’€์ด 

 

์ž…๋ ฅ์„ ๋ฐ›์„ ๋•Œ ArrayList 2๊ฐœ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ž์‹ ๋ณด๋‹ค ๋” ์ž˜ํ•œ ์‚ฌ๋žŒ๊ณผ ์ž์‹ ๋ณด๋‹ค ๋” ๋ชปํ•œ ์‚ฌ๋žŒ์„ ์ €์žฅํ•œ๋‹ค.

dfs๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์ž์‹ ๋ณด๋‹ค ์ž˜ ํ•œ ์‚ฌ๋žŒ์˜ ์ˆ˜์™€ ์ž์‹ ๋ณด๋‹ค ๋ชปํ•œ ์‚ฌ๋žŒ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•ด ๊ฐ€๋Šฅํ•œ ๊ฐ€์žฅ ๋†’์€ ๋“ฑ์ˆ˜์™€ ๊ฐ€๋Šฅํ•œ ๊ฐ€์žฅ ๋‚ฎ์€ ๋“ฑ์ˆ˜๋ฅผ ๊ตฌํ•œ๋‹ค.

 

 

 my solution (Java)

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class _17616_ { // ๋“ฑ์ˆ˜ ์ฐพ๊ธฐ

	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		StringTokenizer st = new StringTokenizer(bf.readLine());

		int n = Integer.parseInt(st.nextToken());
		int m = Integer.parseInt(st.nextToken());
		int x = Integer.parseInt(st.nextToken());

		ArrayList<ArrayList<Integer>> parents = new ArrayList<>();
		ArrayList<ArrayList<Integer>> child = new ArrayList<>();
		for (int i = 0; i < n + 1; i++) {
			parents.add(new ArrayList<>());
			child.add(new ArrayList<>());
		}

		for (int i = 0; i < m; i++) {
			st = new StringTokenizer(bf.readLine());
			int a = Integer.parseInt(st.nextToken());
			int b = Integer.parseInt(st.nextToken());

			parents.get(b).add(a);
			child.get(a).add(b);
		}

		boolean visited[] = new boolean[n+1];
		visited[x] = true;

		int start = dfs(x, parents, 0, visited);
		int end = dfs(x, child, 0, visited);

		bw.write((start + 1) + " " + (n - end));
		bw.flush();
	}

	private static int dfs(int x, ArrayList<ArrayList<Integer>> arr, int count, boolean[] visited) {
		for (int i = 0; i < arr.get(x).size(); i++) {
			if (!visited[arr.get(x).get(i)]) {
				visited[arr.get(x).get(i)] = true;
				count+=dfs(arr.get(x).get(i), arr, 1, visited);
			}
		}
		return count;
	}
}

 

 

Main

๋ณ€์ˆ˜)
n : ํ•™์ƒ ์ˆ˜
m : ์งˆ๋ฌธ ์ˆ˜
x : ๋“ฑ์ˆ˜๋ฅผ ์•Œ๊ณ  ์‹ถ์€ ํ•™์ƒ 
parents : ์ž์‹ ๋ณด๋‹ค ์ž˜ํ•œ ์‚ฌ๋žŒ ์ €์žฅ
child : ์ž์‹ ๋ณด๋‹ค ๋ชปํ•œ ์‚ฌ๋žŒ ์ €์žฅ
a, b : ํ•™์ƒ a๊ฐ€ ํ•™์ƒ b๋ณด๋‹ค ๋” ์ž˜ํ•จ
visited : ๋ฐฉ๋ฌธ ์—ฌ๋ถ€
start : x๋ณด๋‹ค ์ž˜ ํ•œ ์‚ฌ๋žŒ ์ˆ˜
end : x๋ณด๋‹ค ๋ชปํ•œ ์‚ฌ๋žŒ ์ˆ˜

 

- ํ•™์ƒ ์ˆ˜(n), ์งˆ๋ฌธ ์ˆ˜(m), ๋“ฑ์ˆ˜๋ฅผ ์•Œ๊ณ  ์‹ถ์€ ํ•™์ƒ(x) ์ž…๋ ฅ

- ์งˆ๋ฌธ ์ˆ˜(m)๋งŒํผ ํ•™์ƒ a, b๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ parents์—๋Š” ์ž์‹ ๋ณด๋‹ค ์ž˜ํ•œ ์‚ฌ๋žŒ์„ ์ €์žฅํ•˜๋ฏ€๋กœ b์— a๋ฅผ ์ €์žฅํ•˜๊ณ , child์—๋Š” ์ž์‹ ๋ณด๋‹ค ๋ชปํ•œ ์‚ฌ๋žŒ์„ ์ €์žฅํ•˜๋ฏ€๋กœ a์— b๋ฅผ ์ €์žฅ

- ์ž์‹ ๋ณด๋‹ค ์ž˜ํ•œ ์‚ฌ๋žŒ์˜ ์ˆ˜์™€ ๋ชปํ•œ ์‚ฌ๋žŒ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด dfs ํ•จ์ˆ˜ 2๋ฒˆ ํ˜ธ์ถœ

- ์ •๋‹ต ์ถœ๋ ฅ [์ž์‹ ๋ณด๋‹ค ์ž˜ํ•œ ์‚ฌ๋žŒ์˜ ์ˆ˜ +1, ํ•™์ƒ ์ˆ˜ - ์ž์‹ ๋ณด๋‹ค ๋ชปํ•œ ์‚ฌ๋žŒ์˜ ์ˆ˜]

 

dfs

- ์ž์‹ ๊ณผ ์—ฐ๊ฒฐ๋œ ์‚ฌ๋žŒ๋“ค์ด๋ฉฐ, ์•„์ง ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜์œผ๋ฉด ๋ฐฉ๋ฌธ ํ›„ dfs ํ•จ์ˆ˜ ํ˜ธ์ถœ