Scroll indicator done
728x90

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


#include<iostream>
#include<queue>
#include<string.h>
using namespace std;
int map[101][101];
bool chk[101];
int main() {
	int n, m;

	cin >> n >> m;
	int input1;
	int input2;
	for (int i = 0; i < m; i++)
	{
		cin >> input1 >> input2;

		map[input1][input2] = 1;
		map[input2][input1] = 1;
	}
	queue<pair<int, int>> q;

	int sum = 0;
	int result = 999999;
	int people = 0;
	for (int i = 1; i <= n; i++)
	{
		q.push({ i,0 });
		chk[i] = true;
		sum = 0;
		while (!q.empty())
		{

			int curNum = q.front().first;
			int nextLayer = q.front().second + 1;
			q.pop();

			for (int j = 1; j <= n; j++)
			{
				if (map[curNum][j] == 1 && !chk[j])
				{
					chk[j] = true;
					q.push({ j, nextLayer });
					sum += nextLayer;
				}
			}
		}

		if (sum < result)
		{
			result = sum;
			people = i;
		}
		memset(chk, false, sizeof(chk));
	}

	cout << people << '\n';
	return 0;
}

 

728x90

'BAEKJOON > C++' 카테고리의 다른 글

[B2178][미로 탐색][C++]  (0) 2021.07.16
[B1992][쿼드트리][C++]  (0) 2021.07.14
[B11047][동전 0][C++]  (0) 2021.07.05
[B5525][IOIOI][C++]  (0) 2021.07.02
[B1268][임시 반장 정하기][C++]  (0) 2021.07.01