Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4-dhlee777 #17

Merged
merged 2 commits into from
Mar 27, 2024
Merged

4-dhlee777 #17

merged 2 commits into from
Mar 27, 2024

Conversation

dhlee777
Copy link
Contributor

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

https://www.acmicpc.net/problem/2606

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

2์‹œ๊ฐ„

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

์ด๋ฌธ์ œ๋Š” ๋‹จ์ˆœํ•œ bfs๋ฅผ ์ด์šฉํ•ด ํ’€ ์ˆ˜ ์žˆ์ง€๋งŒ ์œ ๋‹ˆ์˜จํŒŒ์ธ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ๋„ ํ•œ๋ฒˆ ํ’€์–ด๋ณด์•˜๋‹ค.

์ฒซ๋ฒˆ์งธํ’€์ด

์ฒซ๋ฒˆ์žฌํ’€์ด๋Š” ๊ทธ๋ƒฅ ๋‹จ์ˆœํžˆ bfs๋ฅผ ์ด์šฉํ•˜์—ฌ ํ’€์—ˆ๋‹ค. 1๋ฒˆ ์ปดํ“จํ„ฐ์™€ ์—ฐ๊ฒฐ๋œ ์ปดํ“จํ„ฐ๋Š” ๋ชจ์กฐ๋ฆฌ ๊ฐ์—ผ๋˜๋Š”๊ฒƒ์ด๋ฏ€๋กœ bfs๋ฅผ ์ด์šฉํ•˜์—ฌ 1๋ฒˆ์ปดํ“จํ„ฐ์™€ ์—ฐ๊ฒฐ๋œ ๋ชจ๋“  ์ปดํ“จํ„ฐ๋ฅผ ๋‹ค ํƒ์ƒ‰ํ•ด์ฃผ๋ฉฐ ์นด์šดํŠธ ํ•˜๋ฉด ๋œ๋‹ค. ์—ฌ๊ธฐ์„œ ์ฃผ์˜ํ•ด์•ผํ• ์ ์€ ๋งจ์ฒ˜์Œ์— ์‹œ์ž‘์ •์ ์„ ๋ฐฉ๋ฌธํ–ˆ๋‹ค๊ณ  ํ‘œ์‹œํ•ด์ฃผ๋Š”๊ฒƒ! (visited[a]=1) ๋งจ์ฒ˜์Œ ์ด๋ถ€๋ถ„์„ ๋†“์ณค์–ด์„œ ๊ณ„์† ์ด์ƒํ•œ ๋‹ต์ด ๋‚˜์™”๋‹ค..

#include<iostream>
#include<vector>
#include<queue>
using namespace std;
vector<int>com_edge[101];    //์ปดํ“จํ„ฐ๊ฐ„์˜ ๊ฐ„์„ ์˜ ์ •๋ณด๋ฅผ ์ €์žฅํ•˜๋Š” ์ด์ฐจ์›๋ฒกํ„ฐ
int visited[101];   //๊ฐ์—ผ๋œ ์ปดํ“จํ„ฐ์ธ์ง€ ํ™•์ธํ•˜๊ธฐ์œ„ํ•œ ๋ฐฐ์—ด
queue<int> q;
int coun = 0;
void bfs(int a) {     //bfs ๋ฅผ ์œ„ํ•œ ํ•จ์ˆ˜
	q.push(a);
	visited[a] = 1;
	while (!q.empty()) {
		int com = q.front();
		q.pop();
		for (int i = 0; i < com_edge[com].size(); i++) {
			int com_linked = com_edge[com][i];
			if (!visited[com_linked])
			{
				q.push(com_linked);
				coun++;            //๋ฐฉ๋ฌธ๋˜์ง€์•Š์€ ์—ฐ๊ฒฐ๋œ ์ปดํ“จํ„ฐ์ผ๊ฒฝ์šฐ ์นด์šดํŠธ
				visited[com_linked] = 1;
			}
		}
	}

}
int main(void) {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int com_num, edge, a, b;
	cin >> com_num >> edge;
	for (int i = 0; i < edge; i++) {
		cin >> a >> b;
		com_edge[a].push_back(b);
		com_edge[b].push_back(a);
	}
	bfs(1);
	cout << coun;
	return 0;
}

๋‘๋ฒˆ์งธํ’€์ด

๋‘๋ฒˆ์งธํ’€์ด๋Š” ์œ ๋‹ˆ์˜จํŒŒ์ธ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ’€์–ด์ฃผ์—ˆ๋‹ค. 1๋ฒˆ์ปดํ“จํ„ฐ์™€ ์—ฐ๊ฒฐ๋œ ์ปดํ“จํ„ฐ๋Š” ๋‹ค ๊ฐ์—ผ๋˜๋Š”๊ฒƒ์ด๋ฏ€๋กœ ์œ ๋‹ˆ์˜จ ํŒŒ์ธ๋“œ๋ฅผ ์ด์šฉํ•ด ๋ฃจํŠธ๋…ธ๋“œ๋ฅผ 1๋ฒˆ ์ปดํ“จํ„ฐ๋กœ ๊ฐ€์ง€๋Š” ์ปดํ“จํ„ฐ๋Š”(1๋ฒˆ ์ปดํ“จํ„ฐ์™€ ๊ฐ™์€ ์ง‘ํ•ฉ์˜ ์ปดํ“จํ„ฐ)๋ชจ๋‘ ๊ฐ์—ผ๋œ ์ปดํ“จํ„ฐ๋ผ๊ณ  ์ƒ๊ฐํ–ˆ๋‹ค.
์ „์ฒด์ ์ธ ํ๋ฆ„์€

  1. addํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์ปดํ“จํ„ฐ ์ง‘ํ•ฉ์„ ๋งŒ๋“ค์–ด์ค€๋‹ค.
  2. for๋ฌธ์„ ํ†ตํ•ด ๋ชจ๋“  ์ปดํ“จํ„ฐ์˜ ๋ฃจํŠธ ์ปดํ“จํ„ฐ๋ฅผ ํ™•์ธํ•ด์ฃผ๋ฉฐ ๋งŒ์•ฝ ๋ฃจํŠธ์ปดํ“จํ„ฐ๊ฐ€ 1์ผ๊ฒฝ์šฐ ๊ฐ์—ผ๋œ ์ปดํ“จํ„ฐ์™€ ๊ฐ™์€ ์ง‘ํ•ฉ์ด๋ฏ€๋กœ ์นด์šดํŠธํ•ด์ค€๋‹ค.
  3. 1๋ฒˆ์ปดํ“จํ„ฐ๋ฅผ ํ†ตํ•ด ๊ฐ์—ผ๋œ ์ปดํ“จํ„ฐ์˜ ์ˆ˜๋ฅผ ์นด์šดํŠธ ํ•˜๋Š”๊ฒƒ์ด๋ฏ€๋กœ 1๋ฒˆ์ปดํ“จํ„ฐ๋Š” ์นด์šดํŠธ์—์„œ ์ œ๊ฑฐํ•ด์ค€๋‹ค.
#include<iostream>
using namespace std;
int parent [101];      //์–ด๋–ค์ปดํ“จํ„ฐ์˜ ๋ถ€๋ชจ์ปดํ“จํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฐฐ์—ด
int find_parent(int a) {      //๋ฃจํŠธ ์ปดํ“จํ„ฐ๋ฅผ ์ฐพ๋Š” ํ•จ์ˆ˜
	if (a == parent[a]) return a;
	else return parent[a] = find_parent(parent[a]);

}
void add(int a, int b) {    //๋‘๊ฐœ์˜ ์ปดํ“จํ„ฐ๊ฐ€ ์†ํ•œ ๊ฐ๊ฐ์˜ ์ง‘ํ•ฉ์„ ํ•ฉ์น˜๋Š” ํ•จ์ˆ˜
	a = find_parent(a);
	b = find_parent(b);
	if (a > b) parent[a] = b;
	else parent[b] = a;
}
int main(void) {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int com_num, edge,a,b;       
	cin >> com_num >> edge;
	for (int i = 1; i <= com_num; i++) {   //์ œ์ผ์ฒ˜์Œ ์ž์‹ ์˜ ๋ถ€๋ชจ๋Š” ์ž์‹ ์ด๊ธฐ์— ์ดˆ๊ธฐํ™”ํ•ด์ค€๋‹ค.
		parent[i] = i;
	}
	for (int i = 0; i < edge; i++) {
		cin >> a >> b;
		add(a, b);
	}
	int coun = 0;
	for (int i = 1; i <= com_num; i++) {     // ์–ด๋–ค์ปดํ“จํ„ฐ์˜ ๋ฃจํŠธ์ปดํ“จํ„ฐ๊ฐ€ 1์ด๋ผ๋ฉด 1๊ณผ ์—ฐ๊ฒฐ๋œ ๊ฐ™์€ ์ง‘ํ•ฉ์ด๋ฏ€๋กœ ์ˆซ์ž๋ฅผ ์นด์šดํŠธํ•œ๋‹ค.
		if (find_parent(i) == 1) coun++;
	}
	
	cout << coun-1;    //1๋ฒˆ ์ปดํ“จํ„ฐ์—์˜ํ•ด ๊ฐ์—ผ๋œ ์ปดํ“จํ„ฐ์˜ ์ˆ˜๋ฅผ ์„ธ๋Š”๊ฒƒ์ด๋ฏ€๋กœ 1๋ฒˆ์ปดํ“จํ„ฐ๋ฅผ ์ œ์™ธํ•ด์ค€๋‹ค.
	return 0;
}

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

๋‹จ์ˆœํ•œ bfs๋ฌธ์ œ์ด์ง€๋งŒ ์œ ๋‹ˆ์˜จํŒŒ์ธ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ํ’€์–ด๋ณด๋‹ˆ ๋ญ”๊ฐ€ ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ ๋” ํฌ๊ณ  ๋น„ํšจ์œจ์ ์ธ๊ฒƒ ๊ฐ™๊ธดํ•˜์ง€๋งŒ ์ƒ‰๋‹ฌ๋ž๋‹ค.

Copy link
Collaborator

@InSange InSange left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์œ ๋‹ˆ์˜จ ํŒŒ์ธ๋“œ๋กœ ์ ‘๊ทผํ–ˆ์„ ๋•Œ๋Š” ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง€๋Š” a, b๊ฐ’์„ ์ผ์ผ์ด ํŠธ๋ฆฌ ๋†’์ด๋ฅผ ์˜ฌ๋ ค์น˜๊ธฐํ•˜๋ฉด์„œ ๋น„๊ตํ•ด์•ผ๋˜๋‹ค๋ณด๋‹ˆ ๊นŠ์ด์˜ ํฌ๊ธฐ๋งŒํผ ์‹œ๊ฐ„ ๋ณต์žก๋„๊ฐ€ ์ฃผ์–ด์ง€๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!
๊ทธ์— ๋ฐ˜ํ•ด bfs๋Š” ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” ๋…ธ๋“œ๋“ค์ด ์—ฐ๊ณ„๋˜๋Š” ๋ฐฉ์‹์ด๋‹ค๋ณด๋‹ˆ ํ•œ ๋ฒˆ ๋น„๊ตํ•ด์ฃผ๋ฉด ์ญ‰ ์—ฐ๊ฒฐ๋˜๊ธฐ๋•Œ๋ฌธ์— ์ฐจ์ด๊ฐ€ ์–ด๋Š์ •๋„ ๋‚˜๋Š” ๊ฒƒ ๊ฐ™๋„ค์š”!
์ถ”ํ›„์—๋Š” ์‹œ๊ฐ„ ๋ณต์žก๋„๊นŒ์ง€ ๋ช…์‹œํ•ด์ฃผ์‹œ๋ฉด ๋ณด๊ธฐ ๋” ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹น!!
๋‘ ๊ฐ€์ง€ ์ ‘๊ทผ๋ฒ•์œผ๋กœ ํ‘ธ๋Š” ๋ฐฉ๋ฒ• ๋„ˆ๋ฌด ์ข‹์•˜์Šต๋‹ˆ๋‹ค~

@dhlee777
Copy link
Contributor Author

์ถ”ํ›„์—๋Š” ํ•œ๋ฒˆ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ๋ช…์‹œํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค!

Copy link
Collaborator

@seongwon030 seongwon030 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋ฐ”์ด๋Ÿฌ์Šค ๋ฌธ์ œ์— ์œ ๋‹ˆ์˜จํŒŒ์ธ๋“œ๋ฅผ ์ ์šฉํ•ด์„œ ํ’€ ์ˆ˜ ์žˆ์—ˆ๋„ค์š”. ๊ฐ™์€ ์ง‘ํ•ฉ์ธ์ง€ ํ™•์ธํ•˜๋Š” ๊ณผ์ •์—์„œ ๋ฃจํŠธ ๋…ธ๋“œ๊ฐ€ 1์ผ๋•Œ ๊ฐ์—ผ๋œ ๊ฒƒ์ด๋ฏ€๋กœ ๊ฐ™์€ ์ง‘ํ•ฉ์œผ๋กœ ๋ถ„๋ฅ˜ํ•˜์—ฌ ์นด์šดํŠธํ•˜๋Š” ๋ฐฉ์‹ ๋„ˆ๋ฌด ์ž˜ ๋ดค์Šต๋‹ˆ๋‹ค ใ…Žใ…Ž

Copy link
Collaborator

@yuyu0830 yuyu0830 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2๋ฒˆ์งธ ์ ‘๊ทผ์€ ์ƒ๊ฐ ๋ชปํ–ˆ๋„ค์š”. ์žฌ๋ฐŒ๊ฒŒ ์ž˜ ๋ดค์Šต๋‹ˆ๋‹ค :)
๋‚˜๋จธ์ง€๋Š” ์ „๋ถ€ ์ž˜ ์ดํ•ดํ–ˆ๋Š”๋ฐ ํ˜น์‹œ count ๋ณ€์ˆ˜๋ฅผ coun ์ด๋ผ๊ณ  ์“ฐ๋Š” ์ด์œ ๊ฐ€ ์žˆ๋‚˜์š”? cnt๋‚˜ count, c ๊นŒ์ง€๋Š” ๋ดค๋Š”๋ฐ coun ์€ ๋˜ ์ฒ˜์Œ์ด๋„ค์š” ใ…Žใ…Ž

@dhlee777
Copy link
Contributor Author

count ๋ผ ์„ ์–ธํ•ด์ฃผ๋‹ˆ๊นŒ ๊ณ„์† ํ‚ค์›Œ๋“œ๊ฐ€ ๋ชจํ˜ธํ•˜๋‹ค๊ณ  ํ•ด์„œ t๋ฅผ ๋นผ์„œ coun์œผ๋กœ ๋ฐ”๊ฟ”์คฌ๋Š”๋ฐ ๋“ฃ๊ณ ๋ณด๋‹ˆ cnt๊ฐ€ ๋” ์ง๊ด€์ ์ธ๊ฑฐ ๊ฐ™์Šต๋‹ˆ๋‹ค ๋‹ค์Œ๋ถ€ํ„ฐ๋Š” cnt๋กœ ์„ ์–ธํ•ด์ค˜์•ผ๊ฒ ๋„ค์š” !

@dhlee777 dhlee777 merged commit a0eb419 into main Mar 27, 2024
@dhlee777 dhlee777 deleted the 4-dhlee777 branch March 27, 2024 05:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants