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;
typedef long long lint;
typedef pair<int, int> pii;
const int INF = 2e9;
const int MOD = 1e9+7;
const int MAX = 5010;
const lint LNF = 2e18;
int n;
vector<int> G[MAX], R[MAX];
int solve(int r){
int res = 1;
queue<int> Q;
int dep[MAX] = {};
dep[r] = 1; Q.push(r);
while(!Q.empty()){
int v; v=Q.front(); Q.pop();
for(int x:R[v]){
if(dep[x]) continue;
Q.push(x); dep[x] = dep[v]+1;
res += dep[x];
}
}
for(int i=1; i<=n; i++) if(dep[i]==0) return INF;
return res;
}
int main(){
ios::sync_with_stdio(0); cin.tie(0);
cin>>n;
for(int i=1; i<=n; i++){
int k, x; cin>>k;
for(int j=1; j<=k; j++) cin>>x, G[i].push_back(x), R[x].push_back(i);
}
int ans = INF;
for(int i=1; i<=n; i++) ans = min(ans, solve(i));
cout<<ans<<'\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |