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 N = 5003;
vector <int> adj[N], adj2[N];
int vs[N], sz[N], ans, tmp = 1e9+7, n;
queue <int> q;
void bfs(){
while(!q.empty()){
int u = q.front();
vs[u] = 1;
//cout << "BFS " << u << endl;
q.pop();
for (auto v: adj[u]){
if (!vs[v]){
q.push(v);
adj2[u].push_back(v);
vs[v] = 1;
}
}
}
//cout << endl;
}
void dfs(int u, int par){
sz[u]++;
for (auto v: adj2[u]){
if (v != par){
dfs(v, u);
sz[u] += sz[v];
}
}
ans += sz[u];
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++){
int m;
cin >> m;
for (int j = 1; j <= m; j++){
int x;
cin >> x;
adj[x].push_back(i);
}
}
for (int i = 1; i <= n; i++){
for (int j = 1; j <= n; j++){
vs[j] = 0;
adj2[j].clear();
}
q.push(i);
bfs();
bool able = true;
for (int j = 1; j <= n; j++){
if (!vs[j]){
able = 0;
}
}
if (!able){
continue;
}
ans = 0;
dfs(i, i);
tmp = min(tmp, ans);
}
cout << tmp;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |