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 SZ(v) ((int)(v).size())
using namespace std;
using ll = long long;
template<typename... Args>
void read(Args&... args)
{
((cin >> args), ...);
}
template<typename T>
void read(vector<T> &vec)
{
for (auto &v : vec) read(v);
}
void write() {}
template<typename H, typename... T>
void write(const H &h, const T&... t)
{
cout << h;
if (sizeof...(t)) {cout << ' '; write(t...);}
}
template<typename T>
void write(const vector<T> &vec)
{
if (SZ(vec) == 0) return;
write(vec[0]);
for (int i(1); i < SZ(vec); ++i)
{cout << ' '; write(vec[i]);}
}
template<typename... Args>
void writeln(Args... args)
{
write(args...); cout << '\n';
}
int main(void)
{
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int nbEmployes;
read(nbEmployes);
vector<vector<int>> filsPossibles(nbEmployes);
for (int i(0); i < nbEmployes; ++i)
{
int nbParents;
read(nbParents);
for (int j(0); j < nbParents; ++j)
{
int p;
read(p);
p--;
filsPossibles[p].push_back(i);
}
}
ll sol(1e18);
for (int racine(0); racine < nbEmployes; ++racine)
{
queue<int> q;
vector<ll> dis(nbEmployes, 1e9);
dis[racine] = 1;
q.push(racine);
while (!q.empty())
{
int u = q.front(); q.pop();
for (auto v : filsPossibles[u])
if (dis[v] == 1e9)
{
q.push(v);
dis[v] = dis[u] + 1;
}
}
ll cost(0);
for (auto d : dis)
cost += d;
sol = min(sol, cost);
}
writeln(sol);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |