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 ll long long
#define forn(i,n) for(int i=0;i<n;i++)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(),v.rend()
#define pb push_back
#define sz(a) (int)a.size()
const int N = 5005;
bool vis[N];
vector<int> adj[N];
vector<int> g[N];
int c[N];
int f = 0;
void bfs(int u){
forn(i, N)g[i].clear(), vis[i] = false;
vis[u] = true;
queue<int> q;
q.push(u);
vis[u] = true;
while(!q.empty()){
int i = q.front();
q.pop();
for(int v: adj[i]){
if(!vis[v]){
q.push(v);
g[i].pb(v);
vis[v] = true;
}
}
}
}
void dfs(int u, int par){
ll s = 0;
for(int v: g[u]){
if(v == par)continue;
dfs(v, u);
s += c[v];
}
c[u] = s + 1;
f += c[u];
}
void solve()
{
int n;
cin >> n;
for(int i = 0;i < n; ++i){
int k;
cin >> k;
while(k--){
int a;
cin >> a;
--a;
adj[a].pb(i);
}
}
int ans = INT_MAX;
for(int i = 0;i < n; ++i){
bfs(i);
bool ok = true;
for(int j = 0;j < n; ++j){
if(!vis[j])ok = false;
}
if(!ok)continue;
f = 0;
dfs(i, -1);
ans = min(ans, f);
}
cout << ans << "\n";
}
int32_t main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int t = 1;
//cin >> t;
while(t--)
{
solve();
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |