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

8-seongwon030 #37

Merged
merged 1 commit into from
May 11, 2024
Merged

8-seongwon030 #37

merged 1 commit into from
May 11, 2024

Conversation

seongwon030
Copy link
Collaborator

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

๊ฒฐํ˜ผ์‹

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

1์‹œ๊ฐ„

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

  1. ์นœ๊ตฌ๊ด€๊ณ„๋ฅผ 2์ฐจ์› ๋ฆฌ์ŠคํŠธ์— ์ €์žฅํ•œ๋‹ค.
    ex) ๋งŒ์•ฝ 1 3์ด๋ฉด 0๋ถ€ํ„ฐ ์ธ๋ฑ์Šค๊ฐ€ ์‹œ์ž‘ํ•˜๋ฏ€๋กœ arr[0][2]์™€ arr[2][0]์˜ ๊ฐ’์„ 1๋กœ ์„ค์ •ํ•œ๋‹ค.
  2. ๋ฐฉ๋ฌธ ๋ฆฌ์ŠคํŠธ๋ฅผ 0์œผ๋กœ ์„ค์ •ํ•œ๋‹ค.
  3. ํ์— ์ธ๋ฑ์Šค 0๋ถ€ํ„ฐ ๋„ฃ๊ณ  ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ๋ฅผ ํ•œ๋‹ค.
    -> ์ธ๋ฑ์Šค 0์€ ์ƒ๊ทผ์ด ์ž์‹ ์ด๋ฏ€๋กœ ๋ฌด์กฐ๊ฑด ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ
  4. ํ๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•˜๊ณ , ํ์—์„œ ๋…ธ๋“œ๋ฅผ ํ•˜๋‚˜์”ฉ ๊บผ๋‚ด์–ด ๊ทธ๊ฒƒ๊ณผ ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ๊ฐ€ ๋ฐฉ๋ฌธ์ด ์•ˆ๋˜์—ˆ๋‹ค๋ฉด ํ์— ์ถ”๊ฐ€ํ•˜๊ณ  ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌํ•œ๋‹ค.
  5. BFS๋ฅผ ์‹œ์ž‘ ๋…ธ๋“œ 0์œผ๋กœ ํ˜ธ์ถœํ•œ๋‹ค.
  6. ๋ฐฉ๋ฌธ์ˆœ์„œ๊ฐ€ 2๋‚˜ 3์ผ ๋•Œ ์นด์šดํŠธ๋ฅผ ์„ผ๋‹ค. <= 1๋ฒˆ์€ ์ž๊ธฐ์ž์‹ ์ด๊ณ  2๋ฒˆ์€ ์นœ๊ตฌ, 3๋ฒˆ์€ ์นœ๊ตฌ์˜ ์นœ๊ตฌ์ด๋‹ค.

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

๏ฟฝ์ธ์ ‘๋ฆฌ์ŠคํŠธ๋กœ ๊ฐ„์„ ์„ ๊ตฌํ˜„ํ•ด๋ดค์Šต๋‹ˆ๋‹น

Comment on lines +12 to +13
arr[a-1][b-1] = 1 # ๋…ธ๋“œ ์ €์žฅ
arr[b-1][a-1] = 1
Copy link

@9kyo-hwang 9kyo-hwang Apr 15, 2024

Choose a reason for hiding this comment

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

์ด๊ฑด ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ๊ฐ€ ์•„๋‹ˆ๋ผ ์ธ์ ‘ ํ–‰๋ ฌ์ธ๋””...?

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.

์ €๋Š” 1์ดŒ ์นœ๊ตฌ์ผ๋•Œ๋งŒ queue์— ์ง‘์–ด ๋„ฃ๋Š” ๋ฐฉ์‹์œผ๋กœ ์ฒ˜๋ฆฌํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค!

void Solve()
{
	cin >> n >> m;
	n++;
	relation.assign(n, vector<int>());
	visited.assign(n, false);

	int a, b;
	for (int i = 0; i < m; i++)
	{
		cin >> a >> b;
		relation[a].push_back(b);
		relation[b].push_back(a);
	}

	invite.push(1);
	visited[1] = true;
	while (!invite.empty())
	{
		int cur = invite.front();
		invite.pop();

		for (auto num : relation[cur])
		{
			if (visited[num]) continue;
			if (cur == 1) invite.push(num);
			answer.push_back(num);
			visited[num] = true;
		}
	}

	cout << answer.size(); 
}

@InSange
Copy link
Collaborator

InSange commented Apr 27, 2024

์ €๋Š” 1์ดŒ ์นœ๊ตฌ์ผ๋•Œ๋งŒ queue์— ์ง‘์–ด ๋„ฃ๋Š” ๋ฐฉ์‹์œผ๋กœ ์ฒ˜๋ฆฌํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค!

void Solve()
{
	cin >> n >> m;
	n++;
	relation.assign(n, vector<int>());
	visited.assign(n, false);

	int a, b;
	for (int i = 0; i < m; i++)
	{
		cin >> a >> b;
		relation[a].push_back(b);
		relation[b].push_back(a);
	}

	invite.push(1);
	visited[1] = true;
	while (!invite.empty())
	{
		int cur = invite.front();
		invite.pop();

		for (auto num : relation[cur])
		{
			if (visited[num]) continue;
			if (cur == 1) invite.push(num);
			answer.push_back(num);
			visited[num] = true;
		}
	}

	cout << answer.size(); 
}

2์ดŒ ์ผ๋•Œ๋Š” queue์— ๋„ฃ๋Š” ๊ฒƒ์ด ์•„๋‹Œ ๋‹ต์— ๋ฐ”๋กœ ์ฒดํฌ!
์ค‘๋ณต์€ visited๋กœ ๋งค๋ฌด๋ฆฌ๊นŒ์ง€
๊ฒฐํ˜ผ์‹ ๊ธฐ๋Œ€ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค

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.

bfs ์ˆœํšŒํ•˜๋ฉด์„œ ์ •๋‹ต cnt ์—ฐ์‚ฐ๋„ ๊ฐ™์ด ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์•„์š”!

Copy link
Contributor

@dhlee777 dhlee777 left a comment

Choose a reason for hiding this comment

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

์ธ์ ‘ํ–‰๋ ฌ์„ ์ด์šฉํ•œ bfs ํ’€์ด ์ž˜๋ณด์•˜์Šต๋‹ˆ๋‹ค! ์ฝ”๋“œ๊ฐ€ ์ „์ฒด์ ์œผ๋กœ ์ฝ๊ธฐ์ข‹๊ฒŒ ๊น”๋”ํ•œ๊ฑฐ ๊ฐ™์Šต๋‹ˆ๋‹ค

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.

5 participants