关闭

POJ 1144 Tarjan 割点 解题报告

170人阅读 评论(0) 收藏 举报
分类:

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

【解题报告】

代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 110

int n,cnt,head[N];
struct Edge{int to,nxt;}e[N*N];
int dfn[N],low[N],vis[N],cut[N];
int indexx,root,ans;

void init()
{
    cnt=-1;indexx=root=ans=0;
    memset(head,-1,sizeof(head));
    memset(dfn,-1,sizeof(dfn));
    memset(low,-1,sizeof(low));
    memset(vis,0,sizeof(vis));
    memset(cut,0,sizeof(cut));
}
void adde(int u,int v)
{
    e[++cnt].to=v;e[cnt].nxt=head[u];head[u]=cnt;
    e[++cnt].to=u;e[cnt].nxt=head[v];head[v]=cnt;
}
void tarjan(int u)
{
    dfn[u]=low[u]=++indexx;
    vis[u]=1;
    for(int i=head[u];~i;i=e[i].nxt)
    {
        int v=e[i].to;
        if(!vis[v])
        {
            tarjan(v);
            low[u]=min(low[u],low[v]);
            if(low[v]>=dfn[u]&&u!=1) cut[u]=1;
            else if(u==1) root++;
        }
        else low[u]=min(low[u],dfn[v]);
    }
}
int main()
{
    while(scanf("%d",&n)&&n)
    {
        init();int u,v;
        while(scanf("%d",&u)&&u)  
        {  
            while(getchar()!='\n')  
            {  
                scanf("%d",&v);  
                adde(u,v);  
            }  
        } 
        for(int i=1;i<=n;++i)
            if(dfn[i]==-1) tarjan(i);
        if(root>1) ans++;
        for(int i=2;i<=n;++i) if(cut[i]) ans++;
        printf("%d\n",ans);
    }
    return 0;
}
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 110

int n,cnt=-1,head[N];
struct Edge{int to,nxt;}e[N*N];
int idc,dfn[N],low[N],vis[N],cut[N];

void init()
{
    cnt=-1;idc=0;
    memset(head,-1,sizeof(head));
    memset(dfn,0,sizeof(dfn));
    memset(low,0,sizeof(low));
    memset(vis,0,sizeof(vis));
    memset(cut,0,sizeof(cut));
}
void adde(int u,int v)
{
    e[++cnt].to=v;e[cnt].nxt=head[u];head[u]=cnt;
    e[++cnt].to=u;e[cnt].nxt=head[v];head[v]=cnt;
}
void Tarjan(int u)
{
    dfn[u]=low[u]=++idc;vis[u]=1;
    for(int i=head[u];~i;i=e[i].nxt)
    {
        int v=e[i].to;
        if(!vis[v])
        {
            Tarjan(v);
            low[u]=min(low[u],low[v]);
            if(low[v]>=dfn[u]) cut[u]++;
        }
        else low[u]=min(low[u],dfn[v]);
    }
}
int main()
{
    while(~scanf("%d",&n)&&n)
    {
        init(); 
        int u,v;
        while(scanf("%d",&u)&&u)
        {
            while(getchar()!='\n')
            {
                scanf("%d",&v);
                adde(u,v);
            }
        }
        for(int i=1;i<=n;++i) if(!vis[i]) Tarjan(i);
        int ans=0;
        cut[1]--;
        for(int i=1;i<=n;++i) printf("%d ",dfn[i]);puts("");
        for(int i=1;i<=n;++i) printf("%d ",low[i]);puts("");
        for(int i=1;i<=n;++i)
        {
            printf("%d ",cut[i]);
            if(cut[i]>0) ans+=1;
        }
        puts("");
        printf("%d\n",ans);
    }
    return 0;
}
0
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

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

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

关于tarjan算法的一些理解(割点割边)

首先介绍以下tarjan算法: ---------------------------------------------tarjan算法------------------------------...
  • qq_24451605
  • qq_24451605
  • 2015-07-20 06:44
  • 2060

【POJ 1144】 Network(割点入门)

【POJ 1144】 Network(割点入门) Network Time Limit: 1000MS   Memory Limit: 10000K To...
  • ChallengerRumble
  • ChallengerRumble
  • 2016-02-20 14:59
  • 1317

【POJ 1144】Network(Tarjan求割点)

红叶黄花秋意晚,千里念行客
  • reverie_mjp
  • reverie_mjp
  • 2016-11-08 00:09
  • 168

poj1144Network 无向图求割点Tarjan

n个点,组成一个无向图,求这个图中割点的数量。模板题。 只是这道题在输入数据的时候有点麻烦,如样例中,第一组数据有五个点,5 1 2 3 4 表示5这个点与1 2 3 4 点相连。其中这个图的割点...
  • zcmartin2014214283
  • zcmartin2014214283
  • 2016-05-06 12:50
  • 653

[poj1144 Network]tarjan求割点

Network Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9767   Accept...
  • bit_Line
  • bit_Line
  • 2014-11-29 10:08
  • 392

tarjan求割点 poj 1144

tarjan可真是多才多艺,既求强连通,又求割点割边,还求lca,居然每一种都相差不多。求割点主要是利用dfs遍历树(李顶龙童鞋一直不理解tarjan,从dfs树应该比较直观)。rel[x]记录x实际...
  • huyuncong
  • huyuncong
  • 2011-05-12 22:26
  • 1813

POJ 1144 Network 无向图求割点Tarjan

题目描述:Description A Telephone Line Company (TLC) is establishing a new telephone cable network. They...
  • wuxuanyi27
  • wuxuanyi27
  • 2016-05-05 11:57
  • 218

Tarjan算法求解桥和边双连通分量(附POJ 3352 Road Construction解题报告)

在说Tarjan算法解决桥和边连通分量问题之前我们先来回顾一下Tarjan算法是如何求解强连通分量的。       Tarjan算法在求解强连通分量的时候,通过引入dfs过程中对一个点访问的顺序dfs...
  • geniusluzh
  • geniusluzh
  • 2011-07-21 11:03
  • 9535

poj 1144 Network tarjan求无向连通图的割点个数

A Telephone Line Company (TLC) is establishing a new telephone cable network. They are connecting se...
  • kongming_acm
  • kongming_acm
  • 2011-04-29 20:31
  • 672
    个人资料
    • 访问:36875次
    • 积分:3084
    • 等级:
    • 排名:第13011名
    • 原创:277篇
    • 转载:22篇
    • 译文:0篇
    • 评论:13条
    友情链接
    文章分类