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 int long long
#define pii pair<int,int>
#define fi first
#define se second
using namespace std;
using ll = long long;
using ldb = long double;
const int N = (int)1e8;
const int maxN = (int)5e3 + 5;
const int mod = 1e9 + 7;
void InputFile()
{
//freopen("scrivener.inp","r",stdin);
//freopen("scrivener.out","w",stdout);
//freopen("test.out","r",stdin);
}
void FastInput()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
}
int n;
vector<vector<int>> adj1;
vector<int> adj2[5005];
int res = mod;
int check = 0;
void Read()
{
cin >> n;
adj1.resize(n+1);
for(int i = 1;i <= n;i++)
{
int k;
cin >> k;
for(int j = 1;j <= k;j++)
{
int t;
cin >> t;
adj1[t].push_back(i);
}
}
}
int dp[maxN];
int su[maxN];
void DFS(int u)
{
su[u] = 1;
for(int v : adj2[u])
{
DFS(v);
su[u] += su[v];
}
check += su[u];
}
void BFS(int sta)
{
for(int i = 1;i <= n;i++)
{
dp[i] = mod;
adj2[i].clear();
}
dp[sta] = 0;
queue<int> q;
q.push(sta);
do
{
int u = q.front();
q.pop();
for(int v : adj1[u])
{
if(dp[v] > dp[u] + 1)
{
dp[v] = dp[u] + 1;
adj2[u].push_back(v);
q.push(v);
}
}
}
while(!q.empty());
for(int i = 1;i <= n;i++)
{
if(dp[i] >= mod) return;
}
check = 0;
DFS(sta);
res = min(res,check);
}
void Solve()
{
for(int i = 1;i <= n;i++)
BFS(i);
cout << res;
}
void Debug()
{
//Main_sub();
//Naive();
}
int32_t main()
{
FastInput();
//InputFile();
//int sub_type;
//cin >> sub_type;
//Sieve();
int test;
//cin >> test;
test = 1;
while(test--)
//for(int prc = 1; prc <= test; prc++)
{
Read();
Solve();
//Debug();
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |