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;
vector<int> adjlist[5000];
vector<int> treeadjlist[5000];
int INF = 1e9;
int n;
int treesum = 0;
int dfs(int root){
int tot = 1;
for (int i : treeadjlist[root]){
tot+=dfs(i);
}
treesum+=tot;
return tot;
}
int bfs(int root){
for (int i = 0; i<n; i++){
treeadjlist[i].clear();
}
vector<bool> visited(n);
queue<int> q;
q.push(root);
visited[root]=true;
int cnt = 1;
while (q.size()){
int f = q.front();
q.pop();
for (int i : adjlist[f]){
if (visited[i]){continue;}
visited[i]=true;
cnt++;
treeadjlist[f].push_back(i);
q.push(i);
}
}
if (cnt!=n){
return INF;
}
treesum=0;
dfs(root);
return treesum;
}
int main(){
cin.tie(0);
ios_base::sync_with_stdio(0);
cin>>n;
//adjlist.resize(n);
for (int i = 0; i<n; i++){
int k;
cin>>k;
for (int j = 0; j<k; j++){
int x;
cin>>x;
x--;
adjlist[x].push_back(i);
}
}
int minval = INF;
for (int i = 0; i<n; i++){
minval=min(minval,bfs(i));
}
cout<<minval<<'\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... |