This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int N;
bool adjMat[5001][5001];
int 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;
int ans = 0;
while (!q.empty()) {
array<int, 2> cur = q.front();
int u = cur[0];
int depth = cur[1];
q.pop();
ans += 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;
}
}
int ans = INT_MAX;
for (int i = 1; i <= N; i++) {
ans = min(ans, bfs(i)+1);
}
cout << ans << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |