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;
typedef long long int ll;
typedef long double ld;
#define pb push_back
#define pf push_front
#define fi first
#define se second
const ll mod = 1e9+7, mxn = 5e3+7;
vector<vector<ll>> g(mxn), gg(mxn);
ll n, dp[mxn], pr[mxn];
bool vis[mxn];
void dfs(ll u) {for (ll i : gg[u]) {dfs(i); dp[u] += dp[i];}}
ll solve(ll s)
{
queue<ll> q; q.push(s);
memset(vis,0,sizeof(vis));
vis[s] = 1; pr[s] = 0;
while (!q.empty())
{
ll u = q.front(); q.pop();
for (ll i : g[u]) if (!vis[i])
{
vis[i] = 1; q.push(i);
pr[i] = u;
}
}
for (ll i = 1; i <= n; i++)
{
dp[i] = 1; gg[i].clear();
if (!vis[i]) return -1;
}
for (ll i = 1; i <= n; i++) if (pr[i]) gg[pr[i]].pb(i);
dfs(s);
ll ans = 0;
for (ll i = 1; i <= n; i++) ans += dp[i];
return ans;
}
signed main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// freopen("test.inp","r",stdin); freopen("test.out","w",stdout); freopen("test.err","w",stderr);
cin >> n;
for (ll i = 1; i <= n; i++)
{
ll k; cin >> k;
while (k--)
{
ll x; cin >> x;
g[x].pb(i);
}
}
ll ans = 1e18;
for (ll i = 1; i <= n; i++)
{
ll c = solve(i);
if (c != -1) ans = min(ans, c);
}
cout << ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |