天天看點

#HDU 2553 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.

#HDU 2553 The Bottom of a Graph (強連通算法)

Sample Input

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

Sample Output

1 3
2
           

題目大意 : 多樣例輸入一個有向圖,對他的底部排序并輸出。

思路 : 題目意思就是讓你求縮點後入度為0的點。是以首先把tarjan敲上去,把縮點後的點給标記一下(縮點可以将所有有環的子圖變成一個點),然後周遊每一條邊,如果一個點所連的那個點縮點後和他不在一個集合當中,另他所連接配接的那個點入度 + 1, 最後周遊縮點後的點,如果入度為0,則将他儲存,排序輸出。

因為忘記排序WA了一次。。。

AC代碼 : 

#include<iostream>
#include<algorithm>
#include<cstring>
#include<stack>
using namespace std;
const int maxn = 1e5 + 5;

struct node
{
    int v, next;
}e[maxn];
int dfn[maxn], low[maxn], n, m, tot, ans;
int head[maxn], suo[maxn], in[maxn], cnt;
int pre[maxn], X;
bool vis[maxn];
stack <int> st;
void add (int from, int to) {
    e[++cnt].v = to;
    e[cnt].next = head[from];
    head[from] = cnt;
}
void init() {
    memset(e, 0, sizeof(e));
    memset(head, -1, sizeof(head));
    memset(in, 0, sizeof(in));
    memset(suo, 0, sizeof(suo));
    memset(vis, 0, sizeof(vis));
    memset(dfn, 0, sizeof(dfn));
    memset(low, 0, sizeof(low));
    cnt =  tot = ans = X = 0;
}
void tarjan (int u) {
    dfn[u] = low[u] = ++tot;
    vis[u] = 1;
    st.push(u);
    for (int i = head[u]; i != -1; i = e[i].next) {
        if (!dfn[e[i].v]) {
            tarjan (e[i].v);
            low[u] = min (low[u], low[e[i].v]);
        }
        else if (vis[e[i].v]) low[u] = min (low[u], dfn[e[i].v]);
    }
    if (dfn[u] == low[u]) {
        ans++; // 标記縮點
        int k;
        do {
            k = st.top();
            st.pop();
            vis[k] = 0;
            suo[k] = ans;   //suo裡的值相同代表他們處于同一個集合中
        }
        while (k != u);
    }
}

int main()
{
    while (cin >> n && n) {
        cin >> m;
        init();
        for (int i = 0; i < m; i++) {
            int ui, vi;
            cin >> ui >> vi;
            add (ui, vi);
        }
        for (int i = 1; i <= n; i++) {
            if (!dfn[i]) tarjan(i);
        }
        for (int i = 1; i <= n; i++) {
            for (int j = head[i]; j != -1 ; j = e[j].next) {
                if (suo[i] != suo[e[j].v]) in[suo[i]]++;  //入度更新
            }
        }
        for (int i = 1; i <= ans; i++) {
            if (!in[i]) {
                for (int j = 1; j <= n; j++) {
                    if (i == suo[j]) pre[X++] = j;
                }
            }
        }
        sort (pre, pre + X);
        for (int i = 0; i < X; i++) cout << pre[i] << " ";
        cout << endl;
    }
    return 0;
}
           

繼續閱讀