POJ-1523 SPF (割点[Tarjan])

SPF
http://poj.org/problem?id=1523
Time Limit: 1000MS   Memory Limit: 10000K
     

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. 
POJ-1523 SPF (割点[Tarjan])_第1张图片

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


题目大意:给定一个无向图,若删除某个点,剩余的图不连通,则输出该点和剩余图的连通分量数。


找不到练习寻找割点的裸的简单题了,本题是求删除割点后能形成的连通分量数,感觉求连通分量数的写法与讲义还是有些差别,但是明白之后发现这样写更简洁明了

割点:①若u为树根,且u有多余一个子树

            ②若u不为树根,且存在(u,v)为树枝边(或称父子边,即u为v在搜索树中的父亲),使得dfn[u]<=low[v]

桥:当(u,v)为树枝边,且满足dfn[u]<low[v](前提是其没有重边)


#include <cstdio>
#include <cstring>
#include <vector>

using namespace std;

const int MAXN=1005;

int n,num;
int mp[MAXN],low[MAXN],dfn[MAXN],blocks[MAXN];//biocks[i]表示删掉i点后,能形成的连通块数
vector<int> g[MAXN];
bool haveSPF;//haveSPF表示有无SPF

void Tarjan(int u,int p) {
    dfn[u]=low[u]=++num;
    int v;
    for(int i=0;i<g[u].size();++i) {
        v=g[u][i];
        if(u!=p) {//若v不为u的父节点
            if(dfn[v]==0) {//若v未遍历
                Tarjan(v,u);
                low[u]=min(low[u],low[v]);
                if(dfn[u]<=low[v])//若最终blocks[u]>1时,u才割点(若u为根节点,则其至少2次为true;若u不为根节点,则其至少1次为true)
                    ++blocks[u];//删除点u后,其子结点能形成的连通块数+1
                //若该不等式不取等号,则(u,v)为桥
            }
            else if(dfn[v]<low[u])//若v遍历到时间更小
                low[u]=dfn[v];
        }
    }
}

int main() {
    int s,e,kase=0;
    while(scanf("%d",&s),s!=0) {
        for(int i=1;i<MAXN;++i)
            g[i].clear();
        memset(mp,0,sizeof(mp));
        num=n=0;
        do {
            scanf("%d",&e);
            if(mp[s]==0)
                mp[s]=++n;
            if(mp[e]==0)
                mp[e]=++n;
            g[mp[s]].push_back(mp[e]);
            g[mp[e]].push_back(mp[s]);
        } while(scanf("%d",&s),s!=0);

        for(int i=1;i<=n;++i) {
            dfn[i]=0;
            blocks[i]=1;//默认删除i点后,i点的父亲结点有一个连通块
        }
        for(int i=1;i<=n;++i) {//防止图不连通
            if(dfn[i]==0) {
                blocks[i]=0;//根节点无父亲结点,即不存在父亲结点的连通块
                Tarjan(i,0);
            }
        }
        haveSPF=false;
        printf("Network #%d\n",++kase);
        for(int i=1;i<=n;++i) {
            if(blocks[mp[i]]>1) {
                haveSPF=true;
                printf("  SPF node %d leaves %d subnets\n",i,blocks[mp[i]]);
            }
        }
        if(!haveSPF)
            printf("  No SPF nodes\n");
        printf("\n");
    }
    return 0;
}


你可能感兴趣的:(poj,图论,Tarjan)