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>
#define ll long long
#define ld long double
#define f first
#define s second
#define mp make_pair
#define pb push_back
#define vll vector<ll>
#define fastio ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL)
using namespace std;
const ll N = 5005;
const ll MOD = 1e9+7;
vll adj[N];
vll adj2[N];
ll dp[N];
bool vis[N];
void dfs(ll cur,ll par)
{
dp[cur]=0;
vis[cur]=true;
for(auto v:adj[cur])
{
if(v==par) continue;
if(vis[v])continue;
dfs(v,cur);
dp[cur]+=dp[v];
}
dp[cur]+=1;
}
int main()
{
fastio;
ll n;
cin >> n;
bool poss=true;
ll src;
for(int i =1;i<=n;i++)
{
ll k;
cin >> k;
if(k==0)
{
src=i;
poss=false;
}
for(int j =1;j<=k;j++)
{
ll x;
cin >> x;
adj[x].pb(i);
}
}
if(!poss)
{
dfs(src,-1);
ll sum=0;
for(int i =1;i<=n;i++) sum+=dp[i];
cout<<sum<<endl;
return 0;
}
ll ans = LLONG_MAX;
for(int i=1;i<=n;i++)
{
for(int j =1;j<=n;j++) dp[j] =0,vis[j]=false;
dfs(i,-1);
ll co=0;
for(int j =1;j<=n;j++)
{
if(vis[j])co++;
}
if(co==n)
{
//cout << i << " " << dp[i] << endl;
ll su=0;
for(int j =1;j<=n;j++)su+=dp[j];
ans=min(ans,su);
}
}
cout << ans << endl;
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... |