๋ฌธ์ œ


BOJ ๋งํฌ


ํ’€์ด

ํ”Œ๋กœ์ด๋“œ-์›Œ์…œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ™œ์šฉํ•ด ๊ฐ ๋…ธ๋“œ์˜ ์นœ๊ตฌ ๊ด€๊ณ„๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.


์นœ๊ตฌ ๊ด€๊ณ„๋Š” ์ตœ๋Œ“๊ฐ’์„ ์ ์ˆ˜๋กœ ๋งค๊ธฐ๋ฏ€๋กœ, ์ถœ๋ฐœ ๋…ธ๋“œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ตœ๋Œ€ ๊ฐ€์ค‘์น˜๋ฅผ ๊ตฌํ•œ๋‹ค.


์ดํ›„ ๋‹ค๋ฅธ ๋…ธ๋“œ ์ ์ˆ˜์™€ ๋น„๊ตํ•ด ์ตœ์†Œ ์ ์ˆ˜๋ฅผ ๊ฐ€์ง„ ๋…ธ๋“œ๋ฅผ ๋ฐฐ์—ด์— ์ €์žฅํ•œ๋‹ค.


ํ›„๊ธฐ


์ด์ „ ๋ฌธ์ œ์—์„œ๋Š” ํ”Œ๋กœ์ด๋“œ ์›Œ์…œ ๊ทธ๋ž˜ํ”„๋ฅผ ๊ตฌํ•˜๊ณ , ์ •๋‹ต์šฉ ๋ฐฐ์—ด์„ ๊ณ„์‚ฐํ•˜๋Š” ๊ณผ์ •์—์„œ ์ž๊ธฐ ์ž์‹ ์— ๋Œ€ํ•œ ๊ฐ€์ค‘์น˜๋ฅผ ๋นผ์ฃผ์—ˆ๋‹ค.


์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ด€๋ จ ์ฝ”๋“œ๋Š” ํ•ด๋‹น ๊ตฌํ˜„๋ถ€์—์„œ ๋ชจ๋‘ ๋งˆ์น˜๊ณ , ์ •๋‹ต์„ ๊ณ„์‚ฐํ•˜๋Š” ๋ถ€๋ถ„์—์„  ๋‹ค๋ฅธ ์ฝ”๋“œ๋“ค์ด ์นจ๋ฒ”ํ•˜์ง€ ์•Š๋„๋ก init์— ์ž๊ธฐ ์ž์‹ ์— ๋Œ€ํ•œ ์ดˆ๊ธฐํ™” ์ฝ”๋“œ์„ ์ถ”๊ฐ€ํ–ˆ๋‹ค (17-20๋ฒˆ์งธ ์ค„).


์ฝ”๋“œ

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <vector>
using namespace std;

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);

	// input & init
	int fw[51][51];
	vector<int> v;
	int n;
	cin >> n;

	fill(&fw[0][0], &fw[n][n + 1], 1e9);
	for (int i = 1; i <= n; i++)
	{
		fw[i][i] = 0;
	}

	while (1)
	{
		int a, b;
		cin >> a >> b;

		if (a == -1) break;

		fw[a][b] = 1;
		fw[b][a] = 1;
	}

	// floyd-warshall
	for (int k = 1; k <= n; k++)
	{
		for (int i = 1; i <= n; i++)
		{
			for (int j = 1; j <= n; j++)
			{
				fw[i][j] = min(fw[i][k] + fw[k][j], fw[i][j]);
			}
		}
	}

	int res = 1e9;
	for (int i = 1; i <= n; i++)
	{
		int score = 0;
		for (int j = 1; j <= n; j++)
		{
			score = max(score, fw[i][j]);
		}

		if (score > res) continue;
		else if (score < res)
		{
			res = score;
			v.clear();
		}
		v.push_back(i);
	}

	// output
	cout << res << " " << v.size() << "\n";
	for (int i = 0; i < v.size(); i++)
	{
		cout << v[i] << " ";
	}

	return 0;
}



๊ฒฐ๊ณผ

image

Leave a comment