您的位置:首页 > 大数据 > 人工智能

POJ 1523 SPF //求割点

2010-10-01 12:02 246 查看
 

 

SPF

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 1861 Accepted: 828
Description
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 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 leaves 2 subnets

Network #2
No SPF nodes

Network #3
SPF node 2 leaves 2 subnets
SPF node 3 leaves 2 subnets

Source
Greater New York 2000

定义
       割点:如果在图G中删去一个结点u后,图G的连通分枝数增加,即W(G-u)>W(G),则称结点u为G的割点,又称关节点。

描述:当一个结点u是割点时必满足以下两个条件之一:
             1)u为根且至少有两棵子树;
             2)u不为根且存在一个u在深搜树中的子女v使得LOW[v] ≥ LAB[u]。(LAB数组存储个结点的编号,LOW数组存储各点及其子树的各结点能到达的最小编号结点的编号。)

DFS
      描述:在对于任选一个图中结点为根的DFS搜索树中建立一个LAB数组与LOW数组。

 

 

1 //lab为一个全局变量,初始为1, LAB各项初始为0
2 DFS(u)
3      LAB[u] = LOW[u] = lab++
4     for each (u, v) in E(G)
5         if LAB[v] is 0
6              DFS(v)
7              LOW[u] = min{LOW[u], LOW[v]}
8         else if   v isnot parent of u
9              LOW[u] = min{LOW[u], LAB[v]}

#include <stdio.h>
#include<cstring>
#define MAX 1010
bool map[MAX][MAX],visited[MAX];  //邻接表和访问标志
//bool brige[MAX][MAX];  //桥标志
int cut[MAX]; //割顶度,即去掉这点,有多少连通块
int deep[MAX],ans[MAX],n,m; //深度和访问的最小主先
inline int min(int x,int y)
{
    return x<y?x:y;
}
void dfs(int idx,int fa,int d)
{
    int i;
    deep[idx]=ans[idx]=d;
    visited[idx]=1;
    for(i=1;i<=n;i++)
    if(map[idx][i])
    {
        if(i==fa) continue;
        if(visited[i])
        {
            ans[idx]=min(ans[idx],deep[i]);
            continue;
        }
        dfs(i,idx,d+1);
        ans[idx]=min(ans[idx],ans[i]);
        //if(ans[i]>d) brige[i][idx]=brige[idx][i]=1; //割边
        if(ans[i]>=d) cut[idx]++;
    }
    if(fa!=-1)cut[idx]++;
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(map,0,sizeof(map));
        memset(deep,0,sizeof(deep));
        memset(ans,0,sizeof(ans));
        memset(cut,0,sizeof(cut));
        memset(visited,0,sizeof(visited));
        for(int i=1;i<=m;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            map[x][y]=map[y][x]=1;
        }
        dfs(1,-1,0);
        bool flag=false;
        for(int i=1;i<=n;i++)
        if(cut[i]>1)
        {
            printf("  SPF node %d leaves %d subnets/n",i,cut[i]);
            flag=true;
        }
        if(!flag)  printf("  No SPF nodes/n");
        printf("/n");
    }
    return 0;
}

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息