poj1144Network 无向图求割点Tarjan

n个点,组成一个无向图,求这个图中割点的数量。模板题。

只是这道题在输入数据的时候有点麻烦,如样例中,第一组数据有五个点,5 1 2 3 4 表示5这个点与1 2 3 4 点相连。其中这个图的割点只是5这个点。第二组数据6个点,2 与1 3 5相连,5与2 4 6相连,其中2点与5点都是割点。

有两类节点可以成为割点:

对根节点u,若其有两棵或两棵以上的子树,则该根结点u为割点;

对非叶子节点u(非根节点),若其子树的节点均没有指向u的祖先节点的回边,说明删除u之后,根结点与u的子树的节点不再连通;则节点u为割点。

对于根结点,显然很好处理;但是对于非叶子节点,怎么去判断有没有回边是一个值得深思的问题。

我们用dfn[u]记录节点u在DFS过程中被遍历到的次序号(即时间戳),low[u]记录节点u或u的子树通过非父子边追溯到最早的祖先节点(即DFS次序号最小),那么low[u]的计算过程如下:

当(u,v)为树边时low[u]={min{low[u], low[v]},这块就是一个递归后回溯的过程

当(u , v )为回边且v不为u的父亲节点low[u]=min{low[u], dfn[v]},这里是说找到了不通过父亲节点还能回去的路的那个点,那么用这个点的时间戳和现在的能到达的最小时间戳作比较就可以了

详见http://www.cnblogs.com/en-heng/p/4002658.html

#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
const int MAXN=105;
const int MAXM=10010;
struct edge
{
    int to,next;
    bool cut;
} edge[MAXM];
int head[MAXM],tot;
int low[MAXN],dfn[MAXN],Stack[MAXN];
int index,top;
bool instack[MAXN];
bool cut[MAXN];
int add_block[MAXN];//切记这个数组表示的是增加了几个连通块,而不是当前有几个连通块。
int bridge;
void addedge(int u,int v)
{
    edge[tot].to=v;
    edge[tot].next=head[u];
    edge[tot].cut=false;
    head[u]=tot++;
}
void tarjan(int u,int pre)
{
    int v;
    low[u]=dfn[u]=++index;
    Stack[top++]=u;
    instack[u]=true;//这个地方用了栈,刚开始并没理解这块的意思,因为刚开始看的是啊哈算法,其实就是说每个点不重复遍历的意思
    int son=0;
    for(int i=head[u]; i!=-1; i=edge[i].next)
    {
        v=edge[i].to;
        if(v==pre)
            continue;
        if(!dfn[v])
        {
            son++;
            tarjan(v,u);
            if(low[u]>low[v])
                low[u]=low[v];
            if(u!=pre&&low[v]>=dfn[u])
            {
                cut[u]=true;
                add_block[u]++;
            }
        }
        else if(low[u]>dfn[v])
            low[u]=dfn[v];
    }
    if(u==pre&&son>1)
        cut[u]=true;
    if(u==pre)
        add_block[u]=son-1;
    instack[u]=false;
    top--;
}

void init()
{
    tot=0;
    memset(head,-1,sizeof(head));//这里一定不要忘了加
    memset(low,0,sizeof(low));
    memset(dfn,0,sizeof(dfn));
    memset(instack,0,sizeof(instack));
    memset(add_block,0,sizeof(add_block));
    memset(cut,0,sizeof(cut));
}
int a,b,n;
int main()
{
    while(cin>>n)
    {
        int sum=0;
        init();
        if(n==0)
            break;
        while(scanf("%d",&a)!=-1)
        {
            if(a==0)
               break;
            while(getchar()!=‘\n‘)
            {
                cin>>b;
                addedge(a,b);
                addedge(b,a);
            }
        }
        tarjan(1,1);
        for(int i=1; i<=n; i++)
            if(cut[i])
                sum++;
        cout<<sum<<endl;
    }
    return 0;
}
时间: 2024-11-05 16:10:18

poj1144Network 无向图求割点Tarjan的相关文章

UVA 315 Network(无向图求割点)

题目大意 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 two pl

无向图求割点 UVA 315 Network

输入数据处理正确其余的就是套强联通的模板了 #include <iostream> #include <cstdlib> #include <cstdio> #include <algorithm> #include <vector> #include <queue> #include <cmath> #include <stack> #include <cstring> using namespa

POJ 1144 无向图求割点

学长写的: #include<cstdio>#include<cstdlib>#include<cmath>#include<iostream>#include<algorithm>#include<cstring>#include<vector>using namespace std;#define maxn 10005int dfn[maxn];///代表最先遍历到这个点的时间int low[maxn];///这个点所

POJ1523 SPF 【求割点Tarjan】

SPF Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6131   Accepted: 2814 Description Consider the two networks shown below. Assuming that data moves around these networks only between directly connected nodes on a peer-to-peer basis, a

B - Network---UVA 315(无向图求割点)

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 two places

(连通图 模板题 无向图求割点)Network --UVA--315(POJ--1144)

链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=251 http://poj.org/problem?id=1144 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82833#problem/B 代码: #include<cstdio> #include<

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 2117 Electricity 【无向图求割点】【求去掉一个点后 图中最多的BCC数目】

Electricity Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4597   Accepted: 1515 Description Blackouts and Dark Nights (also known as ACM++) is a company that provides electricity. The company owns several power plants, each of them sup

UVA315 (无向图求割点)

题目大意:给定一个无向图,问共存在多少个割点.(割点:去掉此点后此图会断开连接)割点有两种存在:一种是第一次搜索的根节点,若其子节点数超过两个,则此点去掉后图会 断开连接,因此此点为割点:或者此点为搜索树的子节点,若其子节点的最早达到时间状态比其自身要晚,则说明此点不得不经过,并以此来更新其子节点,故其也是割点. 详见代码. #include <stdio.h> #include <algorithm> #include <vector> #include <st