Submission #76875

#TimeUsernameProblemLanguageResultExecution timeMemory
76875shoemakerjoBosses (BOI16_bosses)C++14
100 / 100
718 ms1560 KiB
#include <bits/stdc++.h>

using namespace std;

#define maxn 5010
int n;


bool vis[maxn];
vector<vector<int>> ch(maxn);
queue<int> q;
int dep[maxn];

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin >> n;
	int v;
	for (int i = 1; i <= n; i++) {
		int ct;
		cin >> ct;
		for (int j = 0; j < ct; j++) {
			cin >> v;
			ch[v].push_back(i);
		}
		
	}
	int ans = n*n;
	for (int i = 1; i <= n; i++) {
		fill(vis, vis+maxn, false);
		q.push(i);
		dep[i] = 1;
		vis[i] = true;
		int cans = 1;
		while (!q.empty()) {
			v = q.front(); q.pop();
			for (int vv : ch[v]) {
				if (!vis[vv]) {
					dep[vv] = dep[v]+1;
					vis[vv] = true;
					cans += dep[vv];
					q.push(vv);
				}
			}
		}
		bool ok = true;
		for (int j = 1; j <= n; j++) {
			if (!vis[j]) {
				ok = false;
				// cout << "missed!" << endl;
			}
		}
		if (ok) ans = min(ans, cans);
	}
	cout << ans << endl;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...