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 FOR(i, a, b) for (int i = a; i<=b ; i++)
#define FORD(i, a, b) for (int i = a; i>=b; i--)
#define REP(i, a) for (int i = 0; i<a; i++)
#define N 5050
#define pp pair<int, int>
#define IO cin.tie(NULL);cout.tie(NULL);
#define bit(S, i) (((S) >> i) & 1)
template<typename T> inline void read(T &x) {
char c;
bool neg = false;
while (!isdigit(c = getchar()) && c != '-');
x = 0;
if (c == '-') {
neg = true;
c = getchar();
}
do {
x = x * 10 + c - '0';
} while (isdigit(c = getchar()));
if (neg) x = -x;
}
template<typename T> inline void write(T x) {
if (x < 0) {
putchar('-');
write(-x);return;
}
if (x < 10) {
putchar(char(x + 48));
}
else {
write(x/10);
putchar(char(48 + x%10));
}
}
template<typename T> inline void writeln(T x) {
write(x);
putchar('\n');
}
using namespace std;
int n, k, u, p[N];
vector<int> e[N];
queue<int> q;
long long d[N];
long long bfs(int s) {
FOR(i, 1, n) p[i] = 0;
p[s] = -1;
d[s] = 1;
q.push(s);
int cnt = 0;
long long ans = 0;
while (!q.empty()) {
int u = q.front();
q.pop();
ans += d[u];
cnt++;
for (int v : e[u]) if (p[v] == 0) {
p[v] = u;
d[v] = d[u] + 1;
q.push(v);
}
}
if (cnt == n) return ans;
return 1e18;
}
int main() {
IO;
read(n);
FOR(i, 1, n) {
read(k);
while (k--) {
read(u);
e[u].push_back(i);
}
}
long long ans = 1e18;
FOR(i, 1, n) ans = min(ans, bfs(i));
writeln(ans);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |