# POJ2553 The Bottom of a Graph 强连通 tarjan

#include<iostream>
#define min(a,b) (a<b?a:b)
using namespace std;
const int N=5005;
bool mat[N][N];
int dfn[N],low[N];
bool instack[N];
int indgr[N];
int outdgr[N];
int stack[N];
int sp;
int index;
int n,m;
int belong[N];
void tarjan(int i)
{
dfn[i]=low[i]=index++;
stack[sp++]=i;
instack[i]=true;
for(int j=1;j<=n;j++)
{
if(mat[i][j])
{
if(!dfn[j])
{
tarjan(j);
low[i]=min(low[i],low[j]);
}
else if(instack[j])
{
low[i]=min(dfn[j],low[i]);
}
}
}
if(low[i]==dfn[i])
{
int j;
do
{
j=stack[--sp];
instack[j]=false;
belong[j]=i;
}while(j!=i);
}
}
void solve()
{
for(int i=1;i<=n;i++)
{
if(!dfn[i])
{
tarjan(i);
}
}
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
if(mat[i][j])
{
if(belong[i]!=belong[j])
{
indgr[belong[j]]++;
outdgr[belong[i]]++;
}
}
}
for(int i=1;i<=n;i++)
{
if(outdgr[belong[i]]==0)
{
printf("%d ",i);
}
}
printf("\n");
}
int main()
{
while(scanf("%d%d",&n,&m),n!=0)
{
index=1;
sp=1;
memset(indgr,0,sizeof(indgr));
memset(outdgr,0,sizeof(outdgr));
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
memset(mat,0,sizeof(mat));
memset(instack,0,sizeof(instack));
memset(stack,0,sizeof(stack));
memset(belong,0,sizeof(belong));
int from,to;
for(int i=1;i<=m;i++)
{
scanf("%d%d",&from,&to);
mat[from][to]=true;
}
solve();
}
return 0;
}

#### 欧拉回路 欧拉路径某些问题集合

2017-10-11 16:31:46

#### scu图论专题题解

2016-06-03 00:11:30

#### POJ2762 Going from u to v or from v to u? 强连通+缩点

2015-07-09 11:54:40

#### POJ 2762 单向的连通图

2017-10-03 19:42:16

#### Kuangbin带你飞 专题四 最短路径 习题报告

2017-08-29 00:52:51

#### poj2553 The Bottom of a Graph【强连通】

2017-07-24 13:54:17

#### POJ2553 The Bottom of a Graph (Tarjan)

2013-09-14 12:23:14

#### poj2553 The Bottom of a Graph (Tarjan)

2015-01-21 16:09:17

#### POJ2553——The Bottom of a Graph

2014-10-03 17:33:28

#### poj2553——The Bottom of a Graph

2010-09-20 01:26:00