이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define END "\n"
#define rall(v) (v).rbegin(), (v).rend()
#define all(v) (v).begin(), (v).end()
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <typename T>
using ordered_set =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
void solve(){
int n;
cin >> n;
vector<vector<int>> adj(n + 1);
for (int i = 1; i <= n; ++i) {
int k;
cin >> k;
while (k--) {
int a;
cin >> a;
adj[a].push_back(i);
}
}
ll ans = LLONG_MAX;
for (int s = 1; s <= n; ++s) {
queue<int> q;
vector<bool> vis(n + 1);
vector<int> dist(n + 1);
dist[s] = 1;
vis[s] = 1;
q.push(s);
int tot = 0;
ll sum = 0;
while (!q.empty()) {
++tot;
int v = q.front();
sum += dist[v];
q.pop();
for (auto u: adj[v]) {
if (!vis[u]) vis[u] = 1, dist[u] = dist[v] + 1, q.push(u);
}
}
if (tot == n) ans = min(ans, sum);
}
cout << ans << END;
}
int main()
{
fastio
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... |