Poj1523 -- c-SPF (connected component, cut point)

Source: Internet
Author: User
C-SPF Time limit:1000 ms Memory limit:10000kb 64bit Io format:% I64d & % i64usubmit status

 

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 wocould prevent some of the still available nodes from communicating with each other. nodes 1 and 2 cowould still communicate with each other as cowould nodes 4 and 5, but communication between any other pairs of nodes wowould 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, wocould prevent at least one pair of available nodes from being able to communicate on what was previusly 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 in 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 shoshould 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 shocould 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

Returns the undirected edge. The input ends at 0, asks who the cut point is, and finds the cut point. The graph is divided into several connected components.

The method for finding a cut point is Tarjan. After finding the cut point, use the query set to find and divide the graph into several parts. Note that for the root, if the root has only one subtree, the root is not a cut point, however, if you use Tarjan to determine whether it is a cut point, you need to determine the root node.

Therefore, if the number of parts found by the cut point is 1, no output is required.

The output is pitfall. Each case has two spaces and one case has one row empty.

 

 

#include <cstdio>#include <cstring>#include <stack>#include <algorithm>using namespace std;#define maxn 1200struct node{    int u , v ;    int next ;} edge[1000000] ;int head[maxn] , cnt , vis[1000000] ;int dnf[maxn] , low[maxn] , time ;int ans[maxn] ;int c[maxn] , n ;stack <int> sta;void init(){    memset(head,-1,sizeof(head));    memset(vis,0,sizeof(vis));    memset(dnf,0,sizeof(dnf));    memset(low,0,sizeof(low));    memset(ans,0,sizeof(ans));    cnt = time = n = 0 ;}void add(int u,int v){    edge[cnt].u = u ;    edge[cnt].v = v ;    edge[cnt].next = head[u] ;    head[u] = cnt++ ;    edge[cnt].u = v ;    edge[cnt].v = u ;    edge[cnt].next = head[v] ;    head[v] = cnt++ ;}int find1(int x){    int r , k , l ;    r = x ;    while( r != c[r] )        r = c[r] ;    k = x ;    while( k != r )    {        l = c[k] ; c[k] = r ; k = l ;    }    return r ;}int f(int s){    int i , j , u , v , num[maxn] , sum = 0 ;    memset(num,0,sizeof(num));    for(i = 1 ; i <= n ; i++)        c[i] = i ;    for(i = 1 ; i <= n ; i++)    {        for(j = head[i] ; j != -1 ; j = edge[j].next)        {            if( edge[j].u == s || edge[j].v == s )                continue ;            u = find1( edge[j].u ) ;            v = find1( edge[j].v ) ;            if(u != v)                c[u] = v ;        }    }    for(i = 1 ; i <= n ; i++)    {        if( head[i] != -1 && i != s )        {            num[ find1(i) ]++ ;        }    }    for(i = 1 ; i <= n ; i++)        if( num[i] )            sum++ ;    return sum ;}void tarjan(int u){    dnf[u] = low[u] = ++time ;    int v, i ;    for(i = head[u] ; i != -1 ; i = edge[i].next)    {        if( vis[i] ) continue ;        vis[i] = vis[i^1] = 1 ;        v = edge[i].v ;        if( !dnf[v] )        {            sta.push(i);            tarjan(v);            low[u] = min( low[u],low[v] );            if( low[v] >= dnf[u] && !ans[u] )            {                ans[u] = f(u);            }        }        else if( dnf[v] < dnf[u] )            low[u] = min( low[u],dnf[v] );    }}int main(){    int u , v , temp = 0 , i;    while(scanf("%d", &u) && u)    {        temp++ ;        init();        scanf("%d", &v);        add(u,v);        n = max(n,u);        n = max(n,v);        while(scanf("%d", &u) && u)        {            scanf("%d", &v);            add(u,v);            n = max(n,u);            n = max(n,v);        }        tarjan(1);        int flag = 0 ;        printf("Network #%d\n", temp);        for(i = 1 ; i <= n ; i++)            if( ans[i] > 1 )            {                printf("  SPF node %d leaves %d subnets\n", i, ans[i]);                flag = 1 ;            }        if( !flag )            printf("  No SPF nodes\n\n");        else            printf("\n");    }    return 0;}


 

Poj1523 -- c-SPF (connected component, cut point)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.