#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> adj;
vector<int> vis;
int res = 0;
void bfs(int x){
queue<pair<int,int>> q;
q.push({x,1});
vis[x] = 1;
while(!q.empty()){
auto [a,b] = q.front();q.pop();
if(vis[a]< b) continue;
res += b;
for(auto i: adj[a]){
if(vis[i] != -1) continue;
vis[i] = b + 1;
q.push({i, vis[i]});
}
}
}
int main(){
int n; cin >> n;
adj.resize(n);
for(int i=0; i<n; i++){
int k; cin >> k;
for(int j =0; j<k; j++){
int a; cin >> a; a--;
adj[a].push_back(i);
}
}
int menor = -1;
for(int i=0; i<n; i++){
vis.assign(n,-1);
res = 0;
bfs(i);
bool ok = 1;
for(int j =0; j<n; j++) {
if(vis[j] == -1) ok = 0;
}
if(!ok) continue;
if(menor == -1) menor =res;
else menor = min(menor, res);
}
cout << menor << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
4 ms |
348 KB |
Output is correct |
13 |
Correct |
4 ms |
520 KB |
Output is correct |
14 |
Correct |
128 ms |
628 KB |
Output is correct |
15 |
Correct |
22 ms |
604 KB |
Output is correct |
16 |
Correct |
518 ms |
708 KB |
Output is correct |
17 |
Correct |
498 ms |
708 KB |
Output is correct |
18 |
Correct |
490 ms |
712 KB |
Output is correct |