๐Ÿ“ Coding Test Study/Algorithm Problem

[C++][Goorm][ํƒ์ƒ‰] ์นด๋“œ ๊ตํ™˜ํ•˜๊ธฐ

ibelieveinme 2023. 6. 11. 15:44
728x90
 

๊ตฌ๋ฆ„EDU - ๋ชจ๋‘๋ฅผ ์œ„ํ•œ ๋งž์ถคํ˜• IT๊ต์œก

๊ตฌ๋ฆ„EDU๋Š” ๋ชจ๋‘๋ฅผ ์œ„ํ•œ ๋งž์ถคํ˜• IT๊ต์œก ํ”Œ๋žซํผ์ž…๋‹ˆ๋‹ค. ๊ฐœ์ธ/ํ•™๊ต/๊ธฐ์—… ๋ฐ ๊ธฐ๊ด€ ๋ณ„ ์ตœ์ ํ™”๋œ IT๊ต์œก ์†”๋ฃจ์…˜์„ ๊ฒฝํ—˜ํ•ด๋ณด์„ธ์š”. ๊ธฐ์ดˆ๋ถ€ํ„ฐ ์‹ค๋ฌด ํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ต์œก, ์ „๊ตญ ์ดˆ์ค‘๊ณ /๋Œ€ํ•™๊ต ์˜จ๋ผ์ธ ๊ฐ•์˜, ๊ธฐ์—…/

edu.goorm.io

 

 

 

์‚ฌ๋žŒ๋“ค์ด ๋ฒˆํ˜ธ๊ฐ€ ์ ํžŒ ์นด๋“œ๋ฅผ ๊ฐ–๊ณ  ์žˆ๋Š”๋ฐ, ์ด ๋ฒˆํ˜ธ๊ฐ€ ์ž์‹ ์˜ ๋ฒˆํ˜ธ๋ž‘ ์ตœ๋Œ€ํ•œ ๋น„์Šทํ•ด์•ผ ํ•œ๋‹ค.

์นœ๊ตฌ๊ด€๊ณ„์˜ ์‚ฌ๋žŒ๋“ค์€ ์นด๋“œ๋ฅผ ๊ตํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

์ž์‹ ์˜ ๋ฒˆํ˜ธ๋ž‘ ์นด๋“œ์˜ ๋ฒˆํ˜ธ์˜ ์ฐจ์ด๋ฅผ ๋ถˆ๋งŒ์กฑ๋„๋ผ๊ณ  ํ•˜๋Š”๋ฐ, ์ด ๋ถˆ๋งŒ์กฑ๋„๊ฐ€ ๊ฐ€์žฅ ์ ์€ ํ•ฉ์˜ ๊ฒฝ์šฐ๋ฅผ ๊ตฌํ•ด์„œ ์ตœ์†Œ ๋ถˆ๋งŒ์กฑ๋„ ๊ฐ’์„ ์ถœ๋ ฅํ•˜๋ฉด ๋œ๋‹ค.

 

์‚ฌ๋žŒ๋“ค์˜ ์ˆ˜์™€ ์นด๋“œ๋ฒˆํ˜ธ๋Š” 1๋ณด๋‹ค ํฌ๋ฉฐ, ์นœ๊ตฌ๊ด€๊ณ„๊ฐ€ ์—†์„ ์ˆ˜๋„ ์žˆ๋‹ค.


*ํ•ด๊ฒฐ๋ฒ•

1) ์นœ๊ตฌ๊ด€๊ณ„๋ฅผ ํ‘œํ˜„ํ•œ ํ–‰๋ ฌ์„ ๋งŒ๋“ค๊ณ  ์กฐํ•ฉ์„ ๊ตฌํ•  ๋•Œ, ์นœ๊ตฌ๊ด€๊ณ„์ด๋ฉด ํ•ด๋‹น ์กฐํ•ฉ์„ ์„ ํƒํ•ด์„œ ๋ถˆ๋งŒ์กฑ๊ฐ’์„ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•

2) ์นœ๊ตฌ๊ด€๊ณ„๋ฉด ์นด๋“œ๋ฅผ ๋‹ค ๊ตํ™˜ํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, DFS๋กœ ์นœ๊ตฌ๊ด€๊ณ„๋ฅผ ๊ตฌํ•˜๊ณ  ๊ทธ ์นœ๊ตฌ๋“ค์˜ ์นด๋“œ๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ์‹œํ‚ค๋Š” ๋ฐฉ๋ฒ•

2) ๋ฐฉ๋ฒ•์„ ๊ทธ๋ฆผ์œผ๋กœ ํ‘œํ˜„ํ•˜๋ฉด ์œ„์™€ ๊ฐ™๋‹ค.

 

1๋ฒˆ~4๋ฒˆ ์‚ฌ๋žŒ์ด ์กด์žฌํ•˜๊ณ  1-2๋ฒˆ, 2-3๋ฒˆ์ด ์นœ๊ตฌ์‚ฌ์ด์ด๋‹ค. 1~4๋ฒˆ์ด ๋“ค๊ณ  ์žˆ๋Š” ์นด๋“œ๋Š” 3, 1, 5, 4๋ฒˆ ์ด๋‹ค.

1~3๋ฒˆ์ด ์„œ๋กœ ์—ฐ๊ฒฐ๋œ ์นœ๊ตฌ๊ด€๊ณ„์ด๊ธฐ ๋•Œ๋ฌธ์— ์ด๋“ค์€ ์นด๋“œ๋ฅผ ๋งˆ๊ตฌ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋‹ค.

์ด ๋•Œ, ๋ถˆ๋งŒ์กฑ๋„๋ฅผ ์ตœ์†Œ๋กœ ๊ฐ–๊ฒŒ ํ•˜๋ ค๋ฉด ์นด๋“œ๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•œ ํ›„(1, 3, 5) ์ˆœ์„œ๋Œ€๋กœ 1,2,3๋ฒˆ์ด ๊ฐ–๊ฒŒ ํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’์ผ ๊ฒƒ์ด๋‹ค.(4๋ฒˆ ์นœ๊ตฌ๋Š” ์นœ๊ตฌ๊ด€๊ณ„๊ฐ€ ์—†์œผ๋ฏ€๋กœ ์นด๋“œ๋ฅผ ๊ตํ™˜ํ•  ์ˆ˜ ์—†๋‹ค.)

์ฆ‰, ์ตœ์†Œ ๋ถˆ๋งŒ์กฑ๋„๋Š” ์œ„์™€ ๊ฐ™์ด ์นด๋“œ๋ฅผ ๊ตํ™˜ํ–ˆ์„ ๋•Œ์ด๊ณ  3์„ ์ถœ๋ ฅํ•˜๋ฉด ๋œ๋‹ค.

 

*์•Œ์•„๋‘˜ ๊ฒƒ

1) ๋ฒกํ„ฐ ํฌ๊ธฐ ๋ถ€์—ฌํ•˜๋ฉฐ ์ƒ์„ฑํ•˜๋Š” ๋ฒ•

vector<int> v(5); //ํฌ๊ธฐ5์ธ ๋ฒกํ„ฐ ์ƒ์„ฑ
vector<int> v(5, 0); // 5ํฌ๊ธฐ๋กœ ๋งŒ๋“ค๊ณ  0์œผ๋กœ ์ดˆ๊ธฐํ™”

 

2) ๋ฒกํ„ฐ๋กœ ๊ทธ๋ž˜ํ”„/ํŠธ๋ฆฌ ๊ด€๊ณ„ ํ‘œํ˜„ํ•˜๋Š” ๋ฒ•

vector<int> *graph = new vector<int>[3];

graph[0].push_back(1);
graph[1].push_back(0);
graph[0].push_back(2);
graph[2].push_back(0);

์ฆ‰, 0 1 ์ด ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋‹ค๊ณ  ์ฃผ์–ด์ง€๋ฉด, 1๋ฐฐ์—ด ๊ฐ’์— 0 ๋„ ๋ฒกํ„ฐ๋กœ ์ถ”๊ฐ€ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

 

3) ๋ฒกํ„ฐ resize

vector<int> v;

v.resize(5, 0);

5 ํฌ๊ธฐ๋กœ ์žฌ์„ค์ •ํ•˜๊ณ  0์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•˜๊ธฐ

 

*์ฝ”๋“œ

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

vector<int> cardGroup, personGroup;
vector<bool> visited;

void DFS(int currentPerson, vector<int> cards, vector<int> *relations){
	cardGroup.push_back(cards[currentPerson]);
	personGroup.push_back(currentPerson);
	
	for(auto& next: relations[currentPerson]){
		if(visited[next]) continue;
		visited[next] = true;
		DFS(next, cards, relations);
	}
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	int personNum, relationNum;
	long long answer = 0;
	cin >> personNum >> relationNum;
	
	//์นด๋“œ ์ •๋ณด ๋ฐ›๊ธฐ
	vector<int> cards(personNum + 1);
	for(int i = 1; i <= personNum; i++){
		cin >> cards[i];
	}
	
	//๊ด€๊ณ„๋ฅผ ๋ฒกํ„ฐํ–‰๋ ฌ๋กœ ํ‘œํ˜„
	int person1, person2;
	vector<int> *relations = new vector<int>[personNum + 1];
	for(int i = 0; i < relationNum; i++){
		cin >> person1 >> person2;
		relations[person1].push_back(person2);
		relations[person2].push_back(person1);
	}
	
	//BFS๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” ์‚ฌ๋žŒ๋“ค ์ฐพ๊ธฐ
	visited.resize(personNum, false);
	for(int i = 1; i <= personNum; i++){
		if(visited[i]) continue;
		
		visited[i] = true;
		DFS(i, cards, relations);
		
		//์ •๋ ฌํ•˜๊ธฐ
		sort(cardGroup.begin(), cardGroup.end());
		sort(personGroup.begin(), personGroup.end());
		
		//๋ถˆ๋งŒ์กฑ๋„ ๊ตฌํ•˜๊ธฐ
		for(int j = 0; j < cardGroup.size(); j++){
			answer += abs(cardGroup[j] - personGroup[j]);
		}
		cardGroup.clear();
		personGroup.clear();
	}
	
	//์ •๋‹ต์ถœ๋ ฅ
	cout << answer;
	
	return 0;
}

๊ทผ๋ฐ 10, 12๋ฒˆ์ด ํ‹€๋ ธ๊ณ  28 ~ 59๊นŒ์ง€ ํƒ€์ž„์•„์›ƒ.........................

728x90