/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, OCaml, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.
*******************************************************************************/
#include <iostream>
#include<vector>
#include <queue>
using namespace std;
long long bfs(int v, vector<vector<int>> &g) {
queue<int> q;
q.push(v);
int n = g.size();
vector<long long> deep(n, 1e10);
deep[v] = 1;
while (q.size() > 0) {
int u = q.front();
q.pop();
for (int i = 0; i < g[u].size(); ++i) {
if (deep[g[u][i]] > 1000000) {
deep[g[u][i]] = deep[u] + 1;
q.push(g[u][i]);
}
}
}
long long summ = 0;
for(int i = 0; i < n; ++i) {
summ += deep[i];
}
return summ;
}
int main()
{
int n;
cin >> n;
vector<vector<int>> g(n);
for (int i = 0; i < n; ++i) {
int k;
cin >> k;
for (int j = 0; j < k; ++j) {
int v;
cin >> v;
--v;
g[v].push_back(i);
}
}
long long total_cost = 1000000000000;
for (int i = 0; i < n; ++i) {
long long ans = bfs(i, g);
if(ans < total_cost) {
total_cost = ans;
}
}
cout << total_cost;
return 0;
}
Compilation message
bosses.cpp: In function 'long long int bfs(int, std::vector<std::vector<int> >&)':
bosses.cpp:24:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
24 | for (int i = 0; i < g[u].size(); ++i) {
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
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 |
344 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 |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 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 |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
5 ms |
508 KB |
Output is correct |
13 |
Correct |
4 ms |
344 KB |
Output is correct |
14 |
Correct |
102 ms |
600 KB |
Output is correct |
15 |
Correct |
22 ms |
600 KB |
Output is correct |
16 |
Correct |
490 ms |
852 KB |
Output is correct |
17 |
Correct |
463 ms |
788 KB |
Output is correct |
18 |
Correct |
461 ms |
780 KB |
Output is correct |