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;
const int maxn = 5e3+10;
const int inf = 1e9+1;
int n,ans,sum,sz;
bool check;
vector<int>G[maxn];
vector<int>H[maxn];
bool marked[maxn];
int val[maxn];
void bfs(int v){
marked[v] = true;
queue<int>q;
q.push(v);
while(!q.empty()){
int p = q.front();
q.pop();
sz++;
H[p].clear();
for(int b : G[p]){
if(marked[b]) continue;
marked[b] = true;
q.push(b);
H[p].push_back(b);
}
}
}
void dfs(int v){
int s = 0;
sz++;
for(int b : H[v]){
dfs(b);
s += val[b];
}
val[v] = s + 1;
sum += val[v];
if(ans <= sum){
check = false;
return;
}
}
int32_t main(){
ios:: sync_with_stdio(0),cin.tie(0),cout.tie(0);
////////////////////////////////////////////////
ans = inf;
sum = 0;
cin >> n;
for(int i = 0;i<n;i++){
int x; cin >> x;
for(int j = 0;j<x;j++){
int y; cin >> y;
G[y].push_back(i+1);
}
}
for(int i = 0;i<n;i++){
fill(marked,marked+n+1,false);
sz = 0;
bfs(i+1);
if(sz != n){
continue;
}
check = true;
sum = 0;
sz = 0;
dfs(i+1);
if(check == true && sz == n){
ans = min(ans,sum);
}
}
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... |