이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define int long long
#define vi vector<int>
#define ii pair<int, int>
#define f first
#define s second
#define all(x) (x).begin(), (x).end()
#define P 31
#define mod 1'000'000'007
#define inf 1'000'000'000'000
#define pb push_back
#define str string
#define sz size
#define vvi vector<vi>
#define fun function
#define oopt cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(false);
#define file freopen("problemname.in", "r", stdin); freopen("pr.out", "w", stdout);
#define dbg(v) cout << "Line(" << __LINE__ << ") -> " << #v << " = " << (v) << endl;
using namespace std;
template <class T, int SZ> using arr = array<T, SZ>;
int ob[5001];
int par[5001];
int cn[5001];
vi graf[5001];
void solve()
{
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
int k;
cin >> k;
for (int j = 0; j < k; j++)
{
int p;
cin >> p;
graf[p-1].pb(i);
}
}
int tob = 0;
auto bfs = [&tob, &n](int& kor) -> int
{
tob++;
queue<int> q;
q.push(kor);
ob[kor] = tob;
par[kor] = -1;
cn[kor] = 1;
while (q.sz())
{
int t = q.front();
q.pop();
bool list = true;
for (int&v:graf[t])
if (ob[v] != tob)
{
list = false;
ob[v] = tob;
cn[v] = 1;
par[v] = t;
q.push(v);
}
if (list)
{
while (par[t]+1)
{
cn[par[t]] += cn[t];
// cout << t << " " << cn[t] << " " << par[t] << " " << cn[par[t]] << "\n";
t = par[t];
}
}
}
int rez = 0;
for (int i = 0; i < n; i++)
{
if (ob[i] != tob) return inf;
rez += cn[i];
}
// cout << kor << " " << rez << "\n";
return rez;
};
int rez = inf;
for (int i = 0; i < n; i++)
rez = min(rez, bfs(i));
cout << rez << "\n";
}
signed main()
{
oopt;
int t = 1;
// cin >> t;
while (t--)
solve();
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... |