poj_1523 SPF (求割点)

SPF

Time Limit: 1000MS

Memory Limit: 10000K

Total Submissions: 3790

Accepted: 1727

题目连接:http://poj.org/problem?id=1523

Description

Consider the two networks shown below.Assuming that data moves around these networks only between directly connectednodes on a peer-to-peer basis, a failure of a single node, 3, in the network onthe left would prevent some of the still available nodes from communicatingwith each other. Nodes 1 and 2 could still communicate with each other as couldnodes 4 and 5, but communication between any other pairs of nodes would nolonger 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 leastone pair of available nodes from being able to communicate on what waspreviously a fully connected network. Note that the network on the right has nosuch node; there is no SPF in the network. At least two machines must fail beforethere are any pairs of available nodes which cannot communicate. 

说明: http://poj.org/images/1523_1.jpg

Input

The input will contain the description ofseveral networks. A network description will consist of pairs of integers, onepair per line, that identify connected nodes. Ordering of the pairs isirrelevant; 1 2 and 2 1 specify the same connection. All node numbers willrange from 1 to 1000. A line containing a single zero ends the list ofconnected nodes. An empty network description flags the end of the input. Blanklines in the input file should be ignored.

Output

For each network in the input, you willoutput its number in the file, followed by a list of any SPF nodes thatexist. 

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 thenumber of fully connected subnets that remain when that node fails. If thenetwork has no SPF nodes, simply output the text "No SPF nodes"instead of a list of SPF nodes.

Sample Input

1 2

5 4

3 1

3 2

3 4

3 5

0

 

1 2

2 3

3 4

4 5

5 1

0

 

1 2

2 3

3 4

4 6

6 3

2 5

5 1

0

 

0

Sample Output

Network #1

  SPF node 3 leaves2 subnets

 

Network #2

  No SPF nodes

 

Network #3

  SPF node 2 leaves2 subnets

  SPF node 3 leaves2 subnets

Source

Greater New York 2000

题意:

      给你一张网络图,让你求出去掉图中的割点,能把图分割成几个子图。

解题思路:

         根据输入建立图map,在根据tarJan算法来求出割点,对于每一个割点,遍历与它相连的点,进行深搜,计算几次能把所有的点访问完

        

代码:

#include <iostream>
#include<cstdio>
#include<cstring>

#define maxn 1003
using namespace std;

int map[maxn][maxn];
int low[maxn];
int dfn[maxn];
int visited[maxn];
int flag[maxn];
int n;//计算点的个数
int times;
int root;//根节点

int max(int a,int b)
{
    return a>b?a:b;
}
int min(int a,int b)
{
    return a<b?a:b;
}

//深搜,查找连通子图数
void dfs(int x,int fa)
{
	visited[x]=1;
	for(int i=1;i<=n;i++)
	{
		if(i!=fa)
		{//去掉删除的节点
			if(!visited[i] && map[x][i])
			{
				dfs(i,fa);
			}
		}

	}
}

void tarJan(int u,int fa)//点u和点u的父节点
{
    int son=0;//son不能定义成全局变量
    low[u]=dfn[u]=++times;
    for(int i=1;i<=n;i++)
    {
        if(!map[u][i])
            continue;
        if(!dfn[i])
        {//i点如果没有被访问过
			son++;
            tarJan(i,u);
            low[u]=min(low[i],low[u]);
            if((u==root && son>=2) || (u!=root && dfn[u]<=low[i]))
            {
                flag[u]=1;
            }
        }
        else if(i!=fa)
        {
            low[u]=min(low[u],dfn[i]);
        }
    }
}

void init()
{
    memset(map,0,sizeof(map));
    memset(low,0,sizeof(low));
    memset(dfn,0,sizeof(dfn));
    memset(flag,0,sizeof(flag));

    times=0;
    n=-1;
	root=1;
}
void input()
{
    int s,t;
    int f=0;
	int ts=1;
    while(true)
    {
		init();
        while(true)
        {
            scanf("%d",&s);
            if(s==0)
            {
                f++;
                if(f>=2)
                    return ;
                break;
            }
            f=0;
            scanf("%d",&t);
            map[s][t]=map[t][s]=1;
            n=max(n,max(s,t));
        }

        tarJan(root,-1);
		int count;
		int num=0;
		printf("Network #%d\n",ts);
		for(int i=1;i<=n;i++)
		{
			if(flag[i]==1)
			{
				//去掉i
				num++;
				count=0;
				memset(visited,0,sizeof(visited));
				for(int j=1;j<=n;j++)
				{
					if(!visited[j] && map[i][j] && j!=i)
					{
						dfs(j,i);
						count++;
					}
				}
				printf("  SPF node %d leaves %d subnets\n",i,count);
			}
		}
		if(num==0)
		{
			printf("  No SPF nodes\n");
		}

		ts++;
		printf("\n");
    }


}
int main()
{
    input();
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

疯的世界

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值