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>
#define all(dataStructure) dataStructure.begin(),dataStructure.end()
#define ll long long
using namespace std;
namespace std {
template <typename T, int D>
struct _vector : public vector <_vector <T, D - 1>> {
static_assert(D >= 1, "Dimension must be positive!");
template <typename... Args>
_vector(int n = 0, Args... args) : vector <_vector <T, D - 1>> (n, _vector <T, D - 1> (args...)) {}
};
// _vector <int, 3> a(n, m, k);: int a[n][m][k].
// _vector <int, 3> a(n, m, k, x);: int a[n][m][k] initialized with x.
template <typename T>
struct _vector <T, 1> : public vector <T> {
_vector(int n = 0, const T& val = T()) : vector <T> (n, val) {}
};
}
const int MAX = 5e3 + 3;
const ll MOD[] = {1000000007, 998244353};
int n, depth[MAX];
vector <int> adj[MAX];
void Solve() {
cin >> n;
for (int i = 1; i <= n; i++) {
int k;
cin >> k;
while (k--) {
int j;
cin >> j;
adj[j].push_back(i);
}
}
ll ans = 1e9;
for (int i = 1; i <= n; i++) {
fill(depth + 1, depth + n + 1, 0);
queue <int> q;
depth[i] = 1;
q.push(i);
while (q.size()) {
int u = q.front();
q.pop();
for (int &v : adj[u]) {
if (!depth[v]) {
depth[v] = depth[u] + 1;
q.push(v);
}
}
}
ll tmp = 0;
for (int i = 1; i <= n; i++) {
tmp += depth[i];
if (!depth[i]) {
tmp = 1e9;
break;
}
}
ans = min(ans, tmp);
}
cout << ans;
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
#define TASK "TASK"
if (fopen(TASK".INP", "r")) {
freopen(TASK".INP", "r", stdin);
freopen(TASK".OUT", "w", stdout);
}
/* int TEST = 1; cin >> TEST; while (TEST--) */ Solve();
cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "s\n";
return 0;
}
Compilation message (stderr)
bosses.cpp: In function 'int32_t main()':
bosses.cpp:76:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
76 | freopen(TASK".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
bosses.cpp:77:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
77 | freopen(TASK".OUT", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |