1. 程式人生 > >The Bottom of a Graph

The Bottom of a Graph

ive limit rtai assume ted can hab spa mean

               poj——The Bottom of a Graph
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 11044 Accepted: 4542

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. ThenG=(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+1is 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

Source

Ulm Local 2003
定義:點v是匯點須滿足 --- 對圖中任意點u,若v可以到達u則必有u到v的路徑;若v不可以到達u,則u到v的路徑可有可無。
題目大意:
如果v點能夠到的點,反過來能夠到達v點,則稱這個點為sink點,輸出所有的sink點 思路: 我們這樣想:對於一對點如果他們能夠相互到達,那麽他們在縮點是一定能縮成一個點,這樣我們剩下的就是不能縮點的情況了。 也就是說我們縮完點後就剩下兩種情況:1.這個點連向其它的點但是那個點不連向他 2.這個點不連向任意點 在這兩種情況中第二種情況對於sink點的要求是顯然成立的。而第一種情況是不成立的,因為這種情況一定是另一個點不連向這個點的,也就是說她是單向的。 (最開始沒明白的地方。。。) 我們為何要在判斷一個點的出度為0時循環到n?? 因為我們把這些點縮成一個點,這樣我們在進行判斷每一個點的時候有兩種選擇: 1.我們先將出度為零的強連通分量記錄在一個數組中。然後再用一個雙重循環來判斷一下那個點在這個數組中 2.我們用一個一重循環,循環到n,判斷這個點所屬的強連通分量縮點後對應的點的出度是否為0,若是0,用一個數組將其儲存下來。 排序 輸出這個數組內的元素 代碼:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 10000
using namespace std;
bool vis[N];
int n,m,x,y,sum,tim,tot,top;
int out[N],dfn[N],low[N],ans[N],head[N],stack[N],belong[N],point[N];
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<0||ch>9) {if(ch==-) f=-1;ch=getchar();}
    while(ch<=9&&ch>=0) {x=x*10+ch-0;ch=getchar();}
    return f*x;
}
struct Edge
{
    int from,to,next;
 }edge[500010];
void add(int x,int y)
{
    tot++;
    edge[tot].to=y;
    edge[tot].next=head[x];
    head[x]=tot;
} 
void begin()
{
    tot=0;top=0;sum=0,tim=0;
    memset(edge,0,sizeof(edge));
    memset(stack,0,sizeof(stack));
    memset(head,0,sizeof(head));
    memset(out,0,sizeof(out));
    memset(dfn,0,sizeof(dfn));
    memset(low,0,sizeof(low));
    memset(belong,0,sizeof(belong));
    memset(ans,0,sizeof(ans));
}
int tarjan(int now)
{
    dfn[now]=low[now]=++tim;
    stack[++top]=now;vis[now]=true;
    for(int i=head[now];i;i=edge[i].next)
    {
        int t=edge[i].to;
        if(vis[t]) low[now]=min(low[now],dfn[t]);
        else if(!dfn[t]) tarjan(t),low[now]=min(low[now],low[t]);
    }
    if(dfn[now]==low[now])
    {
        sum++;belong[now]=sum;
        for(;stack[top]!=now;top--)
        {
            vis[stack[top]]=false;
            belong[stack[top]]=sum;
            }    
        vis[now]=false;top--;
    }
 } 
void shrink_point()
{
    for(int i=1;i<=n;i++)
     for(int j=head[i];j;j=edge[j].next)
      if(belong[i]!=belong[edge[j].to])
        out[belong[i]]++;
}
int main()
{
    while(~scanf("%d",&n)&&n)
    {
        m=read();begin();
        for(int i=1;i<=m;i++)
            x=read(),y=read(),add(x,y);
        for(int i=1;i<=n;i++)
          if(!dfn[i]) tarjan(i);
        shrink_point();
        x=0;
        for(int i=1;i<=n;i++)
          if(!out[belong[i]]) ans[++x]=i;
        sort(ans+1,ans+1+x);
        if(x) 
        {
            for(int i=1;i<x;i++)
              printf("%d ",ans[i]);    
            printf("%d\n",ans[x]);
        }
        else printf("\n");
    }
    return 0;    
}

註意:註意數組的大小!!

The Bottom of a Graph