# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
230567 | AlexLuchianov | Bosses (BOI16_bosses) | C++14 | 759 ms | 768 KiB |
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 <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cassert>
#include <queue>
using namespace std;
using ll = long long;
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) < (b)) ? (b) : (a))
int const nmax = 5000;
vector<int> g[1 + nmax];
int dist[1 + nmax];
int eval(int node, int n){
for(int i = 1; i <= n; i++)
dist[i] = 0;
queue<int> q;
dist[node] = 1;
ll result = 0;
q.push(node);
while(0 < q.size()){
int node = q.front();
q.pop();
for(int h = 0; h < g[node].size(); h++){
int to = g[node][h];
if(dist[to] == 0){
dist[to] = dist[node] + 1;
q.push(to);
}
}
}
for(int i = 1;i <= n; i++) {
result += dist[i];
if(0 == dist[i])
return -1;
}
return result;
}
int main()
{
int n;
cin >> n;
for(int i = 1;i <= n; i++){
int k;
cin >> k;
for(int j = 1;j <= k; j++){
int val;
cin >> val;
g[val].push_back(i);
}
}
ll result = -1;
for(int i = 1;i <= n; i++){
int cost = eval(i, n);
if(cost == -1)
continue;
if(result == -1)
result = cost;
else if(cost < result)
result = cost;
}
cout << result;
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |