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

24-InSange #83

Merged
merged 5 commits into from
Aug 9, 2024
Merged

24-InSange #83

merged 5 commits into from
Aug 9, 2024

Conversation

InSange
Copy link
Collaborator

@InSange InSange commented Aug 4, 2024

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

ํŠธ๋ฆฌ

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

4์‹œ๊ฐ„

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

๋ฌธ์ œ ๊ฐœ์š”

๋…ธ๋“œ์˜ ๊ฐœ์ˆ˜ N๊ฐœ๊ฐ€ ์ฃผ์–ด์ง€๊ณ  ๊ฐ ๋…ธ๋“œ์˜ ๋ถ€๋ชจ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ๊ฐ€ N๊ฐœ ์ฃผ์–ด์ง„๋‹ค.
๋ฃจํŠธ ๋…ธ๋“œ์˜ ๋ถ€๋ชจ๋Š” -1์ด๋‹ค.
๊ทธ๋ฆฌ๊ณ  ๋งˆ์ง€๋ง‰์œผ๋กœ ์ง€์šฐ๊ณ ์ž ํ•˜๋Š” ๋…ธ๋“œ์˜ ๋ฒˆํ˜ธ๊ฐ€ ์ฃผ์–ด์ง€๋ฉฐ ํ•ด๋‹น ๋…ธ๋“œ์˜ ์ž์‹ ๋…ธ๋“œ๋„ ๋‹ค ์—†์–ด์ง„๋‹ค.

์ ‘๊ทผ ๋ฐฉ์‹

๊ฐ ๋…ธ๋“œ์—๋Š” ์ž์‹๋“ค์˜ ์ธ๋ฑ์Šค๋ฅผ ์ €์žฅํ•  ๋ฐฐ์—ด๊ณผ ๋ถ€๋ชจ ๋…ธ๋“œ์˜ ์ฃผ์†Œ๋ฅผ ์ €์žฅํ•  ๋ณ€์ˆ˜๋ฅผ ์ง€๋‹ˆ๊ณ  ์žˆ์œผ๋ฉฐ ์ถ”๊ฐ€๋กœ ๋ฆฌํ”„๋…ธ๋“œ์ธ์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” ๋ถˆ๋ฆฌ์–ธ ๋ณ€์ˆ˜๋„ ์ง€๋‹ˆ๊ณ  ์žˆ๋‹ค.
ํ•ด๋‹น ๋…ธ๋“œ๋Š” ๊ตฌ์กฐ์ฒด๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์œผ๋ฉฐ ๊ฐ ๋…ธ๋“œ๋“ค์— ๋Œ€ํ•œ ์ฃผ์†Œ๋Š” ๋ฐฐ์—ด nodes์— ์ €์žฅ๋œ๋‹ค.
์ง€์šฐ๊ณ ์ž ํ•˜๋Š” ๋…ธ๋“œ๋ฅผ ์ž…๋ ฅ๋ฐ›์œผ๋ฉด ํ•ด๋‹น ๋…ธ๋“œ๊ฐ€ ๋ฃจํŠธ ๋…ธ๋“œ์ธ์ง€ ํŒŒ์•…ํ•˜์—ฌ ๋ฃจํŠธ ๋…ธ๋“œ๊ฐ€ ์•„๋‹ ๊ฒฝ์šฐ ํ•ด๋‹น ๋…ธ๋“œ์˜ ๋ถ€๋ชจ ๋…ธ๋“œ๋ฅผ ์—…๋ฐ์ดํŠธ ์‹œ์ผœ์ค€๋‹ค.

#include <iostream>
#include <vector>
#include <queue>

using namespace std;

struct Node
{
	Node()
	{
		parent = nullptr;
		leafNode = true;
	}

	vector<int> childs;
	Node* parent;
	bool leafNode;
};

vector<Node*> nodes;
int N, ans = 0, rIndex;

void NodeUpdate(Node* curNode)
{
	for (auto child : curNode->childs)
	{
		if (nodes[child] != nullptr)
		{
			curNode->leafNode = false;
			return;
		}
	}
	curNode->leafNode = true;
}

void Solve()
{
	cin >> N;

	for (int i = 0; i < N; i++)
	{
		Node* node = new Node();

		nodes.push_back(node);
	}
	int root_index = 0;

	for(int i = 0; i < N; i++)
	{
		int p;
		cin >> p;

		if (p == -1)
		{
			root_index = i;
			continue;
		}
		nodes[i]->parent = nodes[p];
		nodes[i]->parent->childs.push_back(i);
		if (nodes[i]->parent)
		{
			NodeUpdate(nodes[i]->parent);
		}
	}

	cin >> rIndex;
	if (rIndex == root_index)	// ์ œ๊ฑฐ ๋œ๊ฒŒ ๋ฃจํŠธ๋ผ๋ฉด
	{
		cout << ans << "\n";
		return;
	}
	Node* parentNode = nodes[rIndex]->parent;
	delete nodes[rIndex];
	nodes[rIndex] = nullptr;
	NodeUpdate(parentNode);

	queue<Node*> q;
	q.push(nodes[root_index]);

	while (!q.empty())
	{
		Node* cur = q.front();
		q.pop();

		if (cur->leafNode) ans++;
		for (auto index : cur->childs)
		{
			if (nodes[index] != nullptr)
			{
				q.push(nodes[index]);
			}
		}
	}

	cout << ans;
}

int main()
{
	cin.tie(nullptr);
	ios::sync_with_stdio(false);

	Solve();

	return 0;
}

์ฒ˜์Œ์— ์ ‘๊ทผํ•  ๋•Œ ๋ฃจํŠธ ๋…ธ๋“œ๋Š” ๋งจ ์ฒ˜์Œ ๊ฒƒ์ด ์•„๋‹ ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ๊ณผ ์ง€์›Œ์ง€๋Š” ๋…ธ๋“œ๊ฐ€ ๋ฃจํŠธ ๋…ธ๋“œ์ผ ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์„ ๊ฐ„๊ณผํ•˜์—ฌ ํ‹€๋ฆฌ๊ฒŒ ๋˜์—ˆ๊ณ  ํ•ด๋‹น ๋ถ€๋ถ„์„ ํŒŒ์•…ํ›„ ์˜ˆ์™ธ ์ฒ˜๋ฆฌํ•ด์ค€ ๋’ค ํ†ต๊ณผํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

image

ํ›„๊ธฐ

๊ตฌ์กฐ์ฒด๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋…ธ๋“œํ˜•์‹์œผ๋กœ ํ’€์ดํ•˜๋Š” ๋ฐฉ์‹์ด ์˜ค๋žœ๋งŒ์ด์—ˆ๋‹ค.
ํŠนํžˆ ํฌ์ธํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ถ€๋ถ„์—์„œ ํ—ท๊ฐˆ๋ฆฌ๋Š” ๋ถ€๋ถ„๋„ ๋ฐœ์ƒํ•˜๋Š” ๋ฌธ์ œ๊ฐ€ ์žˆ์—ˆ๋‹ค.
์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹์ž„์—๋„ ์•Œ๊ณ ๋ฆฌ์ฆ˜์—์„œ ๋ง‰ํžˆ๋‹ค๋ณด๋‹ˆ ์ž์ฃผ ์—ฐ์Šตํ•ด์•ผ๊ฒ ๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ค์—ˆ๋‹ค.

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

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.

์ง„์งœ ํŠธ๋ฆฌ๋ฅผ ๋งŒ๋“ค์–ด์„œ ํ’€์—ˆ๋„ค์š”.. ๋ฌธ์ œ ์ž์ฒด๋Š” ์–ด๋ ค์šด ๋ฌธ์ œ๋Š” ์•„๋‹ˆ์ง€๋งŒ ๋ฃจํŠธ ์ œ๊ฑฐ ๊ฐ™์ด ์˜์™ธ๋กœ ๊ท€์ฐฎ์€ ์กฐ๊ฑด๋“ค์ด ์žˆ๋Š” ๋ฌธ์ œ๋กœ ๊ธฐ์–ตํ•ฉ๋‹ˆ๋‹ค..

ํ™•์‹คํžˆ ์ž๋ฃŒ๊ตฌ์กฐ๋Š” ์ž์ฃผ ์‚ฌ์šฉํ•ด์ค˜์•ผํ•˜๋Š” ๊ฒƒ ๊ฐ™์•„์š”.. ๊นŒ๋จน๊ธฐ ๋„ˆ๋ฌด ์‰ฌ์›€

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.

์ฝ”๋“œ๊ฐ€ ๊ธฐ๋Šฅํ•˜๋Š” ๋‹จ์œ„๊ฐ€ ๋ˆˆ์— ์ž˜ ๋ณด์—ฌ์„œ ์ข‹์•˜์–ด์š”. ๋ช…๋ช…๋„ ์ž˜ ๋˜์–ด ์žˆ์–ด ๊ตฌ์กฐ์ฒด๋กœ ์„ ์–ธ๋œ ๋ณ€์ˆ˜๋“ค์ด ์–ด๋–ค ์—ญํ• ์„ ํ•˜๋Š”์ง€ ์‰ฝ๊ฒŒ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

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.

3 participants