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>
using namespace std;
#define fi first
#define se second
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 5e3 + 5;
int N, ans = 1e9;
vector<int> G[MAXN];
bool vis[MAXN];
int solve(int n) {
memset(vis, 0, sizeof(vis));
queue<pii> q; q.push(pii(n, 1));
vis[n] = 1;
int sum = 1, cnt = 0;
while(!q.empty()) {
int cur = q.front().fi, dep = q.front().se; q.pop();
cnt++;
for(auto nxt : G[cur]) if(!vis[nxt]) {
vis[nxt] = 1;
sum += dep + 1;
q.push(pii(nxt, dep+1));
}
}
if(cnt == N) return sum;
return 1e9;
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
cin >> N;
for(int i=1; i<=N; i++) {
int x, y; cin >> x;
for(int j=1; j<=x; j++) {
cin >> y;
G[y].push_back(i);
}
}
for(int i=1; i<=N; i++) ans = min(ans, solve(i));
cout << 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... |