이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <queue>
#include <vector>
#define vi vector<int>
#define pii pair<int, int>
#define fst first
#define snd second
const int INF = 1e9;
using namespace std;
int n, dst[5001];
queue<int> q;
int res = INF;
vi adj[5001];
inline int retAns(int s)
{
int ans = 1;
for (int i = 0; i < n; i++) {dst[i] = INF;}
q.push(s); dst[s] = 1;
while (q.size())
{
int u = q.front(); q.pop();
for (auto v : adj[u])
{
if (dst[v] == INF)
{
dst[v] = dst[u] + 1;
ans += dst[v];
q.push(v);
}
}
}
for (int i = 0; i < n; i++)
{
if (dst[i] == INF) return INF;
}
return ans;
}
int main()
{
cin >> n;
for (int i = 0; i < n; i++)
{
int k; cin >> k;
for (int j = 0; j < k; j++)
{
int x; cin >> x;
adj[x - 1].push_back(i);
}
}
for (int i = 0; i < n; i++)
{
res = min(res, retAns(i));
}
cout << res << "\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... |