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 int long long
#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(),x.end()
using namespace std;
const int N=5e3+5;
vector <int> g[N];
vector <int> g2[N];
int sum[N];
int t=0;
void dfs(int v,int p){
for(auto to : g2[v]){
if(to!=p){
dfs(to,v);
}
}
sum[v]++;
sum[p]+=sum[v];
t+=sum[v];
}
signed main(){
ios_base::sync_with_stdio();
cin.tie(0);cout.tie(0);
int n;
cin>>n;
for(int i=1;i<=n;i++){
int k;
cin>>k;
while(k--){
int x;
cin>>x;
g[x].pb(i);
}
}
int mn=1e18;
queue <int> q;
for(int i=1;i<=n;i++){
q.push(i);
for(int j=1;j<=n;j++){
g2[j].clear();
sum[j]=0;
}
t=0;
vector <int> vis(n+1);
vis[i]=1;
while(!q.empty()){
int v=q.front();
q.pop();
for(auto to : g[v]){
if(!vis[to]){
vis[to]=1;
g2[v].pb(to);
q.push(to);
}
}
}
bool ok=1;
for(int j=1;j<=n;j++){
if(!vis[j]){ok=0;break;}
}
if(ok){
dfs(i,0);
mn=min(mn,t);
}
}
cout<<mn<<"\n";
}
/*
4
1 4
3 1 3 4
2 1 2
1 3
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |