๐Ÿ“ Coding Test Study/Algorithm Problem

[C++][BAEKJOON][TREE&DP] 2533๋ฒˆ ์‚ฌํšŒ๋ง ์„œ๋น„์Šค(SNS)

ibelieveinme 2021. 10. 4. 16:45
728x90

[๋ฌธ์ œ]

 

2533๋ฒˆ: ์‚ฌํšŒ๋ง ์„œ๋น„์Šค(SNS)

ํŽ˜์ด์Šค๋ถ, ํŠธ์œ„ํ„ฐ, ์นด์นด์˜คํ†ก๊ณผ ๊ฐ™์€ ์‚ฌํšŒ๋ง ์„œ๋น„์Šค(SNS)๊ฐ€ ๋„๋ฆฌ ์‚ฌ์šฉ๋จ์— ๋”ฐ๋ผ, ์‚ฌํšŒ๋ง์„ ํ†ตํ•˜์—ฌ ์‚ฌ๋žŒ๋“ค์ด ์–ด๋–ป๊ฒŒ ์ƒˆ๋กœ์šด ์•„์ด๋””์–ด๋ฅผ ๋ฐ›์•„๋“ค์ด๊ฒŒ ๋˜๋Š”๊ฐ€๋ฅผ ์ดํ•ดํ•˜๋Š” ๋ฌธ์ œ๊ฐ€ ์ค‘์š”ํ•ด์กŒ๋‹ค. ์‚ฌํšŒ๋ง

www.acmicpc.net

[๋ฌธ์ œ์„ค๋ช…]
์‚ฌํšŒ๋ง ์„œ๋น„์Šค(SNS)์— ์†ํ•œ ์‚ฌ๋žŒ๋“ค์€ ์–ผ๋ฆฌ ์•„๋‹ตํ„ฐ์ด๊ฑฐ๋‚˜ ์–ผ๋ฆฌ ์•„๋‹ตํ„ฐ๊ฐ€ ์•„๋‹ˆ๋‹ค. ์–ผ๋ฆฌ ์•„๋‹ตํ„ฐ๊ฐ€ ์•„๋‹Œ ์‚ฌ๋žŒ๋“ค์€ ์ž์‹ ์˜ ๋ชจ๋“  ์นœ๊ตฌ๋“ค์ด ์–ผ๋ฆฌ ์•„๋‹ตํ„ฐ์ผ ๋•Œ๋งŒ ์ด ์•„์ด๋””์–ด๋ฅผ ๋ฐ›์•„๋“ค์ธ๋‹ค. ์–ด๋–ค ์•„์ด๋””์–ด๋ฅผ ์‚ฌํšŒ๋ง ์„œ๋น„์Šค(SNS)์—์„œ ํผ๋œจ๋ฆฌ๊ณ ์ž ํ•  ๋•Œ, ๊ฐ€๋Šฅํ•œ ํ•œ ์ตœ์†Œ์˜ ์ˆ˜์˜ ์–ผ๋ฆฌ ์•„๋‹ตํ„ฐ๋ฅผ ํ™•๋ณดํ•˜์—ฌ ๋ชจ๋“  ์‚ฌ๋žŒ์ด ์ด ์•„์ด๋””์–ด๋ฅผ ๋ฐ›์•„๋“ค์ด๊ฒŒ ํ•˜์ž.

 

์–ผ๋ฆฌ์–ด๋‹ตํ„ฐ์˜ ์กฐ๊ฑด์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.
- ์–ผ๋ฆฌ ์–ด๋‹ตํ„ฐ๊ฐ€ ์•„๋‹ˆ๋ฉด, ๋ชจ๋“  ์นœ๊ตฌ๋“ค์ด ์–ผ๋ฆฌ ์–ด๋‹ตํ„ฐ์•ผ ํ•œ๋‹ค.
- ์–ผ๋ฆฌ ์–ด๋‹ตํ„ฐ์ด๋ฉด, ์ฃผ๋ณ€ ์นœ๊ตฌ๋“ค์ด ์–ผ๋ฆฌ ์–ด๋‹ตํ„ฐ๊ฑฐ๋‚˜ ์•„๋‹ˆ์–ด๋„ ๋œ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, 8๋ช…์˜ ์‚ฌ๋žŒ์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ์œ„์˜ ์นœ๊ตฌ ๊ด€๊ณ„ ํŠธ๋ฆฌ๋ฅผ ์ƒ๊ฐํ•ด๋ณด์ž. 2, 3, 4๋ฒˆ ๋…ธ๋“œ๊ฐ€ ํ‘œํ˜„ํ•˜๋Š” ์‚ฌ๋žŒ๋“ค์ด ์–ผ๋ฆฌ ์•„๋‹ตํ„ฐ๋ผ๋ฉด, ์–ผ๋ฆฌ ์•„๋‹ตํ„ฐ๊ฐ€ ์•„๋‹Œ ์‚ฌ๋žŒ๋“ค์€ ์ž์‹ ์˜ ๋ชจ๋“  ์นœ๊ตฌ๊ฐ€ ์–ผ๋ฆฌ ์•„๋‹ตํ„ฐ์ด๊ธฐ ๋•Œ๋ฌธ์— ์ƒˆ๋กœ์šด ์•„์ด๋””์–ด๋ฅผ ๋ฐ›์•„๋“ค์ธ๋‹ค.

์ฃผ์–ด์ง„ ์นœ๊ตฌ ๊ด€๊ณ„ ๊ทธ๋ž˜ํ”„์—์„œ ์•„์ด๋””์–ด๋ฅผ ์ „ํŒŒํ•˜๋Š”๋ฐ ํ•„์š”ํ•œ ์–ผ๋ฆฌ ์•„๋‹ตํ„ฐ์˜ ์ตœ์†Œ ์ˆ˜๋Š” 3์ด๋‹ค.

 

[๋ฌธ์ œํ’€์ด]
DP๋ฅผ ์ด์šฉํ•œ ๋ธŒ๋ฃจํŠธํฌ์Šค ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ํ’€์–ด์•ผํ•œ๋‹ค.


leaf๋…ธ๋“œ๋ถ€ํ„ฐ or root๋…ธ๋“œ๋ถ€ํ„ฐ ์–ผ๋ฆฌ์–ด๋‹ตํ„ฐ์˜ ์กฐ๊ฑด์„ ํŒ๋ณ„&๊ฐฑ์‹ ํ•˜๋ฉด์„œ ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ๊นŒ์ง€ ์ „๋ถ€ ํƒ์ƒ‰ํ•ด์•ผ, ์–ผ๋ฆฌ์–ด๋‹ตํ„ฐ์˜ ์ตœ์†Œ ์ˆ˜๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๋‚˜๋Š” root ๋…ธ๋“œ๋ถ€ํ„ฐ DFS๋กœ leaf ๋…ธ๋“œ๊นŒ์ง€ ํƒ์ƒ‰ํ–ˆ๋‹ค.

 

1) ์šฐ์„  ๋ฌธ์ œ์—์„œ ์ฃผ์–ด์ง„ ํŠธ๋ฆฌ๋Š” ๊ฐ ๋…ธ๋“œ๋‹น ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ๋“ค์„ ์ „๋ถ€ ๋™์ ์œผ๋กœ ์—ฐ๊ฒฐํ•ด์ฃผ์–ด์•ผ ํ•˜๋ฏ€๋กœ ๋ฒกํ„ฐ ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ–ˆ๋‹ค.

vector<int> tree[MAX];

 

๊ทธ๋ฆฌ๊ณ  ์ž…๋ ฅ๊ฐ’์„ ์–‘๋ฐฉํ–ฅ์œผ๋กœ ์—ฐ๊ฒฐํ•ด์คฌ๋‹ค.

tree[n].push_back(m);
tree[m].push_back(n);

 

2) DP๋Š” leaf๋…ธ๋“œ๊นŒ์ง€ ํƒ์ƒ‰ํ•ด์•ผํ•˜๋ฏ€๋กœ DFS๋ฅผ ์‚ฌ์šฉํ–ˆ๊ณ , DP, visited ๋ฐฐ์—ด๋กœ ํ‘œํ˜„ํ–ˆ๋‹ค.

int dp[MAX][2];
bool visited[MAX];

 

3) ๊ทธ๋ฆฌ๊ณ  root ๋…ธ๋“œ๋ถ€ํ„ฐ DFS๋กœ leaf๋…ธ๋“œ๋ฅผ ์ฐพ๋Š”๋‹ค. ์ด ๋•Œ, ์ž์‹ ์ด ์–ผ๋ฆฌ์–ด๋‹ตํ„ฐ์ผ ๋•Œ์™€ ์–ผ๋ฆฌ์–ด๋‹ตํ„ฐ๊ฐ€ ์•„๋‹ ๋•Œ๋ฅผ ์ €์žฅํ•ด์ค€๋‹ค. leaf๋…ธ๋“œ์—์„œ ๋‹ค์‹œ ๋Œ์•„์˜ฌ ๋•Œ, ์ด ๊ฐ’์„ ๋”ํ•ด์ฃผ๋ฉด์„œ ์ตœ์†Œ๊ฐ’์„ ์ฐพ์„ ๊ฑฐ๋‹ค.

dp[current_node][0] = 0; //๋‚ด๊ฐ€ ์–ผ๋ฆฌ์–ด๋‹ตํ„ฐ๊ฐ€ ์•„๋‹ˆ๋ฉด ์–ผ๋ฆฌ์–ด๋‹ตํ„ฐ์˜ ์ˆ˜๋ฅผ ๋Š˜๋ ค์ฃผ์ง€ ์•Š๋Š”๋‹ค.
dp[current_node][1] = 1; //๋‚ด๊ฐ€ ์–ผ๋ฆฌ์–ด๋‹ตํ„ฐ๋ผ๋ฉด ์–ผ๋ฆฌ์–ด๋‹ตํ„ฐ์˜ ์ˆ˜๋ฅผ ํ•˜๋‚˜ ๋Š˜๋ ค์ค€๋‹ค.

 

4) ์ฆ‰, ๋งˆ์ง€๋ง‰ leaf๋…ธ๋“œ์— ๋„๋‹ฌํ–ˆ๋‹ค๋ฉด(์—ฐ๊ฒฐ๋œ ์ž์‹๋…ธ๋“œ๊ฐ€ ๋”์ด์ƒ ์—†๋‹ค๋ฉด) DFS๋ฅผ returnํ•˜๋ฉด์„œ ํ˜„์žฌ ๋…ธ๋“œ๊ฐ€ ์–ผ๋ฆฌ์–ด๋‹ตํ„ฐ ์ผ ๋•Œ์™€ ์–ผ๋ฆฌ์–ด๋‹ตํ„ฐ๊ฐ€ ์•„๋‹ ๋•Œ๋ฅผ ์ตœ์†Œ๊ฐ’์œผ๋กœ ๊ฐฑ์‹ ํ•ด์ค€๋‹ค.

dp[current_node][0] += dp[neighbor_node][1];//๋‚ด๊ฐ€ ์–ผ๋ฆฌ์–ด๋‹ตํ„ฐ๊ฐ€ ์•„๋‹ˆ๋ฉด, ๋‚ด ์นœ๊ตฌ๋Š” ์–ผ๋ฆฌ์–ด๋‹ตํ„ฐ์—ฌ์•ผ ํ•œ๋‹ค 
dp[current_node][1] += min(dp[neighbor_node][0], dp[neighbor_node][1]);//๋‚ด๊ฐ€ ์–ผ๋ฆฌ์–ด๋‹ตํ„ฐ๋ฉด, ์นœ๊ตฌ๋Š” ์–ผ๋ฆฌ์–ด๋‹ตํ„ฐ์—ฌ๋„ ๋˜๊ณ  ์•„๋‹ˆ์–ด๋„ ๋œ๋‹ค.

 

5) ๋งˆ์ง€๋ง‰์œผ๋กœ root๋…ธ๋“œ์— ๋‹ค๋‹ฌํ–ˆ๋‹ค๋ฉด DFS ํƒ์ƒ‰ ๋!
root๋…ธ๋“œ๊ฐ€ ์–ผ๋ฆฌ์–ด๋‹ตํ„ฐ ์ผ ๋•Œ์™€ ์–ผ๋ฆฌ์–ด๋‹ตํ„ฐ๊ฐ€ ์•„๋‹ ๋•Œ๋ฅผ ๋น„๊ตํ•ด์„œ ์ตœ์†Œ๊ฐ’์„ ์ถœ๋ ฅํ•ด์ค€๋‹ค.

min(dp[1][0], dp[1][1]);

 

[์ฝ”๋“œ]

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

const int MAX = 1000001;

vector<int> tree[MAX];
int dp[MAX][2];
bool visited[MAX];

void SearchEarlyAdaptorNum(int);

int main() {
	int node_num = 0;
	cin >> node_num;

	int n = 0, m = 0;
	for (int i = 0; i < node_num; i++) {
		cin >> n >> m;
		tree[n].push_back(m);
		tree[m].push_back(n);//์–‘๋ฐฉํ–ฅ์œผ๋กœ ์—ฐ๊ฒฐ
	}
	SearchEarlyAdaptorNum(1);//root๋…ธ๋“œ๋ถ€ํ„ฐ DFS๋กœ leaf๋…ธ๋“œ๊นŒ์ง€ ํƒ์ƒ‰
	cout << min(dp[1][0], dp[1][1]);//๋งˆ์ง€๋ง‰ root๋…ธ๋“œ์˜ ๊ฐ’ ์ถœ๋ ฅ

	return 0;
}

void SearchEarlyAdaptorNum(int current_node) {
	visited[current_node] = true;
	dp[current_node][0] = 0; //๋‚ด๊ฐ€ ์–ผ๋ฆฌ์–ด๋‹ตํ„ฐ๊ฐ€ ์•„๋‹ ๋•Œ. ์–ผ๋ฆฌ์–ด๋‹ตํ„ฐ 1๋ช… ์ถ”๊ฐ€์•ˆํ•จ.
	dp[current_node][1] = 1; //๋‚ด๊ฐ€ ์–ผ๋ฆฌ์–ด๋‹ตํ„ฐ ์ผ ๋•Œ. ์–ผ๋ฆฌ์–ด๋‹ตํ„ฐ 1๋ช… ์ถ”๊ฐ€.
	for (int i = 0; i < tree[current_node].size(); i++) {
		int neighbor_node = tree[current_node][i];
		if (!visited[neighbor_node]) {
			SearchEarlyAdaptorNum(neighbor_node);//DFS
			//์ž์‹๋…ธ๋“œ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์—ญ์œผ๋กœ ์ฑ„์›Œ์ฃผ๋Š” ๊ณผ์ •
			dp[current_node][0] += dp[neighbor_node][1];//๋‚ด๊ฐ€ ์–ผ๋ฆฌ์–ด๋‹ตํ„ฐ๊ฐ€ ์•„๋‹ˆ๋ฉด, ๋‚ด ์นœ๊ตฌ๋Š” ์–ผ๋ฆฌ์–ด๋‹ตํ„ฐ์—ฌ์•ผ ํ•œ๋‹ค
			dp[current_node][1] += min(dp[neighbor_node][0], dp[neighbor_node][1]);//๋‚ด๊ฐ€ ์–ผ๋ฆฌ์–ด๋‹ตํ„ฐ๋ฉด, ์นœ๊ตฌ๋Š” ์–ผ๋ฆฌ์–ด๋‹ตํ„ฐ์—ฌ๋„ ๋˜๊ณ  ์•„๋‹ˆ์–ด๋„ ๋œ๋‹ค.
		}
	}
}
728x90