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 int long long
const int N = 5e3 + 10;
int n;
vector<int> adj[N];
int visited[N];
int par[N];
int sub[N];
int val[N];
signed main(){
iostream::sync_with_stdio(false);
cin.tie(nullptr);cout.tie(nullptr);
cin >> n;
for(int i = 0;i<n;i++){
int c;cin >> c;
for(int j = 0;j<c;j++){
int temp;cin >> temp;
temp --;
adj[temp].push_back(i);
}
}
int ans = 1e18;
for(int i = 0;i<n;i++){
// This will the root of the tree
for(int i = 0;i<n;i++){
visited[i] =false;
par[i] = -1;
sub[i] = 0;
val[i] = 1;
}
queue<int> q;
queue<int> q2;
q.push(i);
visited[i] = true;
while(!q.empty()){
int x = q.front();
// cout << x << "==> ";
q.pop();
for(int e : adj[x]){
if(visited[e])
continue;
else{
visited[e] = true;
par[e] = x;
sub[x]++;
q.push(e);
// cout << e << " ";
}
}
// cout << "\n";
if(sub[x] == 0)
q2.push(x);
}
bool flag = false;
for(int i = 0;i<n;i++){
if(!visited[i]){
// cout << i << "\n";
flag = true;
}
}
// for(int i = 0;i<n;i++)
// cout<< par[i] << " ";
// cout << "\n";
if(flag)continue;
int temp = 0;
while(!q2.empty()){
int x = q2.front();
q2.pop();
temp += val[x];
// cout << x << " " << val[x] << "\n";
if(par[x] == -1)
continue;
sub[par[x]]--;
val[par[x]] += val[x];
if(sub[par[x]] == 0)
q2.push(par[x]);
}
ans = min(ans,temp);
}
cout << ans << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |