제출 #894264

#제출 시각아이디문제언어결과실행 시간메모리
894264boxBosses (BOI16_bosses)C++17
100 / 100
402 ms788 KiB
#include <bits/stdc++.h>
using namespace std;
 
const int N = 5000;
 
int main() {
  ios::sync_with_stdio(false); cin.tie(NULL);
  int n; cin >> n;
  static vector<int> c[N];
  for (int i = 0; i < n; i++) {
    int k; cin >> k;
    while (k--) {
      int j; cin >> j, j--;
      c[j].push_back(i);
    }
  }
  long long ans = LLONG_MAX;
  for (int s = 0; s < n; s++) {
    static int d[N];
    memset(d, -1, n * 4);
    queue<int> q;
    d[s] = 0, q.push(s);
    while (!q.empty()) {
      int i = q.front(); q.pop();
      for (int j : c[i]) if (d[j] == -1)
        d[j] = d[i] + 1, q.push(j);
    }
    long long x = 0; bool f = 0;
    for (int i = 0; i < n; i++)
      if (d[i] == -1) { f = 1; break; }
      else x += d[i] + 1;
    if (!f) ans = min(ans, x);
  }
  cout << ans << '\n';
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...