# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
42386 | Hassoony | Bosses (BOI16_bosses) | C++11 | 2 ms | 612 KiB |
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>
#include<unordered_map>
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef long double D;
const ll inf=(1ll<<61);
const ll mod=1e9+7;
const int MX=5002;
int n,k,x;
bool vis[MX];
int val[MX],par[MX],in[MX],vis1[MX];
vector<int>v[MX],v1[MX];
int ans,mn=1e9;
void dfs(int x){
if(v1[x].size()==0){
val[x]=1;
ans++;
vis[x]=1;
return;
}
if(vis[x])return;
vis[x]=1;
int &ret=val[x];
for(auto pp:v1[x]){
if(vis[pp])continue;
dfs(pp);
ret+=val[pp];
}
++ret;
ans+=ret;
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&k);
while(k--){
scanf("%d",&x);
v[x].push_back(i);
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++)vis[j]=0;
queue<int>q;
q.push(i);
while(!q.empty()){
int x=q.front();q.pop();
vis[x]=1;
for(auto pp:v[x]){
if(vis[pp])continue;
par[pp]=x;
in[x]=1;
vis[pp]=1;
q.push(pp);
}
}
bool b=1;
for(int j=1;j<=n;j++){
if(!vis[j])b=0;
}
if(!b)continue;
for(int j=1;j<=n;j++){
if(!in[j])q.push(j),val[j]=1,vis[j]=1,vis1[j]=1;
else val[j]=0,vis[j]=0;
vis1[j]=0;
}
if(q.empty())continue;
while(!q.empty()){
int x=q.front();q.pop();
val[par[x]]+=val[x];
if(!vis[par[x]]){
q.push(par[x]);
if(!vis1[par[x]])val[par[x]]++;
vis1[par[x]]=1;
vis[par[x]]=1;
}
}
ans=0;
for(int j=1;j<=n;j++)ans+=val[j];
mn=min(mn,ans);
}
printf("%d\n",mn);
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |