Submission #733479

#TimeUsernameProblemLanguageResultExecution timeMemory
733479JellyTheOctopusBosses (BOI16_bosses)C++17
0 / 100
0 ms212 KiB
#include <bits/stdc++.h>
using namespace std;

int N;
bool adjMat[5001][5001];

long long bfs(int root) {
	vector<bool> seen(N+1);
	queue<array<int, 2>> q; // node, depth
	q.push({root, 1}); // starting depth at one
	seen[root] = true;
	long long ans = 0;
	while (!q.empty()) {
		array<int, 2> cur = q.front();
		int u = cur[0];
		int depth = cur[1];
		q.pop();
		ans += (long long)depth;
		for (int v = 1; v <= N; v++) {
			if (adjMat[u][v] && !seen[v]) {
				q.push({v, depth+1});
				seen[v] = true;
			}
		}
	}
	return ans;
}

int main() {
	cin >> N;
	cin.ignore();
	for (int v = 1; v <= N; v++) {
		string line;
		getline(cin, line);
		istringstream iss(line);
		int u;
		while (iss >> u) {
			if (u == v) {
				continue;
			}
			adjMat[u][v] = true;
		}
	}
	long long ans = LLONG_MAX;
	for (int i = 1; i <= N; i++) {
		ans = min(ans, bfs(i)+1);
	}
	cout << ans << "\n";
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...