求删掉割点后图变成几个连通分支。
求割点,对每个块上的点进行统计。输出割点存在于几个块就可以了。
#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;
#define L(t) t<<1
#define R(t) t<<1|1
#define eps 1e-7
typedef long long LL;
const int INF=1000000010;
const int maxn=1005;
const int maxm=1005;
const int MOD=1000000007;
int head[maxn],next[maxn*3],to[maxn*3];
int tot,cnt,top;
int dfn[maxn],low[maxn],st[maxn];
int block[maxn];
bool isGe[maxn];
bool node[maxn];
void cutBlock(int v)
{
dfn[v]=low[v]=++cnt;
st[++top]=v;
int u;
for(int i=head[v];i!=-1;i=next[i])
{
u=to[i];
if(!dfn[u])
{
cutBlock(u);
low[v]=min(low[v],low[u]);
if(low[u]>=dfn[v])
{
isGe[v]=true;
while(true)
{
block[st[top]]++;
if(st[top--]==u)
break;
}
block[v]++;
}
}
else
{
low[v]=min(low[v],dfn[u]);
}
}
}
void addEdge(int u,int v)
{
next[tot]=head[u],to[tot]=v,head[u]=tot++;
next[tot]=head[v],to[tot]=u,head[v]=tot++;
}
int main()
{
//freopen("in.txt","r",stdin);
int u,v;
int T=0;
while(scanf("%d",&u),u)
{
cnt=0,tot=0;
memset(isGe,0,sizeof(isGe));
memset(block,0,sizeof(block));
memset(head,-1,sizeof(head));
memset(node,0,sizeof(node));
memset(dfn,0,sizeof(dfn));
scanf("%d",&v);
addEdge(u,v);
while(scanf("%d",&u),u)
{
scanf("%d",&v);
addEdge(u,v);
node[u]=1,node[v]=1;
}
printf("Network #%d\n",++T);
for(int i=1;i<maxn;i++)
{
top=0;
if(!dfn[i]&&node[i])
{
cutBlock(i);
}
}
bool pf=1;
for(int i=1;i<maxn;i++)
{
if(isGe[i]&&node[i]&&block[i]>1)
{
printf(" SPF node %d leaves %d subnets\n",i,block[i]);
pf=0;
}
}
if(pf)
printf(" No SPF nodes\n");
printf("\n");
}
}