The Bottom of a Graph

Description

We will use the following (standard) definitions from graph theory. Let V be a nonempty and finite set, its elements being called vertices (or nodes). Let E be a subset of the Cartesian product V×V, its elements being called edges. Then G=(V,E) is called a directed graph.
Let n be a positive integer, and let p=(e1,...,en) be a sequence of length n of edges ei∈E such that ei=(vi,vi+1) for a sequence of vertices (v1,...,vn+1). Then p is called a path from vertex v1 to vertex vn+1 in G and we say that vn+1 is reachable from v1, writing (v1→vn+1).
Here are some new definitions. A node v in a graph G=(V,E) is called a sink, if for every node w in G that is reachable from v, v is also reachable from w. The bottom of a graph is the subset of all nodes that are sinks, i.e., bottom(G)={v∈V|∀w∈V:(v→w)⇒(w→v)}. You have to calculate the bottom of certain graphs.

Input

The input contains several test cases, each of which corresponds to a directed graph G. Each test case starts with an integer number v, denoting the number of vertices of G=(V,E), where the vertices will be identified by the integer numbers in the set V={1,...,v}. You may assume that 1<=v<=5000. That is followed by a non-negative integer e and, thereafter, e pairs of vertex identifiers v1,w1,...,ve,we with the meaning that (vi,wi)∈E. There are no edges other than specified by these pairs. The last test case is followed by a zero.

Output

For each test case output the bottom of the specified graph on a single line. To this end, print the numbers of all nodes that are sinks in sorted order separated by a single space character. If the bottom is empty, print an empty line.

Sample Input

3 3
1 3 2 3 3 1
2 1
1 2
0

Sample Output

1 3
2
题意:求出强连通分量,判断每个分量是否有出边。按序输出没出边的分量里的点。
代码:
#include <stdio.h>
#include 
<stdlib.h>
#define Min(a, b) a < b ? a : b
#define maxn 5001
struct T
{
    
int v, next;
}fn[maxn 
* maxn];
int g[maxn], visit[maxn], low[maxn], dfn[maxn], stack[maxn], f[maxn], flag[maxn];
int top, id;
void set(int n)
{
    
for (int i = 1; i <= n; i++)
    {
        g[i] 
= -1, visit[i] = 0, f[i] = 0, flag[i] = 0;
    }
}
int dfs(int u, int t)
{
    
int i;
    visit[u] 
= 1;
    stack[top
++= u;
    dfn[u] 
= low[u] = t;
    
for (i = g[u]; i != -1; i = fn[i].next)
    {
        
if (!visit[fn[i].v])
        {
            t 
= dfs(fn[i].v, t + 1);//返回遍历完u的子树的时间 
            low[u] = Min(low[u], low[fn[i].v]);//如果有孩子因为后向边被缩小了low值,则它也相应缩小low值,整个分量的low都取分量中low最小的值。
        }
        
else
        {
            
if (!f[fn[i].v])//如果f[v]还是0,表示u->v是后向边,v还没出过栈过
            {
                low[u] 
= Min(low[u], low[fn[i].v]);
            }
        }
    }
    
if (dfn[u] == low[u])
    {
        id
++;
        
do
        {
            f[stack[
--top]] = id; 
        }
while (top > 0 && stack[top] != u);
    }
    
return t;
}
void tarjan(int n)
{
    
int t = 0;
    id 
= 0;
    
int i;
    
for (i = 1; i <= n; i++)
    {
        
if (!visit[i])
        {
            
//如果i点能与其它点x构成一个分量,则一定能搜到x,否则,它自己便是一个分量,所以这里的搜索不会重复。
            t = dfs(i, t + 1);
        }
    }
}
int main()
{
    
int n, m, u, v, th, k, i, j;
    
while (scanf("%d"&n), n)
    {
        scanf(
"%d"&m);
        th 
=  0;
        
set(n);
        
while (m--)
        {
            scanf(
"%d%d"&u, &v);
            fn[th].v 
= v, fn[th].next = g[u], g[u] = th++;
        }
        tarjan(n);
        
for (i = 1; i <= n; i++)
        { 
            
for (j = g[i]; j != -1; j = fn[j].next)
            {
                
if (f[fn[j].v] != f[i])//两个点的所在分量不同,表示i有出边 
                {
                    
//hash[i] = 1;
                    flag[f[i]] = 1;
                    
break;
                }
            }
        }
        
for (i = 1; i <= n; i++)
        {
            
if (!flag[f[i]])
            {
                
if (i != n)
                {
                    printf(
"%d ", i);
                }
                
else
                {
                    printf(
"%d", i);
                }
            }
        }
        printf(
"\n");
    }
    system(
"pause");
    
return 0;
}
/*
6 8
1 3
1 2
3 5
5 6
2 4
4 6
3 4
4 1

4 5
1 2
2 3
3 1
1 3
3 4

*/