十分有趣的一道题目
Code:
#include<cstdio>
#include<stack>
#include<algorithm>
using namespace std;
const int N=200;
const int maxn=100000+2;
stack<int>S;
int head[N],to[maxn],nex[maxn];
int pre[N],vis[N],low[N],idx[N];
int deg_in[N],deg_out[N];
int scc,cnt,sig;
void add_edge(int u,int v)
{
nex[++cnt]=head[u],head[u]=cnt,to[cnt]=v;
}
void tarjan(int u)
{
vis[u]=1;
S.push(u);
low[u]=pre[u]= ++scc;
for(int v=head[u];v;v=nex[v])
if(!vis[to[v]])
{
tarjan(to[v]);low[u]=min(low[u],low[to[v]]);
}
else if(vis[to[v]]!=-1) low[u]=min(low[u],pre[to[v]]);
if(pre[u]==low[u])
{
++sig;
for(;;)
{
int a=S.top();S.pop();
vis[a]=-1,idx[a]=sig;
if(a==u)break;
}
}
}
int main()
{
//freopen("in.txt","r",stdin);
int n;scanf("%d",&n);
for(int i=1;i<=n;++i)
{
int a;
while(1)
{
scanf("%d",&a);if(!a)break;
add_edge(i,a);
}
}
for(int i=1;i<=n;++i)
if(!vis[i])tarjan(i);
for(int i=1;i<=n;++i)
for(int v=head[i];v;v=nex[v])
{
if(idx[i]!=idx[to[v]]){
deg_in[idx[to[v]]]=deg_out[idx[i]]=1;
}
}
int cc1=0,cc2=0;
for(int i=1;i<=sig;++i)
{
if(deg_in[i]==0)++cc1;
if(deg_out[i]==0)++cc2;
}
if(sig==1)printf("1
0");
else printf("%d
%d",cc1,max(cc1,cc2));
return 0;
}