当前位置:编程学习 > C/C++ >>

poj_1523 SPF (求割点)

SPF
Time Limit: 1000MS
Memory Limit: 10000K
Total Submissions: 3790
Accepted: 1727
 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.

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; 补充:软件开发 , C++ ,

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,