poj 1144 Network(割点)

题目链接: http://poj.org/problem?id=1144

思路分析:该问题要求求出无向联通图中的割点数目,使用Tarjan算法即可求出无向联通图中的所有的割点,算法复杂度为O(|V| + |E|);

代码如下:

#include <cstdio>
#include <vector>
#include <cstring>
#include <iostream>
using namespace std;

const int MAX_N = 100 + 10;
const int MAX_M = 200 + 20;
char str[MAX_M];
vector<int> G[MAX_N];
int pre[MAX_N], is_cut[MAX_N], low[MAX_N];
int dfs_clock;

inline int Min(int a, int b) { return a < b ? a : b; }
int Dfs(int u, int fa)
{
    int lowu = pre[u] = ++dfs_clock;
    int child = 0;

    for (int i = 0; i < G[u].size(); ++i)
    {
        int v = G[u][i];
        if (!pre[v])
        {
            child++;
            int lowv = Dfs(v, u);
            lowu = Min(lowu, lowv);
            if (lowv >= pre[u])
                is_cut[u] = true;
        } else if (pre[v] < pre[u] && v != fa)
            lowu = Min(lowu, pre[v]);
    }
    if (fa < 0 && child == 1)
        is_cut[u] = 0;
    low[u] = lowu;
    return lowu;
}

int main()
{
    int ver_num;

    while (scanf("%d", &ver_num) != EOF && ver_num)
    {
        getchar( );
        int ver_start, ver_next;

        memset(pre, 0, sizeof(pre));
        memset(low, 0, sizeof(low));
        memset(is_cut, 0, sizeof(is_cut));
        for (int i = 0; i < MAX_N; ++i)
            G[i].clear( );
        while (scanf("%d", &ver_start) && ver_start)
        {
            while (getchar() != ‘\n‘)
            {
                scanf("%d", &ver_next);
                G[ver_start].push_back(ver_next);
                G[ver_next].push_back(ver_start);
            }
        }
        int cut_ver_count = 0;
        dfs_clock = 0;
        Dfs(1, -1);
        for (int i = 1; i <= ver_num; ++i)
        {
            if (is_cut[i])
                cut_ver_count++;
        }
        printf("%d\n", cut_ver_count);
    }
    return 0;
}
时间: 2024-08-24 14:00:10

poj 1144 Network(割点)的相关文章

poj 1144 Network(割点 入门)

Network Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 10907   Accepted: 5042 Description A Telephone Line Company (TLC) is establishing a new telephone cable network. They are connecting several places numbered by integers from 1 to N

POJ 1144 Network(无向图连通分量求割点)

题目地址:POJ 1144 求割点.推断一个点是否是割点有两种推断情况: 假设u为割点,当且仅当满足以下的1条 1.假设u为树根,那么u必须有多于1棵子树 2.假设u不为树根.那么(u,v)为树枝边.当Low[v]>=DFN[u]时. 然后依据这两句来找割点就能够了. 代码例如以下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include &

POJ 1144 Network(强连通分量求割点)

题目地址:POJ 1144 求割点.判断一个点是否是割点有两种判断情况: 如果u为割点,当且仅当满足下面的1条 1.如果u为树根,那么u必须有多于1棵子树 2.如果u不为树根,那么(u,v)为树枝边,当Low[v]>=DFN[u]时. 然后根据这两句来找割点就可以了. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <

求无向图的割点 (poj 1144 Network)

割点 :去掉该点后原来的图不连通(出现好几个连通分量),该点被称为割点. 注意删除某点意味着和该点关联的边也全部删除 求割点的伪代码 DFS(v1,father): dfn[v1] = low[v1] = ++dfsClock vis[v1] = true child = 0 for each egde(v1,v2) in E: if(vis[v2] == false) : //(v1,v2)是父子边 DFS(v2,v1) child++ low[v1] = Min(low[v1],low[v2

poj 1144 Network【双连通分量求割点总数】

Network Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11042   Accepted: 5100 Description A Telephone Line Company (TLC) is establishing a new telephone cable network. They are connecting several places numbered by integers from 1 to N

poj 1144 Network 无向图求割点

Network Description A Telephone Line Company (TLC) is establishing a new telephone cable network. They are connecting several places numbered by integers from 1 to N . No two places have the same number. The lines are bidirectional and always connect

poj 1144 Network

Network 题意:输入n(n < 100)个点,不一定是连通图,问有多少个割点? 割点:删除某个点之后,图的联通分量增加. 思路:dfs利用时间戳dfs_clock的特性,点u的low函数low[u]代表以u为子树所得连到的最上面的祖先的时间戳. 即当点u存在一个子节点v,而low[v] >= pre[u]时,u就为割点:且割点要在最后判断,不能直接在iscut[] = true;处自增.因为cc_cnt代表的是当前的节点,而该节点可能有多个子节点,这样会导致重复计算. 直接使用了stri

图论(无向图的割顶):POJ 1144 Network

Network Description A Telephone Line Company (TLC) is establishing a new telephone cable network. They are connecting several places numbered by integers from 1 to N . No two places have the same number. The lines are bidirectional and always connect

poj 1144 Network【无向图求割顶模板题】

Description A Telephone Line Company (TLC) is establishing a new telephone cable network. They are connecting several places numbered by integers from 1 to N. No two places have the same number. The lines are bidirectional and always connect together