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 int long long
#define F first
#define S second
#define pii pair<int,int>
#define mpr make_pair
typedef long long ll;
const int maxn = 5010;
const int mod = 1e9+7;
const int inf = 1e18+10;
int n, m;
vector<int> g[maxn];
bool mark[maxn];
int dis[maxn];
int bfs(int s)
{
memset(mark, 0, sizeof mark);
memset(dis, 0, sizeof dis);
queue<int> q;
q.push(s); dis[s] = 1; mark[s] = 1;
int ans = 0;
while(q.size())
{
int v = q.front();
q.pop();
ans += dis[v];
for(auto u : g[v])
if(!mark[u])
{
mark[u] = 1;
dis[u] = dis[v] + 1;
q.push(u);
}
}
for(int i = 1; i <= n; i++) if(!mark[i]) return inf;
return ans;
}
signed main()
{
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin>> n;
for(int i = 1, sz; i <= n; i++)
{
cin>> sz;
while(sz--)
{
int v; cin>> v;
g[v].push_back(i);
}
}
int ans = inf;
for(int i = 1; i <= n; i++)
ans = min(ans,bfs(i));
cout<< ans;
}
/*
4
1 4
3 1 3 4
2 1 2
1 3
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |