#include <bits/stdc++.h>
using namespace std;
const int N = 5e3 + 5;
#define ll long long
#define pb push_back
#define sz size()
ll n, sub[N], vis[N];
vector <int> v[N], p[N];
void solve (int x, int pr) {
for (int i : p[x]) {
//if (i == pr) continue;
solve (i, x);
sub[x] += sub[i];
}
sub[x]++;
}
ll root(int x) {
queue <int> q;
for (int i = 1; i <= n; ++i) {
vis[i] = 0;
sub[i] = 0;
p[i].clear();
}
q.push(x);
vis[x] = 1;
while (!q.empty()) {
int x = q.front();
q.pop();
for (int i : v[x]) {
if (vis[i]) continue;
p[x].pb (i);
vis[i] = 1;
q.push(i);
}
}
for (int i = 1; i <= n; ++i) {
if (!vis[i]) return -1;
}
solve (x, -1);
ll ret = 0;
for (int i = 1; i <= n; ++i) {
ret += sub[i];
}
return ret;
}
int main () {
// freopen ("input.txt", "r", stdin);
cin >> n;
for (int i = 1; i <= n; ++i) {
int k;
cin >> k;
while (k--) {
ll x;
cin >> x;
v[x].pb (i);
}
}
ll jog = 1e18;
for (int i = 1; i <= n; ++i) {
ll jj = root (i);
if (~jj) {
jog = min (jog, jj);
}
}
cout << jog;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
604 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
604 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
600 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
604 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
600 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
5 ms |
860 KB |
Output is correct |
13 |
Correct |
4 ms |
604 KB |
Output is correct |
14 |
Correct |
148 ms |
1032 KB |
Output is correct |
15 |
Correct |
31 ms |
856 KB |
Output is correct |
16 |
Correct |
544 ms |
996 KB |
Output is correct |
17 |
Correct |
886 ms |
1056 KB |
Output is correct |
18 |
Correct |
919 ms |
1052 KB |
Output is correct |