๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์•Œ๊ณ ๋ฆฌ์ฆ˜/๋ฌธ์ œ

[์ž๋ฐ”] ๋ฐฑ์ค€ 1068 - ํŠธ๋ฆฌ

by applemango2021 2020. 12. 4.

๋ฌธ์ œ ์‰ฝ๊ฒŒ ์ƒ๊ฐํ–ˆ๋Š”๋ฐ, 4๋ฒˆ์งธ ์‹œ๋„์—์„œ์•ผ ์„ฑ๊ณตํ–ˆ๋‹ค.

 

๋„์ €ํžˆ ๋ชจ๋ฅด๊ฒ ์–ด์„œ ๊ฒฐ๊ตญ ๊ฒฐ์ •์ ์ธ ์•„์ด๋””์–ด๋Š” ์•„๋ž˜ ๋ธ”๋กœ๊ทธ์—์„œ ์ฐธ๊ณ ํ–ˆ๋‹ค. 

์Šฌํ”„๊ฒŒ๋„ ๊ฑฐ์˜ ๋ณด๊ณ ์„œ ์ฝ”๋“œ๋ฅผ ์ผ๋Š”๋ฐ๋„ ๋ฐ”๋กœ ์ •๋‹ต์ด ์•ˆ ๋‚˜์™”๋‹ค.

 

soboruya.tistory.com/34

 

[๋ฐฑ์ค€ 1068] ํŠธ๋ฆฌ(JAVA)

https://www.acmicpc.net/problem/1068 1068๋ฒˆ: ํŠธ๋ฆฌ ์ฒซ์งธ ์ค„์— ํŠธ๋ฆฌ์˜ ๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜ N์ด ์ฃผ์–ด์ง„๋‹ค. N์€ 50๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค. ๋‘˜์งธ ์ค„์—๋Š” 0๋ฒˆ ๋…ธ๋“œ๋ถ€ํ„ฐ N-1๋ฒˆ ๋…ธ๋“œ๊นŒ์ง€, ๊ฐ ๋…ธ๋“œ์˜ ๋ถ€๋ชจ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.

soboruya.tistory.com

์ฒซ ๋ฒˆ์งธ๋Š” ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ ๋‚ฌ๋Š”๋ฐ, ์ด์ง„ ํŠธ๋ฆฌ๋ผ๊ณ  ์ƒ๊ฐํ•˜๊ณ  ํ’€์–ด์„œ ๋‚œ๊ฑฐ๋ผ ์ž์„ธํžˆ ๋ณด์ง€๋„ ์•Š์•˜๊ณ 

๋‘ ๋ฒˆ์งธ๋Š” getResult() ๋ฉ”์†Œ๋“œ์—์„œ ์•„๋ž˜์ฒ˜๋Ÿผ ์ž์‹๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜๊ฐ€ 0์ด ์•„๋‹ ๋•Œ ์นด์šดํŠธํ•˜๋ผ๊ณ  ์ž˜๋ชป ์งœ์„œ ๋‚ฌ๋‹ค

 private static int getResult() {
	int cnt=0;
	for(int i=0;i<N;i++) {
		if(tree[i][0]!=removed && tree[i][1]!=0) cnt++;
	}
		
	return cnt;
		
}

 

์„ธ ๋ฒˆ์งธ๋Š” ๋ฐ˜๋ก€ ๋‹ค ์ž…๋ ฅํ•ด๋ด๋„ ํ‹€๋ฆฐ ๊ฒŒ ์—†์–ด์„œ ์˜ค๋ž˜ ๊ณ ๋ฏผํ–ˆ๋‹ค. 

๊ทธ๋Ÿฌ๋‹ค๊ฐ€ ๋ฐ์ดํ„ฐ ์ž…๋ ฅ๋ฐ›๋Š” ๋ถ€๋ถ„์—์„œ ์˜ค๋ฅ˜๊ฐ€ ์žˆ๋Š” ๊ฑธ ๋ฐœ๊ฒฌํ–ˆ๋‹ค.

-1์ด ๋“ค์–ด์˜จ ๊ฒฝ์šฐ(๋ฃจํŠธ ๋…ธ๋“œ์ธ ๊ฒฝ์šฐ)์—๋„ tree[i][0]์— parent๊ฐ’์„ ๋ฐ›์•„์ฃผ์–ด์•ผ ์ œ๊ฑฐ๋Œ€์ƒ์ด 0์ธ ๊ฒฝ์šฐ์— ๋ฃจํŠธ๋…ธ๋“œ๊ฐ€ ์ œ๊ฑฐ๋˜์ง€ ์•Š๋Š”๋ฐ, ๊ทธ ๋กœ์ง์„ ๊ฑด๋„ˆ ๋›ฐ์–ด ๋ฒ„๋ ธ์œผ๋‹ˆ ์—๋Ÿฌ๊ฐ€ ๋‚œ ๊ฒƒ. 

 for(int i=0;i<N;i++) {
	parent = Integer.parseInt(str[i]);
	if(parent==-1) continue;
			
	//ํ˜„์žฌ tree์˜ 0๋ฒˆ์— parent ๊ฐ’ ๋„ฃ๊ณ  
	tree[i][0] = parent;			
	//parent tree์˜ 1๋ฒˆ์— ์ž์‹๋…ธ๋“œ ๊ฐœ์ˆ˜ ++
	tree[parent][1]++;
 }

 

์•„๋ž˜์ฒ˜๋Ÿผ ์ˆœ์„œ๋งŒ ์˜ฎ๊ฒจ์ฃผ์—ˆ๋”๋‹ˆ ๋ฐ”๋กœ ํ†ต๊ณผํ–ˆ๋‹ค. 

 for(int i=0;i<N;i++) {
	parent = Integer.parseInt(str[i]);
	//ํ˜„์žฌ tree์˜ 0๋ฒˆ์— parent ๊ฐ’ ๋„ฃ๊ณ  
	tree[i][0] = parent;
	if(parent==-1) continue;
	
	//parent tree์˜ 1๋ฒˆ์— ์ž์‹๋…ธ๋“œ ๊ฐœ์ˆ˜ ++
	tree[parent][1]++;
 }

 

์ „์ฒด ์ฝ”๋“œ

 import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
	static int[][] tree;
	static int N;
	static int removed = -99;
	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		N = Integer.parseInt(br.readLine().trim());
		tree = new int[N][2]; //0:๋ถ€๋ชจ๋…ธ๋“œ ๋ฒˆํ˜ธ, 1:์ž์‹๋…ธ๋“œ ๊ฐœ์ˆ˜
		
		String[] str = br.readLine().trim().split(" ");
		int parent;

		for(int i=0;i<N;i++) {
			parent = Integer.parseInt(str[i]);
			if(parent==-1) continue;
			
			//ํ˜„์žฌ tree์˜ 0๋ฒˆ์— parent ๊ฐ’ ๋„ฃ๊ณ  
			tree[i][0] = parent;			
			//parent tree์˜ 1๋ฒˆ์— ์ž์‹๋…ธ๋“œ ๊ฐœ์ˆ˜ ++
			tree[parent][1]++;
		}
		int remove = Integer.parseInt(br.readLine().trim());
		removeNode(remove);		
		int result = getResult();
		System.out.println(result);

	}

	private static int getResult() {
		int cnt=0;
		for(int i=0;i<N;i++) {
			if(tree[i][0]!=removed && tree[i][1]==0) cnt++;
		}
		
		return cnt;
		
	}

	private static void removeNode(int remove) {
		
		int parent = tree[remove][0];
		if(parent!=-1) tree[parent][1]--; //๋ถ€๋ชจ๋…ธ๋“œ์˜ ์ž์‹์ˆ˜ --
		
		tree[remove][0]= removed; //์ง€์›Œ์กŒ๋‹ค๊ณ  ํ‘œ์‹œ
		
		if(tree[remove][1]!=0) { //์ž์‹๋…ธ๋“œ๊ฐ€ ์กด์žฌํ•œ๋‹ค๋ฉด 
			for(int i=0;i<N;i++) {
				if(tree[i][0]==remove) {
					removeNode(i);
				}
			}			
		}
	}
}