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

13-InSange #53

Merged
merged 2 commits into from
Jul 10, 2024
Merged

13-InSange #53

merged 2 commits into from
Jul 10, 2024

Conversation

InSange
Copy link
Collaborator

@InSange InSange commented May 21, 2024

πŸ”— 문제 링크

νŠΉμ • 거리의 λ„μ‹œ μ°ΎκΈ°

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

30λΆ„

✨ μˆ˜λ„ μ½”λ“œ

μ „ν˜•μ μΈ BFS문제둜 λ°©ν–₯ 간선이 μžˆλŠ” κ·Έλž˜ν”„λ₯Ό νƒμƒ‰ν•˜λŠ” 것이닀.
좜발 λ„μ‹œ Xλ‘œλΆ€ν„° 거리 K만큼 λ–¨μ–΄μ ΈμžˆλŠ” λ„μ‹œλ“€μ„ 좜λ ₯ν•˜λ©΄ λœλ‹€!

첫 번째 풀이와 두 번째 ν’€μ΄λŠ” 방식은 μœ μ‚¬ν•˜λ‚˜ μ–΄λ–»κ²Œ 확인을 ν•˜μ˜€λŠ”κ°€μ— 따라 λ©”λͺ¨λ¦¬μ™€ μ‹œκ°„ 차이가 λ‚˜μ„œ 적게 λ˜μ—ˆλ‹€.

첫번째 풀이

μΆœλ°œμ§€ X둜 λΆ€ν„° 갈 수 μžˆλŠ” λ„μ‹œλ“€μ„ queueλ₯Ό 톡해 거리λ₯Ό λ§€κΈ°λ©΄μ„œ κ°”λ‹€.
visited λŒ€μ‹ μ— 방문이 κ°€λŠ₯ν•œ 각 λ„μ‹œμ— X둜 λΆ€ν„° 거리λ₯Ό μ±…μ •ν•˜μ—¬ 값을 λ§€κ²¨μ£Όλ©΄μ„œ ν•˜μ˜€μ„ λ•Œ.
μ •μˆ˜ν˜•μ˜ vectorλ₯Ό μ„ μ–Έν•˜μ—¬ -1둜 μ΄ˆκΈ°ν™” ν•œ ν›„ μ—…λ°μ΄νŠΈ μ‹œμΌœμ£Όμ—ˆλ‹€.

int N, M, K, X, a, b, minDist;
deque<deque<int>> town;
queue<int> q;
vector<int> dist;
vector<int> answer;

void Solve()
{
	cin >> N >> M >> K >> X;
	N++;
	town.assign(N, deque<int>());
	dist.assign(N, -1);

	for (int i = 0; i < M; i++)
	{
		cin >> a >> b;

		town[a].push_back(b);
	}

	minDist = -1;
	q.push(X);
	dist[X] = 0;

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

		for (const int& val : town[cur])
		{
			if (dist[val] != -1) continue;
			q.push(val);
			dist[val] = dist[cur] + 1;
			minDist = max(minDist, dist[val]);
		}
	}

	for (int i = 0; i < dist.size(); i++)
	{
		if (dist[i] == K) answer.push_back(i);
	}

	if (answer.size())
	{
		for (const int& val : answer)
		{
			cout << val << "\n";
		}
	}
	else cout << -1;
}

λ‹¨λ²ˆμ— 톡과 λ˜κΈ΄ν•˜μ˜€μœΌλ‚˜ λ©”λͺ¨λ¦¬μ™€ μ‹œκ°„μ„ λ„ˆλ¬΄ μž‘μ•„λ¨Ήμ—ˆλ‹€...
image

그도 그럴 것이 λ„μ‹œμ˜ 수 N의 μ΅œλŒ€ 값은 30만 κ°œμ΄λ‹€. 30만 개의 μ •μˆ˜ν˜• 벑터λ₯Ό μ„ μ–Έν•΄μ£Όμ—ˆμœΌλ‹ˆ 뢀담이 크닀.

두 번째 풀이

큐에닀가 갈 수 μžˆλŠ” λ§ˆμ„κ³Ό X둜 λΆ€ν„° 거리λ₯Ό ν•¨κ»˜ λ„£μ–΄μ„œ μ§„ν–‰ν•˜λ˜ boolν˜• visited 벑터λ₯Ό ν™œμš©ν•˜μ—¬ λ°©λ¬Έ μ—¬λΆ€λ₯Ό 체크해 μ£Όμ—ˆλ‹€.

그리고 μ‹œκ°„μ„ μ‘°κΈˆμ΄λ‚˜λ§ˆ μ€„μ΄κ³ μž νμ—μ„œ λ‹€μŒ λ§ˆμ„λ‘œ 갈 수 μžˆλŠ” 거리 distκ°€ K와 같을 경우 더 이상 νƒμƒ‰ν•˜μ§€ μ•ŠλŠ”λ‹€.
μš°λ¦¬λŠ” K만큼 κ±Έλ¦¬λŠ” λ§ˆμ„μ„ μ•Œμ•„λ‚΄κΈ° μœ„ν•œ 것이지 K거리의 λ§ˆμ„μ—μ„œ 갈 수 μžˆλŠ” λ§ˆμ„μ€ K+1둜 쑰건에 λΆ€ν•©ν•˜μ§€ μ•ŠλŠ”λ‹€.

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

using namespace std;

int N, M, K, X, A, B;
int cur, dist;
queue<pair<int, int>> q;
vector<vector<int>> towns;
vector<bool> visited;
priority_queue<int, vector<int>, greater<int>> answer;

void Solve()
{
	cin >> N >> M >> K >> X;

	N++;
	towns.assign(N, vector<int>());
	visited.assign(N, false);

	while (M--)
	{
		cin >> A >> B;
		towns[A].push_back(B);
	}

	q.push({ X, 0 });
	visited[X] = true;

	while (!q.empty())
	{
		cur = q.front().first;
		dist = q.front().second;

		q.pop();

		if (dist == K)
		{
			answer.push(cur);
			continue;
		}

		for (auto v : towns[cur])
		{
			if (visited[v]) continue;
			q.push({ v, dist + 1 });
			visited[v] = true;
		}
	}

	if (answer.empty())
	{
		cout << -1;
		return;
	}

	while (!answer.empty())
	{
		cout << answer.top() << "\n";
		answer.pop();
	}
	return;
}

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

	Solve();

	return 0;
}

κ²°κ³ΌλŠ” ?

image

λŒ€ν­ 상ν–₯된 것을 확인할 수 μžˆμ—ˆλ‹€.

ν›„κΈ°

ν”„λ‘œκ·Έλž˜λ° 경진 λŒ€νšŒλ₯Ό μ€€λΉ„ν• λ•Œ ν’€λ˜ 문제라 30λΆ„ λ‚΄λ‘œ ν’€κ³ μž 문제의 쑰건에 맞게 κ΅¬ν˜„ λ¨Όμ € μ‹œμž‘ν•˜λ‹€λ³΄λ‹ˆ μ΅œμ ν™”μ™€ 거리가 λ©€μ–΄μ§€κ²Œ λ˜μ—ˆλ‹€. κΈ‰ν•˜λ”λΌλ„ μ’€ 더 고민을 톡해 더 λ‚˜μ€ 방식을 μ°Ύμ•„λ³΄λŠ” λŠ₯λ ₯을 κΈ°λ₯΄λ©΄ μ–΄λ–¨κΉŒ μ‹Άμ—ˆλ‹€.

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

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.

λ¬΄λ‚œν•˜λ‹€λ©΄ λ¬΄λ‚œν•œ 탐색 λ¬Έμ œλ„€μš”. κΆκΈˆν•œκ±΄ visited λ₯Ό μ²΄ν¬ν•˜λŠ”λ° 거리가 k인 λ„μ‹œλ₯Ό 탐색 μ•ˆν•˜λŠ” μ΄μœ κ°€ μžˆλ‚˜μš”? 거리가 k인 μ‹œμ μ—μ„œ visited와 같은 κΈ°λŠ₯을 ν•˜λŠ”κ²Œ μ•„λ‹Œκ°€ κΆκΈˆν•©λ‹ˆλ‹€!

@InSange InSange removed the request for review from dhlee777 June 28, 2024 02:00
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.

저도 ν•΄λ‹Ή 문제λ₯Ό ν’€λ‹€κ°€ python은 μ‹œκ°„μ΄ˆκ³Όκ°€ λ‚˜κ³  pypy둜 해도 μ‹œκ°„λ³΅μž‘λ„λ‚˜ λ©”λͺ¨λ¦¬κ°€ λ„ˆλ¬΄ 많이 λ‚˜μ™”μŠ΅λ‹ˆλ‹€. μƒκ°ν•΄λ³΄λ‹ˆ λͺ¨λ“  경둜λ₯Ό νƒμƒ‰ν•˜λ©΄μ„œ μ΅œλ‹¨κ±°λ¦¬λ₯Ό ꡬ할 ν•„μš”λŠ” μ—†λ‹€λŠ” κ±Έ κΉ¨λ‹¬μ•„λ²„λ ΈμŠ΅λ‹ˆλ‹€. μ΅œλ‹¨κ±°λ¦¬ x에 ν•΄λ‹Ήν•˜λŠ” μˆœκ°„ 더이상 νƒμƒ‰ν•˜μ§€ μ•Šκ³  λ‹€μ‹œ μ‹œμž‘λ…Έλ“œμ—μ„œ λ‹€λ₯Έ 경둜λ₯Ό νƒμƒ‰ν•˜λŠ” λ˜λŠ” κ²ƒμ΄μ—ˆλ„€μš”.

@InSange
Copy link
Collaborator Author

InSange commented Jul 5, 2024

Membe

K인 λ„μ‹œμ— μΈμ ‘ν•œ λ„μ‹œλ“€μ€ K+1μž…λ‹ˆλ‹€! μš°λ¦¬λŠ” K인 거리의 λ„μ‹œλ₯Ό μ›ν•˜κΈ° λ•Œλ¬Έμ— μž¬νƒμƒ‰μ„ ν•  ν•„μš”κ°€μ—†μ–΄μ„œ continueλ₯Ό μ‹œν–‰ν•˜μ˜€μŠ΅λ‹ˆλ‹€!

@InSange InSange merged commit 8c08bdc into main Jul 10, 2024
@InSange InSange deleted the 13-InSange branch July 10, 2024 07:33
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