#include <bits/stdc++.h>
#define REP(v, i, j) for (int v = i; v < j; v++)
#define FORI(v) for (auto i : v)
#define FORJ(v) for (auto j : v)
#define OUT(v, a) FORI(v) cout << i << a;
#define OUTS(v, a, b) cout << v.size() << a; OUT(v, b)
#define in(a, n) REP(i, 0, n) cin >> a[i];
#define SORT(v) sort(begin(v), end(v))
#define REV(v) reverse(begin(v), end(v))
#define pb push_back
#define fi first
#define se second
#define detachIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
using namespace std;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef pair<pii, pii> piiii;
vector<int> child[10000];
int dist[10000];
int n;
int bfs(int root)
{
memset(dist, -1, sizeof dist);
queue<pii> q;
q.push({root, 0});
while (q.size())
{
auto &x = q.front();
q.pop();
if (dist[x.fi] != -1)
continue;
dist[x.fi] = x.se;
FORI(child[x.fi])
q.push({i, x.se + 1});
}
REP(j, 0, n)
if (dist[j] == -1)
return -1;
return 0;
}
int main()
{
detachIO;
cin >> n;
REP(i, 0, n)
{
int x;
cin >> x;
while (x--)
{
int t;
cin >> t;
t--;
child[t]
.pb(i);
}
}
int finalres = INT_MAX;
REP(i, 0, n)
{
if (bfs(i))
continue;
int ans = 0;
REP(j, 0, n)
ans += dist[j];
ans += n;
finalres = min(finalres, ans);
}
cout << finalres << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
588 KB |
Output is correct |
2 |
Correct |
1 ms |
588 KB |
Output is correct |
3 |
Correct |
1 ms |
588 KB |
Output is correct |
4 |
Correct |
1 ms |
588 KB |
Output is correct |
5 |
Correct |
1 ms |
588 KB |
Output is correct |
6 |
Correct |
1 ms |
588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
588 KB |
Output is correct |
2 |
Correct |
1 ms |
588 KB |
Output is correct |
3 |
Correct |
1 ms |
588 KB |
Output is correct |
4 |
Correct |
1 ms |
588 KB |
Output is correct |
5 |
Correct |
1 ms |
588 KB |
Output is correct |
6 |
Correct |
1 ms |
588 KB |
Output is correct |
7 |
Correct |
1 ms |
588 KB |
Output is correct |
8 |
Correct |
1 ms |
552 KB |
Output is correct |
9 |
Correct |
1 ms |
588 KB |
Output is correct |
10 |
Correct |
1 ms |
588 KB |
Output is correct |
11 |
Correct |
1 ms |
588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
588 KB |
Output is correct |
2 |
Correct |
1 ms |
588 KB |
Output is correct |
3 |
Correct |
1 ms |
588 KB |
Output is correct |
4 |
Correct |
1 ms |
588 KB |
Output is correct |
5 |
Correct |
1 ms |
588 KB |
Output is correct |
6 |
Correct |
1 ms |
588 KB |
Output is correct |
7 |
Correct |
1 ms |
588 KB |
Output is correct |
8 |
Correct |
1 ms |
552 KB |
Output is correct |
9 |
Correct |
1 ms |
588 KB |
Output is correct |
10 |
Correct |
1 ms |
588 KB |
Output is correct |
11 |
Correct |
1 ms |
588 KB |
Output is correct |
12 |
Correct |
28 ms |
716 KB |
Output is correct |
13 |
Correct |
21 ms |
792 KB |
Output is correct |
14 |
Correct |
129 ms |
588 KB |
Output is correct |
15 |
Correct |
7 ms |
708 KB |
Output is correct |
16 |
Correct |
572 ms |
780 KB |
Output is correct |
17 |
Correct |
637 ms |
800 KB |
Output is correct |
18 |
Correct |
633 ms |
796 KB |
Output is correct |