POJ 1523 SPF(Tarjan)

来源:互联网 发布:nginx tcp 反向代理 编辑:程序博客网 时间:2024/05/16 12:15

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 failure of a single node, 3, in the network on the left would prevent some of the still available nodes from communicating with each other. Nodes 1 and 2 could still communicate with each other as could nodes 4 and 5, but communication between any other pairs of nodes would no longer be possible. 

Node 3 is therefore a Single Point of Failure (SPF) for this network. Strictly, an SPF will be defined as any node that, if unavailable, would prevent at least one pair of available nodes from being able to communicate on what was previously a fully connected network. Note that the network on the right has no such node; there is no SPF in the network. At least two machines must fail before there are any pairs of available nodes which cannot communicate. 
Input
The input will contain the description of several networks. A network description will consist of pairs of integers, one pair per line, that identify connected nodes. Ordering of the pairs is irrelevant; 1 2 and 2 1 specify the same connection. All node numbers will range from 1 to 1000. A line containing a single zero ends the list of connected nodes. An empty network description flags the end of the input. Blank lines in the input file should be ignored.
Output
For each network in the input, you will output its number in the file, followed by a list of any SPF nodes that exist. 

The first network in the file should be identified as "Network #1", the second as "Network #2", etc. For each SPF node, output a line, formatted as shown in the examples below, that identifies the node and the number of fully connected subnets that remain when that node fails. If the network has no SPF nodes, simply output the text "No SPF nodes" instead of a list of SPF nodes.
Sample Input
1 25 43 13 23 43 501 22 33 44 55 101 22 33 44 66 32 55 100
Sample Output
Network #1  SPF node 3 leaves 2 subnetsNetwork #2  No SPF nodesNetwork #3  SPF node 2 leaves 2 subnets  SPF node 3 leaves 2 subnets

题意:给出一个联通的网络,分别去掉每一个关节点后,有多少连通分量?

思路;

   在用程序实现求关节点时,需要解决一下几个问题:

1)   如何判断顶点v是顶点u的祖先节点

2)   如何判断边(v,u)是回边

3)   如何判断顶点v是顶点u的儿子节点

这三个问题都是在dfs过程中解决的,从顶点u出发进行dfs搜索时,要判断其他每个顶点v跟顶点u是否连接,是否被访问过,如果v跟u邻接,则在生成树中就是两种情况;

① 如果顶点v是顶点u的邻接顶点,且此时v还没有被访问过,则v是u的儿子节点

② 如果顶点v是顶点u的邻接顶点,此时v已经被访问过了,则v是u的祖先节点。并且(v,u)就是一条回边。

回边:可理解为在DFS过程中遇到已访问节点时所经过的边,也称为返祖边、后向边

当(u,v)为树边且low[v]>dfn[u]时,表示v节点只能通过该边(u,v)与u连通,那么(u,v)即为割边。

#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define MAXN 1010int g[MAXN][MAXN];//邻接矩阵int vis[MAXN];//记录每个节点是否访问过int subnets[MAXN];//表示去掉该节点后的连通分量个数int dfn[MAXN],low[MAXN];int nodes,son,tarnum;void init(){    low[1]=dfn[1]=1;    tarnum=1;    son=0;    memset(vis,0,sizeof(vis));    vis[1]=1;    memset(subnets,0,sizeof(subnets));}void tarjan(int u){    int v;    for(v=1; v<=nodes; v++)    {        //v跟u邻接,在生成树中有两种情况        //①v是u的祖先节点 这样(v,u)就是一条回边         //②v是u 的儿子节点        if(g[u][v])        {            if(!vis[v])//v还未访问过,v是u的儿子,第二种情况            {                vis[v]=1;                tarnum++;                dfn[v]=low[v]=tarnum;                tarjan(v);//dfs执行完 low【v】值已经求出                //回退的时候计算定点u的low值                low[u]=min(low[u],low[v]);                if(low[v]>=dfn[u])                {                    if(u!=1) subnets[u]++;//去掉该节点后的连通分量个数                    //根节点的子女节点个数(如果大于2,则根节点是关节点)                    if(u==1) son++;                }            }            //此前v已经访问过了,v是u 的祖先节点,(v,u)就是一条回边,情况一            else low[u]=min(low[u],dfn[v]);        }    }}int main(){    int i,j,u,v,cas=1;    while(1)    {        nodes=0;//统计有多少节点        memset(g,0,sizeof(g));        scanf("%d",&u);        if(u==0) break;        scanf("%d",&v);        nodes=max(nodes,u);        nodes=max(nodes,v);        g[u][v]=g[v][u]=1;        while(1)        {            scanf("%d",&u);            if(u==0) break;            scanf("%d",&v);            nodes=max(nodes,u);            nodes=max(nodes,v);            g[u][v]=g[v][u]=1;        }        init();        tarjan(1);        if(son>1) subnets[1]=son-1;        if(cas>1) printf("\n");        printf("Network #%d\n",cas++);        int flag=0;        for(i=1; i<=nodes; i++)        {            if(subnets[i])            {                flag=1;                printf("  SPF node %d leaves %d subnets\n",i,subnets[i]+1);            }        }        if(!flag) printf("  No SPF nodes\n");    }    return 0;}

原创粉丝点击