#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#include<limits.h>
#include<math.h>
#include<map>
#include<set>
#include<unordered_map>
#include<unordered_set>
#include<iomanip>
#include<cstring>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
using namespace std;
//const int MOD=1e9+7;
//typedef pair<ll,ll>Point;
//typedef pair<ll,ll>Line;
//#define x first
//#define y second
int n;
vector<int>children[5000];
vector<vector<int>>childs;
vector<int>pay;
void dfs(int node){
pay[node]=1;
for(int ch:childs[node]){
dfs(ch);
pay[node]+=pay[ch];
}
}
int get(int root){
vector<bool>vis(n);
childs=vector<vector<int>>(n,vector<int>());
pay=vector<int>(n);
vector<int>level1,level2;
level1.push_back(root);
vis[root]=true;
while(!level1.empty()){
for(int i:level1){
for(int j:children[i]){
if(!vis[j]){
level2.push_back(j);
vis[j]=true;
childs[i].push_back(j);
}
}
}
level1=level2;
level2.clear();
}
for(int i=0;i<n;i++)
if(!vis[i])
return 1e9;
dfs(root);
int res=0;
for(int i=0;i<n;i++)
res+=pay[i];
return res;
}
void solve(){
cin>>n;
for(int i=0;i<n;i++){
int k;
cin>>k;
while(k--){
int c;
cin>>c;
children[c-1].push_back(i);
}
}
int res=1e9;
for(int i=0;i<n;i++){
res=min(res,get(i));
}
cout<<res<<"\n";
}
int main(){
ios::sync_with_stdio(false);cout.tie(NULL);cin.tie(NULL);
//freopen("bank.in","r",stdin);freopen("bank.out","w",stdout);
int t=1;//cin>>t;
while(t--)solve();
return 0;
}
/*
4
1 4
3 1 3 4
2 1 2
1 3
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
356 KB |
Output is correct |
8 |
Correct |
1 ms |
600 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
356 KB |
Output is correct |
8 |
Correct |
1 ms |
600 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
4 ms |
604 KB |
Output is correct |
13 |
Correct |
3 ms |
860 KB |
Output is correct |
14 |
Correct |
480 ms |
1192 KB |
Output is correct |
15 |
Correct |
88 ms |
1036 KB |
Output is correct |
16 |
Correct |
1186 ms |
1420 KB |
Output is correct |
17 |
Execution timed out |
1558 ms |
1680 KB |
Time limit exceeded |
18 |
Halted |
0 ms |
0 KB |
- |