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

11-InSange #41

Merged
merged 3 commits into from
May 20, 2024
Merged

11-InSange #41

merged 3 commits into from
May 20, 2024

Conversation

InSange
Copy link
Collaborator

@InSange InSange commented May 3, 2024

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

์ˆซ์ž ๊ณ ๋ฅด๊ธฐ

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

ํ•˜๋ฃจ

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

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

๋ฌธ์ œ๋ฅผ ๋ณด์•˜์„ ๋•Œ ๋ฐ”๋กœ ์ฝ”๋“œ๊ฐ€ ๋ฒˆ์ฉ์˜€๊ณ  10๋ถ„๋งŒ์— ํ’€์—ˆ๋‹ค.
์ฒซ์งธ ์ค„๊ณผ ๋‘๋ฒˆ์งธ ์ค„์„ ๋น„๊ตํ–ˆ์„ ๋•Œ ์„œ๋กœ ๋ฐฉ๋ฌธํ–ˆ๋˜ ์ ์ด ์žˆ๋Š” ์ˆซ์ž๋ฉด๋˜๊ณ  ๊ฐ™์€ ์Œ์ด๋ฉด ๋œ๋‹ค?
๊ทธ๋Ÿฌ๋ฉด i๊ฐ€ ์ˆœ์„œ๋Œ€๋กœ ์˜ฌ๋ผ๊ฐˆ ๋•Œ ์ฒซ๋ฒˆ์งธ ์ค„๊ณผ ๋‘๋ฒˆ์งธ ์ค„์— ๊ฐ์ž ํ•ด๋‹นํ•˜๋Š” index๋ฒˆํ˜ธ๋ณ„๋กœ ๋ฐฉ๋ฌธํ•ด์คฌ๋‹ค๋Š” ๊ฒƒ์„ ํ‘œ์‹œํ•ด์ฃผ๋ฉด ๋˜์ง€ ์•Š์„๊นŒ?

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int N, num;
vector<bool> v1;
vector<bool> v2;
vector<int> answer;

void Solve()
{
	cin >> N;

	N += 1;
	v1.assign(N, false);
	v2.assign(N, false);

	for (int i = 1; i < N; i++)
	{
		cin >> num;
		v1[i] = true;
		v2[num] = true;

		if ((v1[i] == v2[i]) && (v2[num] == v1[num]))
		{
			answer.push_back(i);
			answer.push_back(num);
		}
	}

	sort(answer.begin(), answer.end());
	answer.erase(unique(answer.begin(), answer.end()), answer.end());

	int asize = answer.size();
	cout << asize << "\n";

	for (int i = 0; i < asize; i++)
	{
		cout << answer[i] << "\n";
	}
}

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

	Solve();

	return 0;
}

๋”ฑ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋ฅผ ๋„ฃ์ž ํ†ต๊ณผ๋˜์—ˆ๊ณ  ๋ฌธ์ œ ๋‚œ์ด๋„๋„ ๊ณจ๋“œ5๊ธธ๋ž˜ ๊ทธ๋Ÿฌ๋ ค๋‹ˆ ํ–ˆ๋”๋‹ˆ ๋™‡!!
image
ํ‹€๋ ธ์Šต๋‹ˆ๋‹ค..

์งˆ๋ฌธ๊ฒŒ์‹œํŒ์— ์žˆ๋Š” ์—ฌ๋Ÿฌ ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๋ฅผ ๊ฑฐ์น˜๊ณ  ๋ฌธ์ œ๋ฅผ ๋‹ค์‹œ ์ฝ์–ด๋ณด๋‹ˆ ๋‚ด๊ฐ€ ๋„ˆ๋ฌด ๋‹จ์ˆœํ•˜๊ฒŒ ์ƒ๊ฐํ–ˆ์—ˆ๋‹ค..

์ฒซ๋ฒˆ์งธ ์ค„๊ณผ ๋‘๋ฒˆ์งธ ์ค„์—์„œ ์ด๋ฃจ๋Š” ์ง‘ํ•ฉ์˜ ์›์†Œ๋Š” ์„œ๋กœ ๋ฐฉ๋ฌธ์„ ํ–ˆ์„ ๋•Œ ๊ทธ ๋ฒˆํ˜ธ๊นŒ์ง€ ๋ฐฉ๋ฌธ์„ ํ–ˆ๋‹ค๋Š” ์ 
ํ•ด๋‹น ๋ถ€๋ถ„์„ ์ฒดํฌํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ฒซ๋ฒˆ์งธ ์ค„์˜ ์š”์†Œ ์ธ๋ฑ์Šค์—์„œ ๋‘๋ฒˆ์งธ ์ค„์„ ๋ฐฉ๋ฌธํ–ˆ์„ ๋•Œ ๊ฒฐ๊ตญ ์ฒซ๋ฒˆ์งธ ์ค„ ์š”์†Œ๋Š” ๋‹ค์‹œ ์ž๊ธฐ ์ž์‹ ์œผ๋กœ ๋Œ์•„์™€์•ผ ๋œ๋‹ค๋Š” ์ ..!

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

๊ทธ๋Ÿผ ๊ฒฐ๊ตญ ๋‚ด๊ฐ€ ๋ฐฉ๋ฌธํ–ˆ๋˜ ๊ธธ์„ ์•Œ์•„์•ผ ๋˜๋Š” ๊ฑด๋ฐ...
์ด ๋ถ€๋ถ„์€ DFS๋กœ ๋ชจ๋“  ๊ฒฝ๋กœ๋ฅผ ํƒ์ƒ‰ํ•œ๋‹ค๊ณ  ์น˜์ž.
๊ทธ๋Ÿผ ๋ฌดํ•œ ๋ฃจํ”„ 4->1 1->4๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์œผ๋‹ˆ visited๋กœ ๋ฐฉ๋ฌธํ–ˆ๋˜ ๊ฒฝ๋กœ๋“ค์„ ๋‹ค ์ฒดํฌํ•ด์ฃผ๋ฉด ๋œ๋‹ค.
์—ฌ๊ธฐ์„œ ๋˜ ๊นŠ๊ฒŒ ์ƒ๊ฐํ–ˆ๋˜ ๋ถ€๋ถ„์ด
์ˆœ์ฐจ์ ์œผ๋กœ i๋ฅผ ์ฆ๊ฐ€ํ•ด์„œ ์ฒซ๋ฒˆ์งธ ์ค„๊ณผ ๋‘๋ฒˆ์งธ ์ค„์„ ๋ฐฉ๋ฌธํ•œ๋‹คํ•˜๋ฉด ์ด์ „์˜ ๊ธฐ๋ก๋“ค์„ ๊ตณ์ด false๋กœ ํ•ด์ œํ•  ํ•„์š”๊ฐ€ ์žˆ์„๊นŒ? ์˜€๋‹ค.

์ด ๋ถ€๋ถ„์— ๋Œ€ํ•ด์„œ ๋ชจ๋“  ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๋ฅผ ๊ธ์–ด๋ณด๊ธฐ๋„ ํ–ˆ๊ณ  ์ƒ๊ฐ๋„ ํ•ด๋ดค์ง€๋งŒ ์•„์‰ฝ๊ฒŒ๋„ ๋ฐœ๊ฒฌํ•˜์ง€๋Š” ๋ชปํ–ˆ๋‹ค.
๊ทธ๋ ‡๊ฒŒ ์‹œ๋„ ํ–ˆ์„ ๋•Œ๋Š” ์—ญ์‹œ
image

๊ทธ๋Ÿผ ๊ฒฐ๊ตญ ๋ฐฉ๋ฌธํ–ˆ๋˜ ๊ฒƒ๋“ค์„ ๋‹ค์‹œ false๋กœ ๋ฐ”๊ฟ”์ค˜์•ผํ•˜์˜€๊ณ  ๊ทธ๋ ‡๊ฒŒ ํ•ด์„œ ๊ฒจ์šฐ ํ†ต๊ณผํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค..!

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int N, num;
vector<bool> visited;
vector<int> v;
vector<int> answer;

void DFS(int cur, int start)
{
	if (visited[cur] == true)
	{
		if (start == cur) answer.push_back(start);
		return;
	}

	visited[cur] = true;
	DFS(v[cur], start);
	visited[cur] = false;
}

void Solve()
{
	cin >> N;

	N += 1;
	visited.assign(N, false);
	v.assign(N, 0);

	for (int i = 1; i < N; i++)
	{
		cin >> v[i];
	}

	for (int i = 1; i < N; i++)
	{
		DFS(i, i);
	}

	int asize = answer.size();
	cout << asize << "\n";

	for (int i = 0; i < asize; i++)
	{
		cout << answer[i] << "\n";
	}
}

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

	Solve();

	return 0;
}

###ํ›„๊ธฐ
์ฒซ๋ฒˆ์งธ ํ’€์ด๋งŒ ๊ณ ์ง‘ํ•˜๋‹ค๊ฐ€ 3์‹œ๊ฐ„์ด ํ๋ฅด๊ฒŒ ๋˜์—ˆ๊ณ 
ํ•ด๋‹น ํ’€์ด ๋ฐฉ์‹์œผ๋กœ ์ƒ๊ฐ์„ ํ–ˆ์„ ๋•Œ ๋ฐ”๋กœ DFS๊ฐ€ ๋– ์˜ค๋ฅด์ง€๋Š” ์•Š์•˜๋‹ค.
๋ธ”๋กœ๊ทธ ๊ธ€์˜ ๋„์›€์œผ๋กœ ์•„ DFS๋ฌธ์ œ๋ผ๋Š” ๊ฒƒ์„ ๊นจ๋‹ซ๊ฒŒ ๋˜์—ˆ๊ณ  ๋ฐ”๋กœ DFS๋ฅผ ์ ‘๋ชฉํ•ด์„œ ์ดํ•ด๋ฅผ ํ•˜๋Š” ์‹์œผ๋กœ ์ ‘๊ทผํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค.

์˜ค๋ž˜ ๊ฑธ๋ฆด๋งŒํ•œ ๋ฌธ์ œ๋Š” ์•„๋‹ˆ์˜€์ง€๋งŒ ๋ฌธ์ œ๋ฅผ ์ดํ•ดํ•˜๋Š” ๋ถ€๋ถ„์—์„œ ์˜ค์ ์ด ๋งŽ์•˜๋‹ค๋Š” ๊ฒƒ์„ ๋ฐœ๊ฒฌํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค..

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

์ฐธ๊ณ  ๋ธ”๋กœ๊ทธ

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.

์ฝ”๋“œ ๊น”๋”ํ•˜๊ฒŒ ์ž‘์„ฑํ•˜๋Š”๊ฑด ํ•ญ์ƒ ๋ณผ ๋•Œ๋งˆ๋‹ค ์‹ ๊ธฐํ•˜๋„ค์š”. ๋ฐฐ์›Œ๊ฐˆ ์ ์ด ๋งŽ์€ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค
DFS๋ฉด ์žฌ๊ท€๋กœ ๊ตฌํ˜„์ด ๋จผ์ € ๋– ์˜ค๋ฅด๊ธด ํ•˜์ง€๋งŒ ์กฐ๊ฑด์„ ์ž˜ ์žก์œผ๋ฉด ๋‹จ์ˆœ while ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค! ์ทจํ–ฅ ์ฐจ์ด์ผ๊นŒ์š”?

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

using namespace std;

typedef pair<int, int> ii;

int n, arr[102];
bool visited[102] = {0, };
bool tmp[102] = {0, };

int main() {
    cin >> n;
    for (int i = 1; i <= n; i++) 
        cin >> arr[i];

    priority_queue<int, vector<int>, greater<int> > q;

    for (int i = 1; i <= n; i++) {
        if (visited[i]) continue;

        for (int i = 1; i <= n; i++) tmp[i] = 0;

        int t = arr[i];

        vector<int> v;
        v.push_back(i);

        while (true) {
            if (t == i) {
                for (int p : v) {
                    visited[p] = 1;
                    q.push(p);
                }
                break;
            }

            if (visited[t] || tmp[t]) {
                break;
            }

            tmp[t] = true;
            v.push_back(t);
            t = arr[t];
        }
    }

    printf("%d\n", q.size());
    while (!q.empty()) {
        printf("%d\n", q.top());
        q.pop();
    }
}

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.

๋ฐฉ๋ฌธ์—ฌ๋ถ€๋ฅผ ๋‹ค์‹œ false๋กœ ์ดˆ๊ธฐํ™”ํ•˜๊ณ  dfs๋ฅผ ์ง„ํ–‰ํ•ด์•ผ ์‚ฌ์ดํด์ด ์กด์žฌํ•˜๋Š”์ง€ ์•Œ ์ˆ˜ ์žˆ๋Š”๊ฑฐ๋„ค์š”. ์ €๋Š” dfs ํ•จ์ˆ˜๋ฅผ ๋”ฐ๋กœ ๋งŒ๋“ค์–ด ์žฌ๊ท€์ ์œผ๋กœ ํ‘ธ๋Š”๊ฒŒ ์ข€ ์–ด๋ ค์›Œ์„œ while์— ๋„ฃ๊ณ  ์Šคํƒ์ด ๋น„์›Œ์งˆ ๋•Œ ๊นŒ์ง€ ๋ฐ˜๋ณตํ–ˆ์–ด์š”.

for i in range(1, n+1):
    visited = [0] * (n+1)
    stack = [i]
    visited[i] = 1

    while stack:
        v = stack.pop()
        for j in graph[v]:
            if not visited[j]:
                stack.append(j)
                visited[j] = 1
            elif visited[j] and i == j: 
                result.add(j)

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.

๋‹ค๋ฅธ ๊ฒฝ์šฐ์˜ ์ˆ˜๋“ค์„ ์œ„ํ•ด ํƒ์ƒ‰ํ›„ visited=false ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ์–ด์•ผ ํ•˜๋Š”๊ตฐ์š” visited.assign(N, false); v.assign(N, 0); ์ด๋Ÿฐ๋ฐฉ์‹์œผ๋กœ ๋ฒกํ„ฐ๋ฅผ์ดˆ๊ธฐํ™” ํ•˜๋Š”๋ฒ•๋„ ์ž˜ ๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์ €๋„ ์ด๋Ÿฐ ๊น”๋”ํ•œ ์ฝ”๋“œ๋ฅผ ์งœ๋Š”๋‚ ๊นŒ์ง€ ๋…ธ๋ ฅํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค..

@InSange
Copy link
Collaborator Author

InSange commented May 20, 2024

์ฝ”๋“œ ๊น”๋”ํ•˜๊ฒŒ ์ž‘์„ฑํ•˜๋Š”๊ฑด ํ•ญ์ƒ ๋ณผ ๋•Œ๋งˆ๋‹ค ์‹ ๊ธฐํ•˜๋„ค์š”. ๋ฐฐ์›Œ๊ฐˆ ์ ์ด ๋งŽ์€ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค DFS๋ฉด ์žฌ๊ท€๋กœ ๊ตฌํ˜„์ด ๋จผ์ € ๋– ์˜ค๋ฅด๊ธด ํ•˜์ง€๋งŒ ์กฐ๊ฑด์„ ์ž˜ ์žก์œผ๋ฉด ๋‹จ์ˆœ while ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค! ์ทจํ–ฅ ์ฐจ์ด์ผ๊นŒ์š”?

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

using namespace std;

typedef pair<int, int> ii;

int n, arr[102];
bool visited[102] = {0, };
bool tmp[102] = {0, };

int main() {
    cin >> n;
    for (int i = 1; i <= n; i++) 
        cin >> arr[i];

    priority_queue<int, vector<int>, greater<int> > q;

    for (int i = 1; i <= n; i++) {
        if (visited[i]) continue;

        for (int i = 1; i <= n; i++) tmp[i] = 0;

        int t = arr[i];

        vector<int> v;
        v.push_back(i);

        while (true) {
            if (t == i) {
                for (int p : v) {
                    visited[p] = 1;
                    q.push(p);
                }
                break;
            }

            if (visited[t] || tmp[t]) {
                break;
            }

            tmp[t] = true;
            v.push_back(t);
            t = arr[t];
        }
    }

    printf("%d\n", q.size());
    while (!q.empty()) {
        printf("%d\n", q.top());
        q.pop();
    }
}

๋ฌธ์ œ๋ฅผ ์ดํ•ดํ•˜๋Š” ๋ถ€๋ถ„์— ๋Œ€ํ•ด์„œ ๊ทธ๋Ÿฐ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!! ๋ฆฌ๋ทฐ์— ์จ์ ธ์žˆ๋“ฏ ์ €๋Š” ๋ฌธ์ œ ๋ฐฉํ–ฅ์„ฑ ์ž์ฒด๊ฐ€ ์ •๋‹ต์„ ์œ ์ถ”ํ•˜๋Š” ๊ฒƒ๊ณผ๋Š” ๊ฑฐ๋ฆฌ๊ฐ€ ๋ฉ€์–ด์„œ.. ์ฒ˜์Œ์— dfs๋กœ ๊ฐ€๋Šฅํ•œ ๊ฒฝ๋กœ์˜ ์œ„์น˜๋“ค์„ ํŒŒ์•…ํ•˜๊ณ  ์ง„ํ–‰์„ ํ•˜๋‹ค๋ณด๋‹ˆ while๋ฌธ์— ๋Œ€ํ•ด์„œ ๊ณ ๋ ค๋Š” ํ•ด๋ณด์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค.

while๋ฌธ์—์„œ ์ œ์•ฝ ์กฐ๊ฑด๋“ค์„ ์ถ”๊ฐ€ํ•˜์—ฌ ํ’€์ดํ•˜๋Š” ๋ฐฉ๋ฒ• ์ž˜ ๋ดค์Šต๋‹ˆ๋‹ค! ํ•˜์ง€๋งŒ ์ €์—๊ฒŒ๋Š” ์ดํ•ดํ•˜๋Š” ์‹œ๊ฐ„์„ ๋น„๋กฏํ•ด ์กฐ๊ธˆ ์–ด๋ ต๋”๋ผ๊ตฌ์š”... ๋”๊ตฐ๋‹ค๋‚˜ dfs์— ๋ฌผ๋“ค์–ด์ ธ ์žˆ๋‹ค๋ณด๋‹ˆ ์‚ฌ๊ณ ์ „ํ™˜์ด ํ•„์š”ํ•  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

@InSange
Copy link
Collaborator Author

InSange commented May 20, 2024

๋ฐฉ๋ฌธ์—ฌ๋ถ€๋ฅผ ๋‹ค์‹œ false๋กœ ์ดˆ๊ธฐํ™”ํ•˜๊ณ  dfs๋ฅผ ์ง„ํ–‰ํ•ด์•ผ ์‚ฌ์ดํด์ด ์กด์žฌํ•˜๋Š”์ง€ ์•Œ ์ˆ˜ ์žˆ๋Š”๊ฑฐ๋„ค์š”. ์ €๋Š” dfs ํ•จ์ˆ˜๋ฅผ ๋”ฐ๋กœ ๋งŒ๋“ค์–ด ์žฌ๊ท€์ ์œผ๋กœ ํ‘ธ๋Š”๊ฒŒ ์ข€ ์–ด๋ ค์›Œ์„œ while์— ๋„ฃ๊ณ  ์Šคํƒ์ด ๋น„์›Œ์งˆ ๋•Œ ๊นŒ์ง€ ๋ฐ˜๋ณตํ–ˆ์–ด์š”.

for i in range(1, n+1):
    visited = [0] * (n+1)
    stack = [i]
    visited[i] = 1

    while stack:
        v = stack.pop()
        for j in graph[v]:
            if not visited[j]:
                stack.append(j)
                visited[j] = 1
            elif visited[j] and i == j: 
                result.add(j)

๋งค๋ฒˆ ์ดˆ๊ธฐํ™” ํ•œ๋‹ค๋Š” ๊ฒƒ์€ ๋น„์šฉ์ด ๋งŽ์ด ๋“ค ๊ฒƒ ๊ฐ™์€๋ฐ.. ์ด์— ๋”ฐ๋ฅธ ๋น„๊ต๊ฐ™์€ ๊ฒƒ๋„ ์žˆ์œผ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์•„์š”!!

@InSange
Copy link
Collaborator Author

InSange commented May 20, 2024

11-InSange

@InSange InSange merged commit 81387c2 into main May 20, 2024
@InSange InSange deleted the 11-InSange branch May 20, 2024 03:17
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