๐Ÿ™‡โ€โ™€๏ธ[Gold III] ๊ฒŒ์ž„ ๊ฐœ๋ฐœ - 1516

๋ฌธ์ œ ๋งํฌ

์„ฑ๋Šฅ ์š”์•ฝ

๋ฉ”๋ชจ๋ฆฌ: 2552 KB, ์‹œ๊ฐ„: 36 ms

๋ถ„๋ฅ˜

๋ฐฉํ–ฅ ๋น„์ˆœํ™˜ ๊ทธ๋ž˜ํ”„, ๋‹ค์ด๋‚˜๋ฏน ํ”„๋กœ๊ทธ๋ž˜๋ฐ, ๊ทธ๋ž˜ํ”„ ์ด๋ก , ์œ„์ƒ ์ •๋ ฌ

์ œ์ถœ ์ผ์ž

2023๋…„ 12์›” 17์ผ 21:11:47

๋ฌธ์ œ ์„ค๋ช…

์ˆŒ ํšŒ์‚ฌ์—์„œ ์ด๋ฒˆ์— ์ƒˆ๋กœ์šด ์ „๋žต ์‹œ๋ฎฌ๋ ˆ์ด์…˜ ๊ฒŒ์ž„ ์„ธ์ค€ ํฌ๋ž˜ํ”„ํŠธ๋ฅผ ๊ฐœ๋ฐœํ•˜๊ธฐ๋กœ ํ•˜์˜€๋‹ค. ํ•ต์‹ฌ์ ์ธ ๋ถ€๋ถ„์€ ๊ฐœ๋ฐœ์ด ๋๋‚œ ์ƒํƒœ๊ณ , ์ข…์กฑ๋ณ„ ๊ท ํ˜•๊ณผ ์ „์ฒด ๊ฒŒ์ž„ ์‹œ๊ฐ„ ๋“ฑ์„ ์กฐ์ ˆํ•˜๋Š” ๋ถ€๋ถ„๋งŒ ๋‚จ์•„ ์žˆ์—ˆ๋‹ค.

๊ฒŒ์ž„ ํ”Œ๋ ˆ์ด์— ๋“ค์–ด๊ฐ€๋Š” ์‹œ๊ฐ„์€ ์ƒํ™ฉ์— ๋”ฐ๋ผ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ๋ชจ๋“  ๊ฑด๋ฌผ์„ ์ง“๋Š”๋ฐ ๊ฑธ๋ฆฌ๋Š” ์ตœ์†Œ์˜ ์‹œ๊ฐ„์„ ์ด์šฉํ•˜์—ฌ ๊ทผ์‚ฌํ•˜๊ธฐ๋กœ ํ•˜์˜€๋‹ค. ๋ฌผ๋ก , ์–ด๋–ค ๊ฑด๋ฌผ์„ ์ง“๊ธฐ ์œ„ํ•ด์„œ ๋‹ค๋ฅธ ๊ฑด๋ฌผ์„ ๋จผ์ € ์ง€์–ด์•ผ ํ•  ์ˆ˜๋„ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฌธ์ œ๊ฐ€ ๋‹จ์ˆœํ•˜์ง€๋งŒ์€ ์•Š์„ ์ˆ˜๋„ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด ์Šคํƒ€ํฌ๋ž˜ํ”„ํŠธ์—์„œ ๋ฒ™์ปค๋ฅผ ์ง“๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ฐฐ๋Ÿญ์„ ๋จผ์ € ์ง€์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ๋ฐฐ๋Ÿญ์„ ๋จผ์ € ์ง€์€ ๋’ค ๋ฒ™์ปค๋ฅผ ์ง€์–ด์•ผ ํ•œ๋‹ค. ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๊ฑด๋ฌผ์„ ๋™์‹œ์— ์ง€์„ ์ˆ˜ ์žˆ๋‹ค.

ํŽธ์˜์ƒ ์ž์›์€ ๋ฌดํ•œํžˆ ๋งŽ์ด ๊ฐ€์ง€๊ณ  ์žˆ๊ณ , ๊ฑด๋ฌผ์„ ์ง“๋Š” ๋ช…๋ น์„ ๋‚ด๋ฆฌ๊ธฐ๊นŒ์ง€๋Š” ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฌ์ง€ ์•Š๋Š”๋‹ค๊ณ  ๊ฐ€์ •ํ•˜์ž.

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ๊ฑด๋ฌผ์˜ ์ข…๋ฅ˜ ์ˆ˜ N(1 โ‰ค N โ‰ค 500)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ N๊ฐœ์˜ ์ค„์—๋Š” ๊ฐ ๊ฑด๋ฌผ์„ ์ง“๋Š”๋ฐ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„๊ณผ ๊ทธ ๊ฑด๋ฌผ์„ ์ง“๊ธฐ ์œ„ํ•ด ๋จผ์ € ์ง€์–ด์ ธ์•ผ ํ•˜๋Š” ๊ฑด๋ฌผ๋“ค์˜ ๋ฒˆํ˜ธ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๊ฑด๋ฌผ์˜ ๋ฒˆํ˜ธ๋Š” 1๋ถ€ํ„ฐ N๊นŒ์ง€๋กœ ํ•˜๊ณ , ๊ฐ ์ค„์€ -1๋กœ ๋๋‚œ๋‹ค๊ณ  ํ•˜์ž. ๊ฐ ๊ฑด๋ฌผ์„ ์ง“๋Š”๋ฐ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„์€ 100,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๋‹ค. ๋ชจ๋“  ๊ฑด๋ฌผ์„ ์ง“๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•œ ์ž…๋ ฅ๋งŒ ์ฃผ์–ด์ง„๋‹ค.

์ถœ๋ ฅ

N๊ฐœ์˜ ๊ฐ ๊ฑด๋ฌผ์ด ์™„์„ฑ๋˜๊ธฐ๊นŒ์ง€ ๊ฑธ๋ฆฌ๋Š” ์ตœ์†Œ ์‹œ๊ฐ„์„ ์ถœ๋ ฅํ•œ๋‹ค.

</br>

๐Ÿš€ํ’€์ด

๋ฌธ์ œ๋Š” ์ดํ•ดํ–ˆ๋Š”๋ฐ ๊ฑฐ์˜ ๋‹ค ํ•œ๊ฑฐ๊ฐ™์€๋ฐ ์•ˆํ’€๋ฆผ.

๊ฐ ์ผ€์ด์Šค์—์„œ ๊ฐ€์žฅ ์ตœ์†Œ์˜ ๊ฐ’์„ ๋“ค๊ณ ์žˆ์–ด์•ผํ•˜๋Š”๋ฐ ์ž˜ ์•ˆ๋จ.

์ €์žฅํ•˜๊ณ  ๋‹ค์Œ์— ํ’€์–ด์•ผ๊ฒ ๋‹ค.

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include<iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <queue>

using namespace std;

/*

๊ทธ๋ž˜ํ”„ ๋ฌธ์ œ์ž„
์•Œ์•„๋ฒ„๋ ธ๋‹ค
๋งจ ์•ž ์ˆซ์ž๋Š” ์ •์ ์˜ ์ •๋ณด
๊ทธ๋‹ค์Œ์— ๋‚˜์˜ค๋Š”๊ฑด ๊ฐ„์„  ์ •๋ณด์ž„


5
10 -1
10 1 -1
4 1 -1
4 3 1 -1
3 3 -1

*/

int n;
vector<vector<int>> seq;
vector<bool> discovered;
int result[510];

void setting()
{
	cin >> n;
	seq = vector<vector<int>>(n + 1, vector<int>(n + 1, -1));
	discovered = vector<bool>(n + 1, false);

	int temp;
	for (int i = 1; i <= n; ++i)
	{
		cin >> temp;
		seq[i][0] = temp;
		//result[i] = temp;
		while (true)
		{
			cin >> temp;
			if (temp == -1)
				break;

			seq[i][temp] = 1;
		}
	}
}

int bfs(int here)
{
	int want = here;
	discovered = vector<bool>(n + 1, false);
	int res = 0;
	queue<int> q;
	q.push(here);
	discovered[here] = true;

	while (q.empty() == false)
	{
		here = q.front();
		q.pop();
		res += seq[here][0];

		for (int there = 1; there <= n; ++there)
		{
			if (seq[here][there] == -1)
				continue;
			if (discovered[there])
				continue;

			//int next = seq[here][there];
			//result[next] = max(result[next], result[here] + seq[next][0]);

			q.push(there);
			discovered[there] = true;
		}
	}

	return res;
}

// ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒ
// ๋ฉ”๋ชจ์ด์ œ์ด์…˜์„ ํ•˜์ž

int cache[502];

void solve()
{
	for (int i = 1; i <= n; ++i)
	{
		cache[i] = bfs(i);
	}

	for (int i = 1; i <= n; ++i)
	{
		int res = 0;
		for (int j = 1; j <= n; ++j)
		{
			if (seq[i][j] != -1)
			{
				res = max(res, cache[j]);
			}
		}
		res += seq[i][0];
		cout << res << '\n';
	}

}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	freopen("input.txt", "rt", stdin);

	setting();
	solve();

	return 0;
}

ํƒœ๊ทธ:

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ: