http://www.byvoid.com/blog/scc-tarjan/zh-hans/#comment-24176
http://blog.youkuaiyun.com/shiqi_614/article/details/7833628
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define FileIn freopen("in.ads","r",stdin)
#define FileOut freopen("out.ads","w",stdout)
#define N 155
#define M 22555
struct Vertex
{
int head;
}V[N];
struct Edge
{
int v,next;
}E[M];
int etop,top,index,dfn[N],low[N],S[N];
bool in[N];
void Init()
{
etop = top = index = 0;
memset(V,-1,sizeof(V));
memset(dfn,0,sizeof(dfn));
memset(in,false,sizeof(in));
}
void Add_Edge(int u,int v)
{
E[etop].v = v;
E[etop].next = V[u].head;
V[u].head = etop++;
}
void Tarjan(int u)
{
int v;
dfn[u] = low[u] = ++index;
in[u] = true;
S[++top] = u;
for(int i=V[u].head;i!=-1;i=E[i].next)
{
v = E[i].v;
if(!dfn[v])
{
Tarjan(v);
low[u] = min(low[u],low[v]);
}
else
if(in[v])
low[u] = min(low[u],dfn[v]);
}
if(dfn[u] == low[u])
{
puts("QiangLianTong:");
do
{
v = S[top--];
in[v] = false;
printf("%d ",v);
}while(u != v);
puts("\n***********\n");
}
}
int main()
{
FileIn;
int n,m,u,v;
while(~scanf("%d%d",&n,&m))
{
Init();
while(m--)
{
scanf("%d%d",&u,&v);
Add_Edge(u,v);
}
Tarjan(1);
}
return 0;
}