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;
#define idata vector<int>
#define graph vector<idata>
#define INF 1e9
int nbNodes;
graph roads;
int bfs (int start) {
idata dist (nbNodes, INF);
queue<int> q;
dist[start] = 0;
q.push(start);
int res = 0;
int con = 0;
while (q.size() != 0) {
int curr = q.front();
q.pop();
res += dist[curr];
con ++;
for (int next : roads[curr]) {
if (dist[next] != INF) continue ;
dist[next] = dist[curr] + 1;
q.push(next);
}
}
if (con != nbNodes) return INF;
return res;
}
int answer () {
int res = INF;
for (int id = 0; id < nbNodes; id ++)
res = min(res, bfs(id));
return res + nbNodes;
}
int main () {
cin >> nbNodes;
roads.resize(nbNodes);
for (int id = 0; id < nbNodes; id ++) {
int cnt; cin >> cnt;
for (int jd = 0; jd < cnt; jd ++) {
int x;
cin >> x; x --;
roads[x].push_back(id);
}
}
cout << answer();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |