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;
const int N = 5e3 + 5;
int cnt = 0, c[N], temp;
bool visited[N];
vector<int> adj[N], vec[N];
void bfs(int node)
{
queue<int> q;
q.push(node);
visited[node] = true;
cnt = 1;
while(!q.empty())
{
int node = q.front();
q.pop();
for(auto to : adj[node])
{
if(!visited[to])
{
cnt++;
visited[to] = true;
q.push(to);
vec[node].push_back(to);
}
}
}
}
void dfs(int node, int par)
{
for(auto to : vec[node])
{
if(to == par)
continue;
dfs(to, node);
c[node] += c[to];
}
c[node]++;
temp += c[node];
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// #ifndef ONLINE_JUDGE
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
// #endif
int n, cevap = 1e9;
cin >> n;
for(int i = 1; i <= n; i++)
{
int k;
cin >> k;
for(int j = 0; j < k; j++)
{
int b;
cin >> b;
adj[b].push_back(i);
}
}
for(int i = 1; i <= n; i++)
{
for(int i = 1; i <= n; i++)
vec[i].clear(), visited[i] = false, c[i] = 0;
cnt = 0;
bfs(i);
if(cnt == n)
{
temp = 0;
dfs(i, 0);
cevap = min(cevap, temp);
}
}
cout << cevap << '\n';
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... |