์ƒ์„ธ ์ปจํ…์ธ 

๋ณธ๋ฌธ ์ œ๋ชฉ

[๋ฐฑ์ค€(BOJ)] 9466๋ฒˆ: ํ…€ ํ”„๋กœ์ ํŠธ (DFS/BFS, C++)

PROGRAMMING/Algorithm

by koharin 2021. 2. 26. 19:34

๋ณธ๋ฌธ

728x90
๋ฐ˜์‘ํ˜•

๐Ÿ“ ๋ฌธ์ œํ’€์ด ๊ณผ์ •


visited: ๋ฐฉ๋ฌธ์—ฌ๋ถ€ ์ฒดํฌ

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

1(current)์ด 3(next)์„ ์ง€๋ชฉํ–ˆ์ง€๋งŒ, 3(current)์—์„œ 3(next)์„ ์ง€๋ชฉํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— 3 ํ˜ผ์ž์„œ๋งŒ ํŒ€์ด๊ณ  1์€ ํŒ€์ด ๋˜์ง€ ๋ชปํ•œ๋‹ค.

 

๋ฐ˜๋ก€

6
2 3 4 5 6 2
output : 1

5
2 5 4 5 2
output : 3

6
1 3 4 3 2 6
output : 2

13
2 4 5 2 4 1 8 9 10 11 9 10 10
output : 8

10
2 5 7 1 6 8 8 3 5 10
output : 6

10
2 7 10 5 3 3 9 10 6 3
output : 8

6
2 1 1 2 6 3
output : 4
#include <stdio.h>
#include <deque>
#include <queue>
using namespace std;

void DFS(int start, deque<int> *graph, bool *visited, bool *done, int *count){
    visited[start] = true;
    printf("[+] %d visited\n", start);
    int next = graph[start][0];
    if(visited[next] == false){
        DFS(next, graph, visited, done, count);
    }
    // count that included in project
    else if(!done[next]){
        for(int i=next; i !=start; i=graph[i][0]) { (*count)++; printf("%d counted.\n", i); } 
        (*count)++; // for counting start
        printf("%d counted.\n", start);
    }

    done[start] = true;
}

int main(){
    int T; // T: # of testcase
    scanf("%d", &T);

    for(int i=0; i<T; i++){
        int n;
        int count=0; // n: # of students
        scanf("%d", &n);
        deque<int> graph[n+1];
        bool visited[n+1];
        bool done[n+1];
        fill(visited, visited+n+1, false);
        fill(done, done+n+1, false);

        for(int j=1; j<=n; j++){
            int num; 
            scanf("%d", &num);
            graph[j].push_back(num);
        }
        
        for(int j=1; j<=n; j++){
            if(visited[j] == false) DFS(j, graph, visited, done, &count);
        }
        
        printf("%d\n", n-count);
    }
}

๊ฐ graph[i]๋Š” ํ•˜๋‚˜์”ฉ๋งŒ ์›์†Œ๋ฅผ ๊ฐ€์ง€๋ฏ€๋กœ, start์—์„œ next๋Š” ํ•ญ์ƒ graph[start][0]์ด ๋œ๋‹ค.

์ด๊ฒƒ์œผ๋กœ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ์ค„์—ฌ์„œ ์‹œ๊ฐ„์ดˆ๊ณผ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•œ๋‹ค.

๋˜ํ•œ visited์ผ ๋•Œ, ๋ฐฉ๋ฌธ์ด ๋๋‚˜์ง€ ์•Š์•˜์œผ๋ฉด ํ•ด๋‹น visited๋ถ€ํ„ฐ graph[i][0]์— ๋Œ€ํ•ด count++์„ ํ•ด์„œ ํ”„๋กœ์ ํŠธ์— ์†ํ•œ ๊ฒƒ์„ ์ฒดํฌํ•˜๊ณ , start์™€ ๊ฐ™๋‹ค๋ฉด ๋๋‚ธ๋‹ค. ๋ฐ˜๋ณต๋ฌธ์ด ๋๋‚˜๋ฉด start์— ๋Œ€ํ•œ count๋„ ํ•œ๋‹ค.

๋งŒ์•ฝ start = 6์ด๊ณ  ๋‹ค์‹œ next = 4๋ผ๋ฉด ์ด๋ฏธ visited์ด๋ฏ€๋กœ ๋ฐ˜๋ณต๋ฌธ์„ ์‹œ์ž‘ํ•ด์„œ 4๋ถ€ํ„ฐ ์นด์šดํŒ…์„ ํ•˜๊ณ ,

i=4, i=graph[4]=7, i=graph[7]=6์œผ๋กœ ๋‹ค์‹œ start๋กœ ๋Œ์•„์˜ค๋ฉด ๋๋‚ธ๋‹ค. ์ด๋•Œ 6์— ๋Œ€ํ•œ ์นด์šดํŒ…๋„ ํ•ด์•ผ ํ•˜๋ฏ€๋กœ ๋ฐ˜๋ณต๋ฌธ ๋๋‚˜๊ณ  ํ•œ๋ฒˆ ๋” ์นด์šดํŒ…์„ ํ•ด์ค€๋‹ค.

๊ทธ๋ฆฌ๊ณ  start์— ๋Œ€ํ•ด done[start]=true๋กœ ๋ฐฉ๋ฌธ์ด ๋๋‚˜๊ณ  ๋”์ด์ƒ ๋ฐฉ๋ฌธํ•  ์ผ์ด ์—†์Œ์„ ํ‘œ์‹œํ•œ๋‹ค.

 

 

 

๐Ÿ’ป C++ ์ œ์ถœ์ฝ”๋“œ


#include <bits/stdc++.h>
using namespace std;

void DFS(int start, int *graph, bool *visited, bool *done, int *count){
    visited[start] = true;
    int next = graph[start];
    if(visited[next] == false) DFS(next, graph, visited, done, count);
    // count that included in project
    else if(!done[next]){
        for(int i=next; i !=start; i=graph[i])(*count)++; 
        (*count)++; // for counting start
    }
    done[start] = true;
}

int main(){
    int T; // T: # of testcase
    scanf("%d", &T);

    for(int i=0; i<T; i++){
        int n;
        int count=0; // n: # of students
        scanf("%d", &n);
        int graph[n+1]={0,};
        bool visited[n+1];
        bool done[n+1];
        fill(visited, visited+n+1, false);
        fill(done, done+n+1, false);

        for(int j=1; j<=n; j++) scanf("%d", &graph[j]);
        
        for(int j=1; j<=n; j++){
            if(visited[j] == false) DFS(j, graph, visited, done, &count);
        }
        printf("%d\n", n-count);
    }
}

๋ฌธ์ œํ’€์ด ๊ณผ์ •์—์„œ๋Š” STL๋กœ ๊ตฌํ˜„์„ ํ–ˆ์—ˆ๋Š”๋ฐ, ์‹œ๊ฐ„์ดˆ๊ณผ ๋ฌธ์ œ๊ฐ€ ํ•ด๊ฒฐ๋˜์ง€ ์•Š์•„์„œ ๊ทธ๋ƒฅ deque ๊ฐ™์€ STL ์•ˆ ์“ฐ๊ณ  ๋ฐฐ์—ด๋กœ graph ๋งŒ๋“ค๊ณ  ์ž…๋ ฅ๋ฐ›์œผ๋‹ˆ๊นŒ ์‹œ๊ฐ„์ดˆ๊ณผ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ณ  ๋งž์•˜์Šต๋‹ˆ๋‹ค๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ์—ˆ๋‹ค.

n-count๋Š” count๊ฐ€ ํ”„๋กœ์ ํŠธ์— ์†ํ•œ ์‚ฌ๋žŒ ์ˆ˜์ด๋ฏ€๋กœ n-count๋กœ ํ”„๋กœ์ ํŠธ์— ์†ํ•˜์ง€ ์•Š์€ ์ˆ˜๋กœ ๋‹ต์„ ๊ตฌํ•œ๋‹ค.

 

 

๐Ÿ‘ ๊ฒฐ๊ณผ


728x90
๋ฐ˜์‘ํ˜•

๊ด€๋ จ๊ธ€ ๋”๋ณด๊ธฐ