• POJ 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.

    Sample Input

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

    Sample Output

    1 3
    2
    题目大意,一个点所能到达的任意一个点都能返回这个点,那么这个点称为bottom点,找出所有的底部点。
    找出所有的强连通子图,每一个子图看作一个点,若该点与其他点有路可达,在该店所代表的所有结点都不符合定义,及输出所有
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <cstdio>
    #include <vector>
    #include <queue>
    #include <stack>
    #include <cstdlib>
    #include <iomanip>
    #include <cmath>
    #include <cassert>
    #include <ctime>
    #include <map>
    #include <set>
    using namespace std;
    #pragma comment(linker, "/stck:1024000000,1024000000")
    #define lowbit(x) (x&(-x))
    #define max(x,y) (x>=y?x:y)
    #define min(x,y) (x<=y?x:y)
    #define MAX 100000000000000000
    #define MOD 1000000007
    #define pi acos(-1.0)
    #define ei exp(1)
    #define PI 3.1415926535897932384626433832
    #define ios() ios::sync_with_stdio(true)
    #define INF 0x3f3f3f3f
    #define mem(a) ((a,0,sizeof(a)))
    typedef long long ll;
    int pos[5006],vis[5006],low[5006],dnf[5006];
    int ans[5006],cnt[5006],degree[5006];
    int n,m,x,y,k,top,num,inf;
    vector<int>v[5006];
    void init()
    {
        k=0;
        top=0;
        num=0;
        for(int i=0;i<=n;i++)
            v[i].clear();
        memset(vis,0,sizeof(vis));
        memset(pos,0,sizeof(pos));
        memset(ans,0,sizeof(ans));
        memset(cnt,0,sizeof(cnt));
        memset(dnf,0,sizeof(dnf));
        memset(low,0,sizeof(low));
        memset(degree,0,sizeof(degree));
    }
    void tarjan(int u)
    {
        dnf[u]=low[u]=++k;
        pos[++top]=u;
        vis[u]=1;
        for(int i=0;i<v[u].size();i++)
        {
            if(!dnf[v[u][i]])
            {
                tarjan(v[u][i]);
                low[u]=min(low[u],low[v[u][i]]);
            }
            else if(vis[v[u][i]]) low[u]=min(low[u],dnf[v[u][i]]);
        }
        if(low[u]==dnf[u])
        {
            num++;
            while(1)
            {
                inf=pos[top--];
                ans[inf]=num;
                vis[inf]=0;
                if(inf==u) break;
            }
        }
    }
    void dfs(int u)
    {
        cnt[u]=ans[u];
        for(int i=0;i<v[u].size();i++)
        {
            if(ans[u]!=ans[v[u][i]]) degree[ans[u]]++;
            if(!cnt[v[u][i]]) dfs(v[u][i]);
        }
    }
    void solve()
    {
        for(int i=1;i<=n;i++)
            if(!dnf[i]) tarjan(i);
        for(int i=1;i<=n;i++)
            if(!cnt[i]) dfs(i);
        bool flag=1;
        for(int i=1;i<=n;i++)
        {
            if(!degree[ans[i]])
            {
                if(flag) {printf("%d",i);flag^=1;}
                else printf(" %d",i);
            }
        }
        printf("
    ");
    }
    int main()
    {
        while(scanf("%d",&n) && n)
        {
            scanf("%d",&m);
            init();
            for(int i=0;i<m;i++)
            {
                scanf("%d%d",&x,&y);
                v[x].push_back(y);
            }
            solve();
        }
        return 0;
    }
    出度为0的连通子图所包含的点
  • 相关阅读:
    【1】BIO与NIO、AIO的区别
    BIO | NIO | AIO (Java版)
    Java NIO 机制分析(一) Java IO的演进
    AbstractQueuedSynchronizer同步队列与Condition等待队列协同机制
    【1】【JUC】Condition和生产者消费者模型
    【JUC】JDK1.8源码分析之CyclicBarrier
    常用排序,查找,树算法集锦
    AFNetworking
    ios推送服务,php服务端
    如何使用subversion管理iOS源代码
  • 原文地址:https://www.cnblogs.com/shinianhuanniyijuhaojiubujian/p/8991885.html
Copyright © 2020-2023  润新知