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 lev[N];
int dfs(int i){
if(!visited[i]){
int ans = 0;
vector<int> temp;
visited[i] = true;
for(int e : adj[i]){
// cout << 'd' << " " << e << "\n";
if(!visited[e]){
temp.push_back(e);
ans += dfs(e);
}
}
// cout << i << "=>> ";
// for(int e : temp)
// cout << e << " ";
// cout << "\n";
lev[i] = ans + 1;
return lev[i];
}
return 0;
}
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[i].push_back(temp);
}
}
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;
lev[i] = -1;
}
dfs(i);
int temp = 0;
for(int i = 0;i<n;i++){
// cout << lev[i] << " ";
temp += lev[i];
if(!visited[i]){
// cout << i << "\n";
goto next;
}
}
// cout << "\n";
ans = min(ans,temp);
next:
continue;
}
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... |