poj 1523 割点分块

来源:互联网 发布:淘宝 黑搜与白搜 编辑:程序博客网 时间:2024/05/01 18:59
SPF
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 4088 Accepted: 1889

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 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
 
 
求解割点以及割点将图分成的块数。先求割点,然后计算割点对图的划分,即该割点能将图分解成几块。
#include<iostream>#include<stdio.h>#include<string.h>using namespace std;#define maxn 1005#define maxm 100005int dfn[maxn],low[maxn],head[maxn],vis[maxn],vis1[maxn];int cut[maxn];int e,m,n,root,index;struct node{    int to;    int next;} edge[maxm];void init(){    e=0;    index=0;    memset(dfn,0,sizeof(dfn));    memset(low,0,sizeof(low));    memset(vis,0,sizeof(vis));    memset(head,-1,sizeof(head));    memset(cut,0,sizeof(cut));}int min(int a,int b){    return a<b?a:b;}void addedge(int u,int v){    edge[e].to=v;    edge[e].next=head[u];    head[u]=e++;}void findCutpoint(int u,int father){    int son=0;    dfn[u]=low[u]=++index;    vis[u]=1;    for(int i=head[u]; i!=-1; i=edge[i].next)    {        int v=edge[i].to;        if(vis[v]==1&&v!=father)            low[u]=min(low[u],dfn[v]);        if(!vis[v])        {            findCutpoint(v,u);            son++;            low[u]=min(low[u],low[v]);            if((u!=root&&dfn[u]<=low[v])||(u==root&&son>1))                cut[u]=1;        }    }    vis[u]=2;}void dfs(int t){    vis1[t]=1;    for(int i=head[t]; i!=-1; i=edge[i].next)    {        int v=edge[i].to;        if(!vis1[v])        {            vis1[v]=1;            dfs(v);        }    }}int mmax=-1;int main(){    int cas=1;    while(cin>>m,m)    {        init();        if(m>mmax)            mmax=m;        cin>>n;        if(n>mmax)            mmax=n;        addedge(m,n);        addedge(n,m);        int a,b;        while(cin>>a,a)        {            if(a>mmax)                mmax=a;            cin>>b;            if(b>mmax)                mmax=b;            addedge(a,b);            addedge(b,a);        }        root=1;        findCutpoint(1,-1);        int f=0;        cout<<"Network #"<<cas++<<endl;        for(int i=1; i<=mmax; i++)        {            if(cut[i])            {                f=1;                memset(vis1,0,sizeof(vis1));                vis1[i]=1;                int son=0;//计算割点分成的块(割点的孩子)                for(int j=head[i]; j!=-1; j=edge[j].next)                {                    if(!vis1[edge[j].to])                    {                        dfs(edge[j].to);                        son++;                    }                }                cout<<"  SPF node "<<i<<" leaves "<<son<<" subnets"<<endl;            }        }        if(!f)        {            cout<<"  No SPF nodes"<<endl;        }        cout<<endl;    }    return 0;}

原创粉丝点击