이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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<int>childs[5000];
int pay[5000];
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);
for(int i=0;i<n;i++)
childs[i].clear();
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
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |