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;
void main_program();
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0);
main_program();
}
int n;
vector<int> depth;
vector<vector<int>> adj;
int total;
void main_program(){
cin >> n;
adj.resize(n);
for (int i = 0; i < n; i++){
int sz; cin >> sz;
for (int j = 0; j < sz; j++){
int x; cin >> x; x--;
adj[x].push_back(i);
}
}
int res = 1e9;
for (int root = 0; root < n; root++){
depth.assign(n, 0);
total = 1;
queue<int> q; q.push(root); depth[root] = 1;
while (!q.empty()){
int x = q.front(); q.pop();
for (auto k: adj[x]){
if (!depth[k]){
depth[k] = depth[x] + 1;
total += depth[k];
q.push(k);
}
}
}
bool valid = true;
for (int i = 0; i < n; i++){
if (!depth[i]){
valid = false;
break;
}
}
if (!valid) continue;
res = min(res, total);
}
cout << res << "\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... |