程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> poj_1523 SPF (求割點)

poj_1523 SPF (求割點)

編輯:C++入門知識

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; 
            scanf("%d",&t); 
            map[s][t]=map[t][s]=1; 
            n=max(n,max(s,t)); 
        } 
 
        tarJan(root,-1); 
        int count; 
        int num=0; 
        printf("Network #%d\n",ts); 
        for(int i=1;i<=n;i++) 
        { 
            if(flag[i]==1) 
            { 
                //去掉i 
                num++; 
                count=0; 
                memset(visited,0,sizeof(visited)); 
                for(int j=1;j<=n;j++) 
                { 
                    if(!visited[j] && map[i][j] && j!=i) 
                    { 
                        dfs(j,i); 
                        count++; 
                    } 
                } 
                printf("  SPF node %d leaves %d subnets\n",i,count); 
            } 
        } 
        if(num==0) 
        { 
            printf("  No SPF nodes\n"); 
        } 
 
        ts++; 
        printf("\n"); 
    } 
 
 

int main() 

    input(); 
    return 0; 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved