求割点
#include<iostream>
#include<map>
#include<algorithm>
#include<stack>
#include<vector>
#include<string>
#include<cstring>
#include<cstdio>
using namespace std;
vector<int>g[1005];
stack<int>st;
int dfn[1005],low[1005],spf[1005],indx;
bool vis[1005];
void tarjan(int i)
{
int j;
low[i]=dfn[i]=++indx;
vis[i]=true;
for(j=0;j<g[i].size();j++)
{
int v=g[i][j];
if(!vis[v])
{
tarjan(v);
if(dfn[i]<=low[v])
{
spf[i]++;
}
else
{
low[i]=min(low[v],low[i]);
}
}
else
{
low[i]=min(low[i],dfn[v]);
}
}
}
int main()
{
int i,x,y;
int t=1;
while(scanf("%d",&x)&&x)
{
indx=0;
for(i=0;i<1005;i++)
{
g[i].clear();
spf[i]=0;
vis[i]=false;
}
scanf("%d",&y);
g[x].push_back(y);
g[y].push_back(x);
while(scanf("%d",&x)&&x)
{
scanf("%d",&y);
g[x].push_back(y);
g[y].push_back(x);
}
int flag=0;
tarjan(1);
spf[1]--;
printf("Network #%d\n",t++);
for(i=1;i<1005;i++)
{
if(spf[i]>0)
{
printf(" SPF node %d leaves %d subnets\n",i,spf[i]+1);
flag=1;
}
}
if(!flag)
printf(" No SPF nodes\n");
printf("\n");
}
return 0;
}
转载于:https://blog.51cto.com/8590696/1358875