이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define ll long long int
const ll N = 5000+5 , INF = 1e18 , MOD = 1e9+7;
vector<ll> ssize(N);
vector<ll> adj2[N];
void dfs(ll s){
ssize[s] = 1;
for(auto u : adj2[s]){
dfs(u);
ssize[s] += ssize[u];
}
}
void solve(){
ll n;
cin >> n;
vector<ll> adj1[N];
for(ll i = 1; i <= n; i++){
ll x;
cin >> x;
while(x--){
ll a;
cin >> a;
adj1[a].push_back(i);
}
}
ll ans = INF;
for(ll root = 1; root <= n; root++){
vector<ll> depth(n+5,-1);
for(ll i = 1; i <= n; i++) adj2[i].clear();
depth[root] = 1;
queue<ll> q;
q.push(root);
ll cnt = 1;
while(q.size()){
ll a = q.front();
q.pop();
for(auto u : adj1[a]){
if(depth[u] == -1){
depth[u] = depth[a]+1;
q.push(u);
adj2[a].push_back(u);
cnt++;
}
}
}
ll cost = 0;
dfs(root);
for(ll i = 1; i <= n; i++) cost += ssize[i];
if(cnt == n) ans = min(ans,cost);
}
cout << ans;
}
int main(){
fast;
ll tc = 1;
// cin >> tc;
while(tc--) 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... |