Submission #1018746

#TimeUsernameProblemLanguageResultExecution timeMemory
1018746coolboy19521Bosses (BOI16_bosses)C++17
100 / 100
541 ms812 KiB
#include "bits/stdc++.h"
#define int long long

using namespace std;

const int inf = 1e18 + 18;
const int sz = 5e3 + 15;

vector<int> aj[sz];
bool vi[sz];
int n;

int bfs(int x) {
	fill_n(vi, sz, false);
	queue<pair<int,int>> q;
	q.push(make_pair(1,x));
	int r = 0;
	int c = 0;

	while (!q.empty()) {
		auto tp = q.front();
		q.pop();
		int d = tp.first;
		int v = tp.second;
		if (vi[v])
			continue;
		vi[v] = true;
		r += d;
		c ++;
		for (int u : aj[v])
			if (!vi[u])
				q.push(make_pair(d + 1, u));
	}

	return (c == n ? r : inf);
}

signed main() {
	cin.tie(nullptr)->sync_with_stdio(false);

	cin >> n;

	for (int i = 1; i <= n; i ++) {
		int k;
		cin >> k;
		for (int j = 1; j <= k; j ++) {
			int v;
			cin >> v;
			aj[v].push_back(i);
		}
	}

	int mn = inf;

	for (int i = 1; i <= n; i ++) {
		int r = bfs(i);
		mn = min(mn, r);
	}

	cout << mn << '\n';
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...