๐Ÿ“ Coding Test Study/Algorithm Problem

[C++][Baekjoon] 16974๋ฒˆ ์„œ์šธ ์ง€ํ•˜์ฒ  2ํ˜ธ์„ 

ibelieveinme 2021. 8. 22. 16:46
728x90

๋ฌธ์ œ

https://www.acmicpc.net/problem/16947

 

16947๋ฒˆ: ์„œ์šธ ์ง€ํ•˜์ฒ  2ํ˜ธ์„ 

์ฒซ์งธ ์ค„์— ์—ญ์˜ ๊ฐœ์ˆ˜ N(3 ≤ N ≤ 3,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์—๋Š” ์—ญ๊ณผ ์—ญ์„ ์—ฐ๊ฒฐํ•˜๋Š” ๊ตฌ๊ฐ„์˜ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๊ฐ™์€ ๊ตฌ๊ฐ„์ด ์—ฌ๋Ÿฌ ๋ฒˆ ์ฃผ์–ด์ง€๋Š” ๊ฒฝ์šฐ๋Š” ์—†๊ณ , ์—ญ์€ 1๋ฒˆ๋ถ€ํ„ฐ N๋ฒˆ๊นŒ์ง€ ๋ฒˆํ˜ธ

www.acmicpc.net

 

ํ’€์ด ์‚ฌ์ง„ ์ฐธ๊ณ 

์ฝ”๋“œ

#include <iostream>
#include <vector>
#include <queue>
#include <cstring>
using namespace std;

const int MAX = 3001;

int n;//๋ฌธ์ œ์—์„œ ์ฃผ์–ด์ง„ ์—ญ์˜ ๊ฐœ์ˆ˜
vector<int> stations[MAX];//๋ฌธ์ œ์—์„œ ์ฃผ์–ด์ง„ ์—ญ ์ •๋ณด
bool visited[MAX];//BFS/DFS ํƒ์ƒ‰์„ ์œ„ํ•œ ๋ฐฉ๋ฌธ์ฒดํฌ ๋ฐฐ์—ด
bool cycle_stations[MAX];//์ˆœํ™˜์—ญ ์—ฌ๋ถ€๋ฅผ ์ €์žฅํ•  ๋ฐฐ์—ด(true or false)

void Input();
void SearchCycle(int start_station, int current_station, int searchNum);//์‚ฌ์ดํด ์—ฌ๋ถ€ ํƒ์ƒ‰(์‹œ์ž‘์—ญ, ํƒ์ƒ‰์ค‘์ธ ํ˜„์žฌ์—ญ, ํƒ์ƒ‰๊นŠ์ด)
int CalculateDistance(int start_station);//์ˆœํ™˜์—ญ๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ ๊ตฌํ•˜๊ธฐ

int main() {
    //1. ์—ญ ์ •๋ณด ์ž…๋ ฅ๋ฐ›๊ธฐ
    Input();
    //2. ์ˆœํ™˜์—ญ ํƒ์ƒ‰
    for (int i = 1; i <= n; i++) {
        SearchCycle(i,i,0);
        memset(visited, false, sizeof(visited));
    }
    //3. ์ˆœํ™˜์—ญ๊ณผ์˜ ๊ฑฐ๋ฆฌ ํƒ์ƒ‰ ๋ฐ ์ถœ๋ ฅ
    for (int i = 1; i <= n; i++) {
         cout << CalculateDistance(i) << " ";
    }
    return 0;
}

void Input() {
    cin >> n;
    int station1, station2;
    for (int i = 0; i < n; i++) {
        cin >> station1 >> station2;
        stations[station1].push_back(station2);
        stations[station2].push_back(station1);        
    }
}

void SearchCycle(int start_station, int current_station, int searchNum) {//DFS
    visited[current_station] = true;
    for (int i = 0; i < stations[current_station].size(); i++) {
        int next_station = stations[current_station][i];
        if (!visited[next_station]) {
            SearchCycle(start_station, next_station, searchNum + 1);
        }
        else {//๋ฐฉ๋ฌธํ–ˆ๋˜ ์—ญ์ด๊ณ  ํƒ์ƒ‰ ๊นŠ์ด๊ฐ€ 2์ด์ƒ์ด๋ฉด ์ˆœํ™˜์—ญ์ด๋ผ๊ณ  ํŒ๋‹จ
            if (next_station == start_station && searchNum >= 2) {
                cycle_stations[start_station] = true;
            }
        }
        if (cycle_stations[start_station]) return;
    }
}

int CalculateDistance(int start_station) {//BFS
    queue<pair<int,int>> q;
    memset(visited, false, sizeof(visited));
    visited[start_station] = true;
    q.push(make_pair(start_station,0));

    while (!q.empty()) {
        int current_station = q.front().first;
        int current_distance = q.front().second;
        q.pop();

        if (cycle_stations[current_station]) { return current_distance; }

        for (int i = 0; i < stations[current_station].size(); i++) {
            int next_station = stations[current_station][i];
            if (!visited[next_station]) {
                visited[next_station] = true;
                q.push(make_pair(next_station, current_distance + 1));
            }
        }
    }
}
728x90