#include <bits/stdc++.h>
#define int long long
#define sz(a) (int)a.size()
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
using namespace std;
vector<vector<int>> child;
int s=0;
int dfs(int x){
int sm=0;
for (auto u : child[x])
{
sm+=dfs(u);
}
s+=sm+1;
return sm+1;
}
signed main() {
ios_base::sync_with_stdio(false); cin.tie(nullptr);
int n; cin >> n;
vector<vector<int>> neigh(n);
child.resize(n);
for (int i = 0; i < n; i++)
{
int k; cin >> k;
for (int j = 0; j < k; j++)
{
int x; cin >> x;
neigh[x-1].push_back(i);
}
}
int mn=1e18;
for (int i = 0; i < n; i++)
{
vector<int> dist(n,1e18);
vector<bool> visited(n,false);
for (int j = 0; j < n; j++) child[j].clear();
dist[i]=0;
queue<int> queue;
queue.push(i);
while(!queue.empty()){
int x=queue.front(); queue.pop();
if(visited[x]) continue;
visited[x]=true;
for (auto u : neigh[x])
{
if(dist[u]>dist[x]+1){
dist[u]=dist[x]+1;
child[x].push_back(u);
queue.push(u);
}
}
}
bool b=false;
for (int j = 0; j < n; j++)
{
if(!visited[j]) b=true;
}
if(b) continue;
s=0;
dfs(i);
mn=min(s,mn);
}
cout << mn << "\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... |