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

15-bomik0221 #60

Merged
merged 3 commits into from
Feb 24, 2024
Merged

15-bomik0221 #60

merged 3 commits into from
Feb 24, 2024

Conversation

bomik0221
Copy link
Member

@bomik0221 bomik0221 commented Feb 18, 2024

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

1766๋ฒˆ: ๋ฌธ์ œ์ง‘
๋ฌธ์ œ ์œ ํ˜• : ์œ„์ƒ์ •๋ ฌ, ๊ทธ๋ž˜ํ”„ ์ด๋ก 

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

40๋ถ„

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

๋ฌธ์ œ ์ดํ•ด

1~N๋ฒˆ๊นŒ์ง€์˜ ๋ฌธ์ œ๋ฅผ ํ’€ ์ˆœ์„œ๋ฅผ ๊ฒฐ์ •ํ•˜๋ผ. ์กฐ๊ฑด์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

1. N๊ฐœ์˜ ๋ฌธ์ œ๋Š” ๋ชจ๋‘ ํ’€์–ด์•ผ ํ•œ๋‹ค.
2. ๋จผ์ € ํ‘ธ๋Š” ๊ฒƒ์ด ์ข‹์€ ๋ฌธ์ œ๊ฐ€ ์žˆ๋Š” ๋ฌธ์ œ๋Š”, ๋จผ์ € ํ‘ธ๋Š” ๊ฒƒ์ด ์ข‹์€ ๋ฌธ์ œ๋ฅผ ๋ฐ˜๋“œ์‹œ ๋จผ์ € ํ’€์–ด์•ผ ํ•œ๋‹ค.
3. ๊ฐ€๋Šฅํ•˜๋ฉด ์‰ฌ์šด ๋ฌธ์ œ๋ถ€ํ„ฐ ํ’€์–ด์•ผ ํ•œ๋‹ค.

์ž…๋ ฅ๊ณผ ์ถœ๋ ฅ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.
image

์‚ฌ๊ณ  ๊ณผ์ •

์˜ˆ์ œ ์ž…๋ ฅ 1๊ณผ ์˜ˆ์ œ ์ถœ๋ ฅ 1์„ ๋ณด๋ฉด, 4๋ฒˆ์€ 2๋ฒˆ๋ณด๋‹ค ์•ž์—, 3๋ฒˆ์€ 1๋ฒˆ๋ณด๋‹ค ์•ž์— ์™€์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ œ๊ฐ€ ์•„๋Š” ๋Œ€๋กœ๋ผ๋ฉด 3๋ฒˆ๊ณผ 4๋ฒˆ์ด ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋†’๊ธฐ ๋•Œ๋ฌธ์— ์•ž์— ์™€์•ผ ํ•˜๋Š”๋ฐ, ๋‹ต์€ 3 1 4 2์ž…๋‹ˆ๋‹ค..! ์œ„์ƒ์ •๋ ฌ ๋ฌธ์ œ์ด๋ฉด์„œ, '๊ฐ€๋Šฅํ•˜๋ฉด ์‰ฌ์šด ๋ฌธ์ œ๋ถ€ํ„ฐ ํ’€์–ด์•ผ ํ•œ๋‹ค.'๋Š” ์กฐ๊ฑด์ด ์ฃผ์š”ํ•œ ํฌ์ธํŠธ์ธ ๋ฌธ์ œ์˜€์Šต๋‹ˆ๋‹ค.

๊ทธ๋ž˜์„œ

  • 4๋ฒˆ์˜ ์ง„์ž…์ฐจ์ˆ˜๊ฐ€ 0์ด์ง€๋งŒ
  • 1์˜ ๋‚œ์ด๋„๊ฐ€ ๋” ๋‚ฎ๊ณ 
  • ๋™์‹œ์— 4์™€ 1 ์‚ฌ์ด์— ๊ด€๊ณ„๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์—
  • 4๋ฒˆ๋ณด๋‹ค 1๋ฒˆ์ด ์•ž์— ์™€์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  • 1๋ฒˆ ์•ž์—๋Š” 3๋ฒˆ์ด ์™€์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— 3 1 ์ˆœ์„œ๋กœ ๋ฌธ์ œ๊ฐ€ ๋†“์ด๊ณ ,
  • ๊ทธ ๋‹ค์Œ์—๋Š” 2๋ฒˆ๋ณด๋‹ค ์•ž์— ์™€์•ผํ•˜๋Š” 4๋ฒˆ์ด
  • ๋งˆ์ง€๋ง‰์—๋Š” 2๋ฒˆ์ด ๋†“์ด๊ฒŒ ๋˜์–ด

๋‹ต์ด 3 1 4 2 ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

๊ทธ๋ž˜์„œ ์ด๊ฒƒ์„ ์–ด๋–ป๊ฒŒ ๊ตฌํ˜„ํ–ˆ๋ƒ๋ฉด..

์œ„์ƒ์ •๋ ฌ์„ ์‹œ์ž‘ํ•  ๋•Œ, ์ง„์ž…์ฐจ์ˆ˜๊ฐ€ 0์ธ ๋ฌธ์ œ๋ฅผ ๋ชจ๋‘ ํ์— ๋„ฃ์ง€ ์•Š๊ณ , ์ง„์ž…์ฐจ์ˆ˜๊ฐ€ 0์ธ ๋ฌธ์ œ ์ค‘์—์„œ ๊ฐ€์žฅ ์‰ฌ์šด ๋ฌธ์ œ๋ฅผ ํ•˜๋‚˜๋งŒ ํ์— ๋„ฃ์—ˆ์Šต๋‹ˆ๋‹ค. ์‰ฌ์šด ๋ฌธ์ œ์˜ ๊ธฐ์ค€์€ '์ˆ˜๊ฐ€ ์ ์„์ˆ˜๋ก ์‰ฌ์šด ๋ฌธ์ œ'๊ธฐ ๋•Œ๋ฌธ์—, ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ฉด์„œ ๊ฐ€์žฅ ๋จผ์ € ๋งŒ๋‚˜๋Š” ์ง„์ž…์ฐจ์ˆ˜๊ฐ€ 0์ธ ์ˆ˜๋ฅผ ํ์— ๋„ฃ์œผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

//์œ„์ƒ์ •๋ ฌ์˜ ์ดˆ๊ธฐ ์ƒํƒœ
std::queue<int> q;
for (int i = 1; i < N + 1; i++) {
	if (indegree[i] == 0) {
		q.push(i); //์ง„์ž…์ฐจ์ˆ˜ 0 ์ธ ๋ฌธ์ œ ์ค‘ ๊ฐ€์žฅ ์‰ฌ์šด ๋ฌธ์ œ ํ์— ๋„ฃ๊ธฐ.
		break;
		//์‰ฌ์šด ๋ฌธ์ œ๋Š” ๋จผ์ € ์ถœ๋ ฅํ•˜๋ผ๋Š” ์กฐ๊ฑด์ด ์žˆ์œผ๋ฏ€๋กœ
		//ํ์— ๋ฌธ์ œ ํ•˜๋‚˜๊ฐ€ ๋“ค์–ด์˜ค๋ฉด ๋ฐ”๋กœ ๋‹ค์Œ ๊ณผ์ •์œผ๋กœ ๋„˜์–ด๊ฐ€์•ผํ•จ.
	}
}

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

//์œ„์ƒ์ •๋ ฌ ์ง„ํ–‰
while (!q.empty()) {
	//temp์— ํ์— ๋“ค์–ด์žˆ๋Š” ์›์†Œ๋ฅผ ์ €์žฅํ•จ.
	int temp = q.front();
	//ํ์—์„œ ๋นผ๋ฉด์„œ sorted ๋ฐฐ์—ด์— ์ €์žฅํ•œ๋‹ค.
	sorted.push_back(temp);
	q.pop();
	//์ค‘๋ณต๋ฐฉ์ง€๋ฅผ ์œ„ํ•ด ํ์—์„œ ๋นผ๋‚ธ ๋ฌธ์ œ๋Š” indegree๋ฅผ -1๋กœ ์„ค์ •ํ•ด์ค€๋‹ค.

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

	//temp ๋’ค์— ์žˆ๋Š” ์• ๋“ค์˜ ์ง„์ž…์ฐจ์ˆ˜ 1์”ฉ ์ค„์—ฌ์ฃผ๊ธฐ
	for (auto next_node : graph[temp]) {
		indegree[next_node]--;
	}
	for (int i = 1; i < N + 1; i++) {
		if (indegree[i] == 0) { //์ง„์ž…์ฐจ์ˆ˜ 0 ์ธ ์•  ์ค‘์—์„œ ๊ฐ€์žฅ ์‰ฌ์šด ์• ๋ฅผ ๋˜ ๋„ฃ๊ธฐ.
			q.push(i);
			break;
		}
	}
}

๐Ÿ“š ์†Œ๊ฐ

์ƒˆ๋กœ์šด ์œ ํ˜• ํŒŒ์•…ํ•˜๋ ค๋ฉด 3๋ฌธ์ œ ์ •๋„ ํ’€์–ด๋ณด๋Š” ๊ฒŒ ์ข‹๋”๋ผ๋Š” ์กฐ์–ธ์„ ๋ฐ›์•„๋“ค์—ฌ, ์ด๋ฒˆ PR๋„ ์œ„์ƒ์ •๋ ฌ์„ ํ’€์–ด๋ณด์•˜์Šต๋‹ˆ๋‹ค. ์ฒ˜์Œ ๋ฌธ์ œ ์ฝ์—ˆ์„ ๋•Œ๋Š” ๋ญ์•ผ~ ์ง€๋‚œ ๋ฌธ์ œ์—์„œ ๋ช‡ ์ค„๋งŒ ๊ณ ์น˜๋ฉด ๋˜๊ฒ ๋„ค~ ํ–ˆ๋Š”๋ฐ ๊ทธ ๋ช‡ ์ค„์„ ์œ„ํ•ด ์—„์ฒญ ์• ๋ฅผ ๋จน์—ˆ์Šต๋‹ˆ๋‹ค......... ์‚ฌ์‹ค ๊ฐ™์€ ์œ„์ƒ์ •๋ ฌ ๋ฌธ์ œ๋ผ ํŽธํ•˜๊ฒŒ ํ’€ ์ค„ ์•Œ๊ณ  ์ ‘๊ทผํ–ˆ๋‹ค๊ฐ€ ๋‹นํ–ˆ๋„ค์š”๐Ÿ˜…
๊ตํ™ฉ๋‹˜์ด ์•Œ๋ ค์ฃผ์‹  ๋ฐฉ๋ฒ•์œผ๋กœ๋„ ํ’€์–ด๋ณด๊ณ  ์‹ถ์—ˆ๋Š”๋ฐ, ์•„์ง์€ ๋ฌด๋ฆฌ.. ๋‹ค์Œ ์œ„์ƒ์ •๋ ฌ ๋ฌธ์ œ๋Š” DFS๋ฅผ ํ™œ์šฉํ•˜์—ฌ ํ’€์–ด๋ณด๋Š” ๊ฒƒ์„ ๋ชฉํ‘œ๋กœ..!! ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

@9kyo-hwang
Copy link

9kyo-hwang commented Feb 19, 2024

์•ˆํƒ€๊น๊ฒŒ๋„... ์ด ๋ฌธ์ œ๋Š” DFS๋ฅผ ์ด์šฉํ•œ ์œ„์ƒ์ •๋ ฌ์€ ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๋‚ฎ์€ ๋ฒˆํ˜ธ๋ฅผ ์šฐ์„ ์ ์œผ๋กœ ๋ฐฉ๋ฌธํ•œ๋‹ค ๋ผ๋Š” ์กฐ๊ฑด์ด ์žˆ์–ด์„œ ๊ทธ๋ ‡์Šต๋‹ˆ๋‹ค.

6 6
6 5
6 4
5 3
4 2
3 1
2 1

์ถœ๋ ฅ : 6 5 3 4 2 1
์ •๋‹ต : 6 4 2 5 3 1

์š”๋Ÿฐ ๋ฐ˜๋ก€๊ฐ€ ์ƒ๊ธฐ๊ฑฐ๋“ ์š”.

์ด ๋ฌธ์ œ๋Š” BFS๋ฅผ ์ด์šฉํ•œ ์œ„์ƒ์ •๋ ฌ์ธ๋ฐ, ์ผ๋ฐ˜ ํ ๋Œ€์‹  ์šฐ์„ ์ˆœ์œ„ ํ(Min Heap ๊ธฐ๋ฐ˜) ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ๋‚ฎ์€ ๋ฒˆํ˜ธ์˜ ๋…ธ๋“œ๊ฐ€ ์šฐ์„ ์ ์œผ๋กœ ๋‚˜ํƒ€๋‚˜๊ฒŒ ๋˜๊ฒ ์ฃ ?

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

using namespace std;

int main()
{
    cin.tie(nullptr)->sync_with_stdio(false);
    
    int N, M;
    cin >> N >> M;
    
    vector<vector<int>> AdjacencyList(N + 1);
    vector<int> InDegrees(N + 1, 0);
    
    while(M--)
    {
        int A, B;
        cin >> A >> B;
        
        AdjacencyList[A].emplace_back(B);
        InDegrees[B] += 1;
    }
    
    priority_queue<int, vector<int>, greater<>> PQ;
    for(int Node = 1; Node <= N; ++Node)
    {
        if(InDegrees[Node] == 0)
        {
            PQ.emplace(Node);
        }
    }
    
    vector<int> Answer;
    while(N--)
    {
        if(PQ.empty())
        {
            break;
        }
        
        int SrcNode = PQ.top();
        PQ.pop();
        Answer.emplace_back(SrcNode);
        
        for(const int& DstNode : AdjacencyList[SrcNode])
        {
            if(--InDegrees[DstNode] == 0)
            {
                PQ.emplace(DstNode);
            }
        }
    }
    
    for(const int& Node : Answer)
    {
        cout << Node << " ";
    }

    return 0;
}

Copy link
Member

@tgyuuAn tgyuuAn left a comment

Choose a reason for hiding this comment

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

์ด ๋ฌธ์ œ ์ •๋ง ์šฐ์„  ์ˆœ์œ„ ํ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ์•„์ด๋””์–ด๋ฅผ ์ฐพ๊ธฐ๊ฐ€ ํž˜๋“ ๋ฐ..

ํ˜น์‹œ ์–ด๋–ป๊ฒŒ ์ฐพ๊ฒŒ ๋˜์…จ๋‚˜์š”..?

๊ทธ๋ƒฅ ํ•˜๋‹ค๋ณด๋‹ˆ๊นŒ ์•Œ๊ฒŒ ๋œ...?

@bomik0221
Copy link
Member Author

์ด ๋ฌธ์ œ ์ •๋ง ์šฐ์„  ์ˆœ์œ„ ํ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ์•„์ด๋””์–ด๋ฅผ ์ฐพ๊ธฐ๊ฐ€ ํž˜๋“ ๋ฐ..

ํ˜น์‹œ ์–ด๋–ป๊ฒŒ ์ฐพ๊ฒŒ ๋˜์…จ๋‚˜์š”..?

๊ทธ๋ƒฅ ํ•˜๋‹ค๋ณด๋‹ˆ๊นŒ ์•Œ๊ฒŒ ๋œ...?

์ €๋Š” ๊ตํ™ฉ๋‹˜ ์ฝ”๋ฉ˜ํŠธ ๋•์— ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ์•Œ๊ฒŒ ๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์—..ใ…Žใ…Ž ์šฐ์„ ์ˆœ์œ„ ํ๋กœ ๋ฌธ์ œ๋ฅผ ํ‘ผ ๊ฑด ์•„๋‹ˆ๊ตฌ์š”, ๊ทธ๋ƒฅ '์‰ฌ์šด ๋ฌธ์ œ๋ถ€ํ„ฐ' ๋ผ๋Š” ๋‹จ์„œ์— ์ง‘์ค‘ํ•˜๋‹ค๋ณด๋‹ˆ ํ’€๋ ธ์Šต๋‹ˆ๋‹น

Copy link
Collaborator

@2secondag 2secondag 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
Member

@miniron-v miniron-v left a comment

Choose a reason for hiding this comment

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

๋กœ์ง์ด ๊น”๋”ํ•ด์„œ ์ดํ•ดํ•˜๊ธฐ ํŽธํ–ˆ์Šต๋‹ˆ๋‹ค! ์ €๋„ ์šฐ์„ ์ˆœ์œ„ ํ๋Š” ๊ตํ™ฉ๋‹˜ ์ฝ”๋ฉ˜ํŠธ ๋ณด๊ณ  ์ƒ๊ฐํ–ˆ๋Š”๋ฐ, ๋ณธ๋ฌธ์€ ์ข€ ๋” ๊ทธ๋ฆฌ๋””ํ•œ ๋ฐฉ๋ฒ• ๊ฐ™๋„ค์š”!

@bomik0221 bomik0221 merged commit 51ab301 into main Feb 24, 2024
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