제출 #484212

#제출 시각아이디문제언어결과실행 시간메모리
484212Sohsoh84Bosses (BOI16_bosses)C++17
100 / 100
1111 ms27972 KiB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pll;

#define all(x)			(x).begin(),(x).end()
#define X			first
#define Y			second
#define sep			' '
#define endl			'\n'
#define debug(x)		cerr << #x << ": " <<  x << endl;

const ll MAXN = 1e6 + 10;

int n, dist[MAXN];
vector<int> adj[MAXN];

inline ll calc(int v) {
	memset(dist, 63, sizeof dist);
	queue<int> q;
	q.push(v);
	dist[v] = 1;

	ll ans = 0;
	while (!q.empty()) {
		v = q.front();
		q.pop();
		ans += dist[v];
		
		for (int u : adj[v]) {
			if (dist[v] + 1 < dist[u]) {
				dist[u] = dist[v] + 1;
				q.push(u);
			}
		}
	}

	for (int i = 1; i <= n; i++) 
		if (dist[i] > MAXN)
			return MAXN * MAXN;

	return ans;
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	cin >> n;
	
	for (int i = 1; i <= n; i++) {
		int k;
		cin >> k;
		for (int j = 0; j < k; j++) {
			int x;
			cin >> x;
			adj[x].push_back(i);
		}
	}

	ll ans = MAXN * MAXN;
	for (int i = 1; i <= n; i++)
		ans = min(ans, calc(i));

	cout << ans << endl;
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...