๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 14267_ํšŒ์‚ฌ ๋ฌธํ™” 1

๋ฟŒ์•ผ._. 2023. 6. 27. 15:30

Gold IV

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

< ํšŒ์‚ฌ ๋ฌธํ™” 1 >

 

๋ฌธ์ œ ํ’€์ด & ์ƒ๊ฐ

 

์ง์† ์ƒ์‚ฌ์˜ ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅ๋ฐ›์œผ๋ฏ€๋กœ ์ด ์ •๋ณด๋ฅผ ์ด์šฉํ•ด์„œ ๊ฐ ์ง์›์˜ ์ง์† ๋ถ€ํ•˜๋ฅผ ์ €์žฅํ•ด ๋‘”๋‹ค.

 

์ฒ˜์Œ์—๋Š” ์ง์† ๋ถ€ํ•˜ ์ •๋ณด๋ฅผ ์ €์žฅํ•ด ๋‘” ๊ฒƒ์„ ์ด์šฉํ•ด์„œ ๋‹ต์„ ๊ตฌํ–ˆ์ง€๋งŒ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค.

๊ทธ ์ด์œ ๋Š” ์นญ์ฐฌ์„ ๋ฐ›์„ ๋•Œ๋งˆ๋‹ค dfs ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ด์„œ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋˜ ๊ฒƒ์ด๋‹ค.

์ด๊ฒƒ์„ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ์นญ์ฐฌ์„ ๋‹ค ์ €์žฅํ•œ ํ›„ dfs ํ•จ์ˆ˜๋ฅผ ํ•œ ๋ฒˆ ํ˜ธ์ถœํ•˜์—ฌ ๊ตฌํ–ˆ๋”๋‹ˆ ํ†ต๊ณผํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

 

 ์‹œ๊ฐ„์ดˆ๊ณผ ์ฝ”๋“œ(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 Main { // ํšŒ์‚ฌ ๋ฌธํ™” 1
	static int sup[];
	static long answer[];
	static ArrayList<ArrayList<Integer>> sub;

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

		sup = new int[n + 1];
		answer = new long[n + 1];
		sub = new ArrayList<>();
		for (int i = 0; i <= n; i++) {
			sub.add(new ArrayList<>());
		}

		st = new StringTokenizer(bf.readLine());
		for (int i = 1; i <= n; i++) {
			sup[i] = Integer.parseInt(st.nextToken());
			if (sup[i] > -1)
				sub.get(sup[i]).add(i);
		}

		for (int j = 0; j < m; j++) {
			st = new StringTokenizer(bf.readLine());
			int i = Integer.parseInt(st.nextToken()); // ์ง์› ๋ฒˆํ˜ธ
			int w = Integer.parseInt(st.nextToken()); // ์นญ์ฐฌ์˜ ์ˆ˜์น˜
			dfs(i, w);
		}

		for (int i = 1; i <= n; i++) {
			bw.write(answer[i] + " ");
		}
		bw.flush();
	}

	private static void dfs(int idx, int w) {
		answer[idx] += w;
		for (int i = 0; i < sub.get(idx).size(); i++) {
			dfs(sub.get(idx).get(i), w);
		}
	}
}

 

 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 _14267_ { // ํšŒ์‚ฌ ๋ฌธํ™” 1
	static int sup[], answer[];
	static boolean visited[];
	static ArrayList<ArrayList<Integer>> sub;

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

		sup = new int[n + 1];
		answer = new int[n + 1];
		visited = new boolean[n + 1];
		sub = new ArrayList<>();
		for (int i = 0; i <= n; i++) {
			sub.add(new ArrayList<>());
		}

		st = new StringTokenizer(bf.readLine());
		for (int i = 1; i <= n; i++) {
			sup[i] = Integer.parseInt(st.nextToken());
			if (sup[i] > -1)
				sub.get(sup[i]).add(i);
		}

		for (int j = 0; j < m; j++) {
			st = new StringTokenizer(bf.readLine());
			int i = Integer.parseInt(st.nextToken()); // ์ง์› ๋ฒˆํ˜ธ
			int w = Integer.parseInt(st.nextToken()); // ์นญ์ฐฌ์˜ ์ˆ˜์น˜
			answer[i] += w;
		}
		
		for(int i=1; i<=n; i++) {
			if(answer[i]>0 && !visited[i]) {
				visited[i]=true;
				dfs(i);
			}
		}

		for (int i = 1; i <= n; i++) {
			bw.write(answer[i] + " ");
		}
		bw.flush();
	}

	private static void dfs(int idx) {
		for (int i = 0; i < sub.get(idx).size(); i++) {
			int next=sub.get(idx).get(i);
			answer[next]+=answer[idx];
			visited[next]=true;
			dfs(next);
		}
	}
}

 

Main

- ํšŒ์‚ฌ ์ง์›์˜ ์ˆ˜ (n), ์ตœ์ดˆ์˜ ์นญ์ฐฌ์˜ ํšŸ์ˆ˜ (m) ์ž…๋ ฅ

- sup : ์ง์† ์ƒ์‚ฌ์˜ ๋ฒˆํ˜ธ ์ €์žฅ
  answer : ์นญ์ฐฌ๋ฐ›์€ ์ •๋„ ์ €์žฅ
  visited: ๋ฐฉ๋ฌธ ์—ฌ๋ถ€

  sub : ์ง์† ๋ถ€ํ•˜์˜ ๋ฒˆํ˜ธ ์ €์žฅ

- ์ง์† ์ƒ์‚ฌ์˜ ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅ๋ฐ›์œผ๋ฉด์„œ ์ง์† ํ•˜์‚ฌ์˜ ๋ฒˆํ˜ธ๋ฅผ ์ €์žฅ

- ์ง์› ๋ฒˆํ˜ธ(i), ์นญ์ฐฌ์˜ ์ˆ˜์น˜(w) ์ž…๋ ฅ ํ›„ answer์— ์นญ์ฐฌ ์ˆ˜์น˜ ์ €์žฅ

- ์นญ์ฐฌ์„ ๋ฐ›์•˜์ง€๋งŒ ์•„์ง ์ง์† ๋ถ€ํ•˜์—๊ฒŒ ์ „๋‹ฌ์„ ์•ˆ ํ–ˆ๋‹ค๋ฉด dfs ํ•จ์ˆ˜ ํ˜ธ์ถœ

- answer ์ถœ๋ ฅ

 

dfs

- ์ง์† ๋ถ€ํ•˜๊ฐ€ ์žˆ๋‹ค๋ฉด ์ง์† ๋ถ€ํ•˜์—๊ฒŒ ์นญ์ฐฌ ์ „๋‹ฌ ํ›„ dfs ํ˜ธ์ถœ



 

'๐ŸŒžAlgorithm > ๐Ÿ”ฅBaekjoon' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Baekjoon] 17298_์˜คํฐ์ˆ˜  (0) 2023.06.28
[Baekjoon] 3425_๊ณ ์Šคํƒ  (0) 2023.06.27
[Baekjoon] 16562_์นœ๊ตฌ๋น„  (0) 2023.06.27
[Baekjoon] 1043_๊ฑฐ์ง“๋ง  (0) 2023.06.26
[Baekjoon] 12893_์ ์˜ ์   (0) 2023.06.26