Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Tags
- FIFO paging
- inflearn
- OS
- 토마토
- 이펙티브코틀린
- 에러핸들링
- TCP/IP
- 제프리리처
- 운영체제
- 김영한
- Four Squares
- 열혈 tcp/ip 프로그래밍
- 스프링 핵심 원리
- HTTP
- 2475번
- 우아한레디스
- Spring
- 열혈 TCP/IP 소켓 프로그래밍
- 10026번
- C++
- Window-Via-c/c++
- BOJ
- Operating System.
- redis
- C#
- 윤성우 저자
- n타일링2
- 우아한 테크 세미나
- 스프링 입문
- Operating System
Archives
- Today
- Total
나의 브을로오그으
[c++] 1389번 : 케빈 베이컨의 6단계 법칙 본문
https://www.acmicpc.net/problem/1389
1389번: 케빈 베이컨의 6단계 법칙
첫째 줄에 유저의 수 N (2 ≤ N ≤ 100)과 친구 관계의 수 M (1 ≤ M ≤ 5,000)이 주어진다. 둘째 줄부터 M개의 줄에는 친구 관계가 주어진다. 친구 관계는 A와 B로 이루어져 있으며, A와 B가 친구라는 뜻
www.acmicpc.net
#include <iostream>
#include <queue>
using namespace std;
#define MAX_USER 100
#define MAX_RELATION 2147483647
int GetKevinBaconNumber(bool relations[][MAX_USER + 1], int from, int N);
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
bool relations[MAX_USER + 1][MAX_USER + 1] = { false, };
int minUser = 101;
int minKevinBaconNumber = MAX_RELATION;
int from = 0, to = 0;
int N = 0, M = 0;
cin >> N >> M;
for (int i = 0; i < M; ++i)
{
cin >> from >> to;
relations[from][to] = true;
relations[to][from] = true;
}
for (int from = 1; from <= N; ++from)
{
int kevinBaconNumber = GetKevinBaconNumber(relations, from, N);
if (kevinBaconNumber < minKevinBaconNumber)
{
minKevinBaconNumber = kevinBaconNumber;
minUser = from;
}
}
cout << minUser << '\n';
return 0;
}
int GetKevinBaconNumber(bool relations[][MAX_USER + 1], int from, int N)
{
queue<int> q;
bool users[MAX_USER + 1] = { false, };
int kevinBaconNumber = 0;
int degree = 1;
q.push(from);
users[from] = true;
while (q.empty() == false)
{
int size = q.size();
for (int i = 0; i < size; ++i)
{
from = q.front();
q.pop();
for (int to = 1; to <= N; ++to)
{
if (users[to] == false && relations[from][to] == true)
{
users[to] = true;
q.push(to);
kevinBaconNumber += degree;
}
}
}
++degree;
}
return kevinBaconNumber;
}
'알고리즘 > BaekJoon' 카테고리의 다른 글
[c++] 1541번 : 잃어버린 괄호 (0) | 2022.06.07 |
---|---|
[c++] 1463번 : 1로 만들기 (0) | 2022.06.03 |
[c++] 1260번 : DFS와 BFS (0) | 2022.05.31 |
[c++] 1107번 : 리모컨 (0) | 2022.05.30 |
[c++] 1074번 : Z (0) | 2022.05.20 |