<span style="font-size:14px;">#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
#define MAX 100001
#define MAXN 100001
struct point{int to,next;};
point a[MAX];
int N,last[MAX],tot=0,step=0,belong[MAX],stack[MAX],ans=0,tot1=0;
int DFN[MAX],LOW[MAX];//DFN[u]:节点u搜索的次序编号,LOW[u]:u或u的子树能够追溯到的最早的栈中节点的次序号<DFN>
bool instack[MAX],putout[MAX];
void add(int x,int y)
{
a[++tot].to=y;
a[tot].next=last[x];
last[x]=tot;
}
void tarjan(int now)
{
DFN[now]=LOW[now]=++step;
stack[++tot1]=now;
instack[now]=true;
for(int i=last[now];i;i=a[i].next)
{
int now_to=a[i].to;
if(!DFN[now_to])
{
tarjan(now_to);
LOW[now]=min(LOW[now],LOW[now_to]);
}
else if(instack[now_to])
{
LOW[now]=min(LOW[now],DFN[now_to]);
}
}
if(DFN[now]==LOW[now])
{
ans++;
int now_out;
do
{
now_out=stack[tot1--];
instack[now_out]=false;
belong[now_out]=ans;
}while(now!=now_out);
}
}
int main()
{
memset(instack,false,sizeof(instack));
memset(putout,false,sizeof(putout));
scanf("%d",&N);
for(int i=1;i<=N;i++)
for(int j=1;j<=N;j++)
{
int C;
scanf("%d",&C);
if(C==1) add(i,j);
}
for(int i=1;i<=N;i++)
if(!DFN[i])
tarjan(i);
printf("%d\n",ans);
for(int i=1;i<=N;i++)
{
if(putout[belong[i]]) continue;
putout[belong[i]]=true;
for(int j=i;j<=N;j++)
{
if(belong[j]==belong[i])
printf("%d ",j);
}
printf("\n");
}
//system("pause");
return 0;
}
—————————————————————————————————
本文原创自Slience的csdn技术博客。
本博客所有原创文章请以链接形式注明出处。
欢迎关注本技术博客,本博客的文章会不定期更新。
大多数人想要改造这个世界,但却罕有人想改造自己。
世上没有绝望的处境,只有对处境绝望的人。
————By slience
—————————————————————————————————