【連通圖|強連通+縮點】POJ-2553 The Bottom of a Graph

The Bottom of a Graph
Time Limit: 3000MS   Memory Limit: 65536K
     

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 vv 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
————————————————————————————————————————————————————————————————————————————————————————————————
題意:給出一個圖,求出圖上所有”自己可達的頂點都能回到自己“的點。
思路:對於一個強連通圖來說,所有點兩兩可達,因此強連通圖全部都是要求的點。
如果不是強連通圖:那麼強連通分量和強連通分量之間的關係呢?
縮點之後,所有出度不爲0的點意味着該點可達另一點但是另一點不可達該點。對於出度爲0的點,因爲它不可達另外的點,所以它是滿足”自己可達的頂點都能回到自己“這一性質的。所以該點所代表的強連通分量的所有點都是要求的點。
P.S. 注意!因爲標號是從0開始的,然而後來的縮點卻是從1開始的,因此初始化的時候for循環從0到n肯定是不對的。WA了一晚上竟然都沒有發現。要適當修改模板了
代碼如下:
/*
 * ID: j.sure.1
 * PROG:
 * LANG: C++
 */
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <climits>
#include <iostream>
#define Mem(f, x) memset(f, x, sizeof(f))
#define PB push_back
#define LL long long
using namespace std;
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
/****************************************/
const int N = 5555, M = 55555;
int n, m;
struct Edge {
    int v, next;
    Edge(){}
    Edge(int _v, int _next):
        v(_v), next(_next){}
}e[M];
int tot, deep, scc_cnt;
int out[N], dfn[N], scc_id[N], line[M][2], head[N];
stack <int> s;

void __init__()
{
    Mem(head, -1);
    Mem(out, 0);
    Mem(dfn, 0);
    Mem(scc_id, 0);
    tot = deep = scc_cnt = 0;
}

void add(int u, int v)
{
    e[tot] = Edge(v, head[u]);
    head[u] = tot++;
}

int dfs(int u)
{
    int lowu = dfn[u] = ++deep;
    s.push(u);
    for(int i = head[u]; ~i; i = e[i].next) {
        int v = e[i].v;
        if(!dfn[v]) {
            int lowv = dfs(v);
            lowu = min(lowu, lowv);
        }
        else if(!scc_id[v]) {
            lowu = min(lowu, dfn[v]);
        }
    }
    if(lowu == dfn[u]) {
        scc_cnt++;
        while(1) {
            int x = s.top(); s.pop();
            scc_id[x] = scc_cnt;
            if(x == u) break;
        }
    }
    return lowu;
}

int main()
{
#ifdef J_Sure
    freopen("000.in", "r", stdin);
    //freopen("999.out", "w", stdout);
#endif
    while(scanf("%d", &n), n) {
        scanf("%d", &m);
        int u, v;
        __init__();
        for(int i = 0; i < m; i++) {
            scanf("%d%d", &u, &v);
            u--; v--;
            add(u, v);
            line[i][0] = u; line[i][1] = v;
        }
        for(int i = 0; i < n; i++) {
            if(!dfn[i]) dfs(i);
        }
        for(int i = 0; i < m; i++) {
            int u = scc_id[line[i][0]], v = scc_id[line[i][1]];
            if(u != v) out[u]++;
        }
        bool fir = false;
        for(int i = 0; i < n; i++) {
            if(!out[scc_id[i]]) {
                if(fir) printf(" ");
                printf("%d", i+1); fir = true;
            }
        }
        puts("");
    }
    return 0;
}


發佈了437 篇原創文章 · 獲贊 20 · 訪問量 46萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章