poj 1523 SPF

#include <iostream>
#include <cstdlib>
#include <cmath>
#include <algorithm>
using namespace std;
//poj1523
int con[1010][1010];  //图的邻接矩阵
int low[1010];        
int mark[1010];
bool iscut[1010];     //标记是否关节点
int n;
int a,b;
int cnt;              //搜索索引


void dfs(int v)
{
     mark[v]=low[v]=cnt++;
     int s=0;
     for (int i=1;i<=n;i++)
     if (con[v][i] && i!=v)
     {
                   if (mark[i]==-1)
                   {
                                   dfs(i);
                                   if (low[i]>=mark[v] && v!=1)
                                     iscut[v]=true;
                                
                                   low[v]=min(low[v],low[i]);
                                   if (v==1) s++;  //根节点单独处理:判断有几棵子树
                   }
                   else low[v]=min(low[v],mark[i]);
                  
     }
     if (s>=2)  iscut[1]=true;
 
}

//遍历,求关节点的子树棵数,即该关节点分割的分量
void travel(int v)
{
     mark[v]=1;
     for (int i=1;i<=n;i++)
     if (con[v][i] && !mark[i])
     travel(i);
}

int main()
{
    int t=1;
    while (1)
    {
          memset(con,0,sizeof(con));
          memset(mark,-1,sizeof(mark));
          memset(low,0,sizeof(low));
          memset(iscut,false,sizeof(iscut));
           n=0;
          scanf("%d",&a);
          if (!a) break;
          scanf("%d",&b);
          n=max(n,a); n=max(n,b);
          con[a][b]=con[b][a]=1;
        
    while (scanf("%d",&a) && a)
    {
         scanf("%d",&b);
        n=max(n,a); n=max(n,b);
          con[a][b]=con[b][a]=1;
    }
    cnt=0;
    dfs(1);
    printf("Network #%d/n",t++);
  
    bool ans=false;
    for (int i=1;i<=n;i++)
      if (iscut[i])
      {
                   memset(mark,0,sizeof(mark));
                   ans=true;
                   int cutnum=0;    //分割连通块的数量
                   mark[i]=1;
                   for (int k=1;k<=n;k++)
                   if (!mark[k])
                   {
                       travel(k);
                       cutnum++;
                   }
                   printf("  SPF node %d leaves %d subnets/n",i,cutnum);
      }
    if (!ans)    printf("  No SPF nodes/n");
   
    printf("/n");
 }
   // system("pause");
    return 0;
}

你可能感兴趣的:(System,NetWork,include)