• POJ 2553 The Bottom of a Graph(Tarjan)


    题意:

    给定有向图,找出图中所有满足条件的点 v,保证所有从 v 出发的都能回到 v。 

    思路:

    1. Tarjan + 缩点:缩点之后统计出所有出度为 0 的点;

    2. 于是这个出度为 0 的强连通分量的所有点都是满足题意的点。

    #include <iostream>
    #include <vector>
    #include <stack>
    #include <algorithm>
    using namespace std;
    
    const int MAXN = 5010;
    vector<int> G[MAXN];
    stack<int> S;
    int dfn[MAXN], low[MAXN], sccno[MAXN], sccnum, tclock;
    int indeg[MAXN], outdeg[MAXN];
    
    void tarjan(int u) {
        dfn[u] = low[u] = ++tclock;
        S.push(u);
    
        for (int i = 0; i < G[u].size(); i++) {
            int v = G[u][i];
            if (!dfn[v]) {
                tarjan(v);
                low[u] = min(low[u], low[v]);
            } else if (!sccno[v]) {
                low[u] = min(low[u], dfn[v]);
            }
        }
    
        if (dfn[u] == low[u]) {
            sccnum += 1;
            int v = -1;
            while (v != u) {
                v = S.top();
                S.pop();
                sccno[v] = sccnum;
            }
        }
    }
    
    void findscc(int n) {
        for (int i = 0; i <= n; i++)
            dfn[i] = low[i] = sccno[i] = 0;
        sccnum = tclock = 0;
        for (int i = 1; i <= n; i++)
            if (!dfn[i]) tarjan(i);
    }
    
    int main() {
        int n, e;
        while (scanf("%d", &n) && n) {
            scanf("%d", &e);
            for (int i = 1; i <= n; i++)
                G[i].clear();
            for (int i = 0; i < e; i++) {
                int u, v;
                scanf("%d%d", &u, &v);
                G[u].push_back(v);
            }
            findscc(n);
            for (int i = 1; i <= n; i++)
                indeg[i] = outdeg[i] = 0;
            for (int u = 1; u <= n; u++) {
                for (int i = 0; i < G[u].size(); i++) {
                    int v = G[u][i];
                    if (sccno[u] != sccno[v]) {
                        indeg[sccno[v]] += 1;
                        outdeg[sccno[u]] += 1;
                    }
                }
            }
            for (int i = 1; i <= n; i++) {
                if (!outdeg[sccno[i]]) 
                    printf("%d ", i);
            }
            printf("\n");
        }
        return 0;
    }
    -------------------------------------------------------

    kedebug

    Department of Computer Science and Engineering,

    Shanghai Jiao Tong University

    E-mail: kedebug0@gmail.com

    GitHub: http://github.com/kedebug

    -------------------------------------------------------

  • 相关阅读:
    压力测试工具集合(ab,webbench,Siege,http_load,Web Application Stress)
    微软压力测试工具 web application stress
    linux下的3种DDOS软件介绍
    windows 配置squid反向代理服务器
    windows下简单配置squid反向代理服务器
    [分享]windows下编译squid的经验(转)
    在CentOS 5下安装中文五笔
    CentOS LVS安装配置
    CentOS4.5下LVS方案
    linux LVS (keepalived+ipvsadm)负载均衡搭建
  • 原文地址:https://www.cnblogs.com/kedebug/p/3071455.html
Copyright © 2020-2023  润新知