[백준] 1389번 케빈 베이컨의 6단계 법칙

2025. 6. 24. 12:33·백준 문제/그래프

문제: 1389번: 케빈 베이컨의 6단계 법칙

basic-algo-lecture/workbook/0x18.md at master · encrypted-def/basic-algo-lecture

 

basic-algo-lecture/workbook/0x18.md at master · encrypted-def/basic-algo-lecture

바킹독의 실전 알고리즘 강의 자료. Contribute to encrypted-def/basic-algo-lecture development by creating an account on GitHub.

github.com

 

//#include <bits/stdc++.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
#include <string>
#include <queue>
#include <set>
#include <iomanip>
#include <string.h>
#include <sstream>
#include <tuple>
#include <regex>
#include <stack>
#include <map>

using namespace std;

vector<int> toNodes;
vector<vector<int>> friends;

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

    int n, m; // 유저 수, 친구 관계 수
    cin >> n >> m;

    friends = vector<vector<int>>(n + 1); // 0번 인덱스 안 써
    toNodes = vector<int>(n + 1);
    for (int i = 0; i < m; i++) {
        int a, b;
        cin >> a >> b;

        friends[a].push_back(b);
        friends[b].push_back(a);
    }

    int totalTmp = INT_MAX;
    int answer = -1;
    for (int i = 1; i <= n; i++) {
        // 노드 하나마다 케빈 베이컨 수 구하기
        int nodeKebinNum = 0;

        queue<pair<int, int>> nexts;
        fill(toNodes.begin(), toNodes.end(), 0);
        
        nexts.push({ i, 1 });
        toNodes[i] = 1; 
        
        while (!nexts.empty()) {
            pair<int, int> cur = nexts.front(); nexts.pop();

            for (int next : friends[cur.first]) {
                if (toNodes[next] != 0) continue;

                nexts.push({ next, cur.second + 1 });
                toNodes[next] = cur.second + 1;
            }
        }

        for (int j = 1; j <= n; j++) {
            if (i == j) continue;

            nodeKebinNum += toNodes[j];
        }

        if (totalTmp > nodeKebinNum) {
            totalTmp = nodeKebinNum;
            answer = i;
        }
    }
    cout << answer << "\n";

    return 0;
}

 

 

처음 풀었을 때는 매 for 문 마다 toNodes = vector<int>(n+1) 로 해줬더니 메모리 초과 에러가 떴다. 그래서 이 문제를 fill 함수를 이용해서 해결했다.

'백준 문제/그래프' 카테고리의 다른 글
  • [백준] 1325번 효율적인 해킹
  • [백준] 2660번 회장뽑기
  • [백준] 24446번 알고리즘 수업 - 너비 우선 탐색 3
  • [백준] 1707번 이분 그래프
dubu0721
dubu0721
dubu0721 님의 블로그 입니다.
  • dubu0721
    dubu0721 님의 블로그
    dubu0721
  • 전체
    오늘
    어제
    • 분류 전체보기 (346) N
      • 프로그래밍언어론 정리 (5)
      • 컴퓨터네트워크 정리 (5)
      • 알고리즘&자료구조 공부 (64)
        • it 취업을 위한 알고리즘 문제풀이 입문 강의 (60)
        • 학교 알고리즘 수업 (3)
        • 실전프로젝트I (0)
      • 백준 문제 (199) N
        • 이분탐색 (7)
        • 투포인트 (10)
        • 그래프 (10) N
        • 그리디 (24)
        • DP (25)
        • BFS (18)
        • MST (7)
        • KMP (4)
        • Dijkstra (3)
        • Disjoints Set (4)
        • Bellman-Ford (2)
        • 시뮬레이션 (3)
        • 백트래킹 (15)
        • 위상정렬 (5)
        • 자료구조 (25)
        • 기하학 (1)
        • 정렬 (11)
        • 구현 (8)
        • 재귀 (8)
        • 수학 (8)
      • 유니티 공부 (11)
        • 레트로의 유니티 게임 프로그래밍 에센스 (11)
        • 유니티 스터디 자료 (0)
        • C# 공부 (0)
      • 유니티 프로젝트 (48)
        • 케이크게임 (13)
        • 점토게임 (35)
      • 언리얼 공부 (10)
        • 이득우의 언리얼 프로그래밍 (10)
      • 진로 (1)
      • 논문 읽기 (1)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    언리얼
    투포인터
    이벤트 트리거
    시뮬레이션
    그래프
    BFS
    재귀
    해시
    스택
    레트로의 유니티 프로그래밍
    그리디
    티스토리챌린지
    유니티 프로젝트
    오블완
    자료구조
    유니티
    C#
    바킹독
    백트래킹
    이분탐색
    유니티 공부 정리
    수학
    골드메탈
    큐
    이득우
    우선순위큐
    정렬
    백준
    dp
    맵
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
dubu0721
[백준] 1389번 케빈 베이컨의 6단계 법칙
상단으로

티스토리툴바