关闭

割点和桥问题 poj1144模板题

标签: 割点
479人阅读 评论(0) 收藏 举报
分类:

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 two places and in each place the lines end in a telephone exchange. There is one telephone exchange in each place. From each place it is 
possible to reach through lines every other place, however it need not be a direct connection, it can go through several exchanges. From time to time the power supply fails at a place and then the exchange does not operate. The officials from TLC realized that in such a case it can happen that besides the fact that the place with the failure is unreachable, this can also cause that some other places cannot connect to each other. In such a case we will say the place (where the failure 
occured) is critical. Now the officials are trying to write a program for finding the number of all such critical places. Help them.

Input

The input file consists of several blocks of lines. Each block describes one network. In the first line of each block there is the number of places N < 100. Each of the next at most N lines contains the number of a place followed by the numbers of some places to which there is a direct line from this place. These at most N lines completely describe the network, i.e., each direct connection of two places in the network is contained at least in one row. All numbers in one line are separated 
by one space. Each block ends with a line containing just 0. The last block has only one line with N = 0;

Output

The output contains for each block except the last in the input file one line containing the number of critical places.

Sample Input

5
5 1 2 3 4
0
6
2 1 3
5 4 6 2
0
0

Sample Output

1
2

Hint

You need to determine the end of one line.In order to make it's easy to determine,there are no extra blank before the end of each line.





#include <iostream>
#include <algorithm>
#include <cstring>
#include<cstdio>
#include<string>
#include<cmath>
#include<cstdlib>
#include<vector>
#define LL long long
#define inf 0x3f3f3f3f
using namespace std;
const int MAX=110;
int dfn[MAX],low[MAX],head[MAX],visited[MAX];
bool cut[MAX];//点1~n是否是割点
int n,cnt,k,root;
struct Edge
{
    int to,next;
}edge[MAX*MAX];
void addedge(int cu,int cv)
{
    edge[cnt].to=cv;
    edge[cnt].next=head[cu];
    head[cu]=cnt++;
}
void tarjan(int u,int fa)
{
    int son=0;
    visited[u]=1;
    dfn[u]=low[u]=++k;
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].to;
        if(visited[v]==1&&v!=fa)
            low[u]=min(low[u],dfn[v]);
        if(visited[v]==0)
        {
            tarjan(v,u);
            son++;
            low[u]=min(low[u],low[v]);
            if((u==root&&son>1)||(u!=root&&dfn[u]<=low[v]))
                cut[u]=1;
        }
    }
    visited[u]=2;
}
int main()
{
    while(cin>>n)
    {
        if(n==0)
            return 0;
        memset(head,-1,sizeof(head));
        memset(dfn,0,sizeof(dfn));
        memset(low,0,sizeof(low));
        memset(visited,0,sizeof(visited));
        memset(cut,0,sizeof(cut));
        cnt=0;
        int u,v;
        while(scanf("%d",&u) && u){
            while(getchar()!='\n'){
                scanf("%d",&v);
                addedge(u,v);
                addedge(v,u);
            }
        }
        root=1;
        tarjan(root,-1);
        int ans=0;
        for(int i=1;i<=n;i++)
            if(cut[i])
                ans++;
        cout<<ans<<endl;
    }
    return 0;
}





求割点和桥模板:
const int MAX=110;
int dfn[MAX],low[MAX],head[MAX],visited[MAX];
bool cut[MAX];//点1~n是否是割点
int n,cnt,k,root,nume;nume桥的个数
struct Edge
{
    int to,next;
}edge[MAX*MAX],cutm[MAX*MAX];//cutm是桥
void addedge(int cu,int cv)
{
    edge[cnt].to=cv;
    edge[cnt].next=head[cu];
    head[cu]=cnt++;
}
void tarjan(int u,int fa)
{
    int son=0;
    visited[u]=1;
    dfn[u]=low[u]=++k;
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].to;
        if(visited[v]==1&&v!=fa)
            low[u]=min(low[u],dfn[v]);
        if(visited[v]==0)
        {
            tarjan(v,u);
            son++;
            low[u]=min(low[u],low[v]);
            if((u==root&&son>1)||(u!=root&&dfn[u]<=low[v]))
                cut[u]=1;
//if(dfn[u]<low[v]) cute[++nume]=Edge(u,v);
求桥
        }
    }
    visited[u]=2;
}
int main()
{
    while(cin>>n)
    {
        if(n==0)
            return 0;
        memset(head,-1,sizeof(head));
        memset(dfn,0,sizeof(dfn));
        memset(low,0,sizeof(low));
        memset(visited,0,sizeof(visited));
        memset(cut,0,sizeof(cut));
        cnt=0;
//构造边
        int u,v;
        while(scanf("%d",&u) && u){
            while(getchar()!='\n'){
                scanf("%d",&v);
                addedge(u,v);
                addedge(v,u);
            }
        }
//、、、、、、、、、、、、
        root=1;
        tarjan(root,-1);
        int ans=0;
        for(int i=1;i<=n;i++)
            if(cut[i])
                ans++;
        cout<<ans<<endl;//输出割点个数
    }


0
0
查看评论

算法五:图的割点和桥

一、定义 图的割点 一个无向连接图中,如果删除某个顶点后,图不再连同(即任意两点之间不能互相到达) ,称这样的顶点为割点 或:某个点是割点当且仅当删除该点和与该点相关联的边后图变得不连通。 图的割边/桥:  一个无向连通图中,如果删除某条边后,图不再连通,这条边就为...
  • my88site
  • my88site
  • 2015-07-09 16:16
  • 1299

Tarjan算法求解一个无向图中的割点和桥问题

基本概念割点:Articulation Point 在无向连通图中,删除一个顶点v及其相连的边后,原图从一个连通分量变成了两个或多个连通分量,则称顶点v为割点,同时也称关节点(Articulation Point)。 双连通的图:一个没有关节点的连通图称为重连通图(biconnected gra...
  • starstar1992
  • starstar1992
  • 2016-10-27 13:29
  • 1548

无向连通图的割点、桥

无向连通图的割点、桥 泳裤王子原创,转载请注明出处 http://blog.csdn.net/tclh123/article/details/6705392 预备知识:        割点集合   &#...
  • tclh123
  • tclh123
  • 2011-08-21 00:40
  • 7244

POJ 1144 Network 无向图求割点

来源:http://poj.org/problem?id=1144 题意:就是给你一些点,某些点之间有边。求有多少个点是割点。 思路:模板题目了,直接用无向图求个点模板就可以ac。需要注意的是输入,输入有点麻烦。以换行结尾可以写成while(getchar() != '\n'),...
  • wmn_wmn
  • wmn_wmn
  • 2012-08-25 10:59
  • 3535

图论之割点和桥

割点:如果在图G中删去一个结点u后,图G的连通分枝数增加,即W(G-u)>W(G),则称结点u为G的割点,又称关节点。 桥:如果在图G中删去一条边e后,图G的连通分支数增加,即W(G-e)>W(G),则称边e为G的桥,又称割边或关节边。 双连通分支:G中不含割点的极大连通子图称为G的...
  • baidu_17313961
  • baidu_17313961
  • 2015-12-08 10:04
  • 878

poj1144-tarjan求割点

何为割点?也就是题目中的关键点。在一个无向图中,去掉一个点,这个无向图会变成多个子图,那么这个点就叫做割点 同理,割边也是如此,如果去掉一条边,能让无向图变成多个子图,那么这条边叫做割边,所谓的桥。   那么tarjan是如何求的割点的呢? 如果u为割点,当且仅当满足下面的1/2 ...
  • zhang20072844
  • zhang20072844
  • 2012-10-23 23:40
  • 5677

算法学习——求割点与桥的tarjan算法 HDU4738

前天打周赛做到 HDU4738  绞尽脑汁都没想到用什么好的方法来解决这个问题   周赛结束之后跟Yasola和xcy讨论了一下居然用到  tarjan算法   exm???  tarjan不是用来求 lca的么??? 回去怒补了一发才知道 ...
  • qq_33656136
  • qq_33656136
  • 2016-11-15 11:48
  • 793

tarjan求桥及割点

/* 求割点,去掉割点连通块的个数 求桥 */ const int maxV=10010,maxE=100010; struct Edge { int to,next; bool cut; }edge[maxE]; int Adj[maxV],Size; void init()...
  • u012797220
  • u012797220
  • 2014-01-23 21:43
  • 1147

无重边连通无向图求割点和桥

一.割点和桥  无向连通图中,如果删除某点后,图变 成不连通,则称该点为割点。    无向连通图中,如果删除某边后,图变 成不连通,则称该边为桥。 所以说啊,割点和桥这个概念的应该范围应该只是在无向连通图中的!这一点要十分注意! 二.怎样判定 dfn[u]定义和前面类似,但是low[...
  • u013555159
  • u013555159
  • 2016-09-03 21:01
  • 481

求无向图的割点和桥

/** * 求 无向图的割点和桥 * 可以找出割点和桥,求删掉每个点后增加的连通块。 * 需要注意重边的处理,可以先用矩阵存,再转邻接表,或者进行判重 * 调用solve输出割点数,全局变量bridge记录边的个数 */ #include #include #include #incl...
  • u013573047
  • u013573047
  • 2015-02-27 10:27
  • 1348
    个人资料
    • 访问:105292次
    • 积分:3828
    • 等级:
    • 排名:第9936名
    • 原创:280篇
    • 转载:16篇
    • 译文:0篇
    • 评论:11条
    赞助一下
    如果您觉得我的文章对您有帮助的话,不妨小额赞助一下,激励我写出更多的好文章,谢谢大家!对于支持我的朋友们,我会根据情况随机选择出幸运用户,送出关于python,深度学习和tensorflow相关的书籍~~

    以下是我的支付宝和微信账户
    如果不愿意赞助,可以扫一扫我发的支付宝红包,也是对我的鼓励,真诚感谢!