首页 技术 正文
技术 2022年11月10日
0 收藏 796 点赞 4,608 浏览 1630 个字

题目链接:http://poj.org/problem?id=1523

SPF:A Single Point of Failure也就是割点(一个点导致网络之间的不连通),由于给出的图是无向图,所以只要连通就一定强连通。要求连通分支的数量就是要求请联通分支的数量,我们可想到tarjan求强连通的步骤,只要一群结点的low值相同他们就是属于同一个SCC(Strongly Connected Component),所以我们只要对于每一个割点,记录一下这个点所到的其他结点的不相同的low值的数量,就是这个点能够将网络分成的连通分支的数量。因为在dfs树上,如果一个根结点是割点的话,对于他的每一个子节点,我们进行一次深搜(没访问过的),并且标记访问,表上记号,等到所有的子结点全部搜完就记号的数量就是强连通分量的个数。

代码如下:

 #include<cstdio>
#include<string.h>
#include<set>
#include<iostream>
using namespace std;
const int maxn =1e3+;
int head[maxn],nxt[maxn],iscut[maxn],dfn[maxn],low[maxn];
bool vis[maxn];
struct node{
int u,v;
}p[maxn];
int e;
int n,cnt;
void addedge(int u,int v)
{
p[e].u=u;
p[e].v=v;
nxt[e]=head[u];
head[u]=e++;
}
void tarjan(int u,int fa)
{
dfn[u]=low[u]=++cnt;//dfn为dfs时间戳,low为回退边指向的最小祖先
int child=;
for(int i=head[u];~i;i=nxt[i])
{
int v=p[i].v;
if(!dfn[v])
{
tarjan(v,u);
low[u]=min(low[u],low[v]);
child++;
if(u!=&&low[v]>=dfn[u])iscut[u]=;
}
else if(dfn[v]<dfn[u]&&v!=fa)//回退边
{
low[u]=min(low[u],dfn[v]);
}
}
if(u==&&child>=)iscut[]=;
}
int main()
{
int x,y;
int step=;
while(scanf("%d",&x)&&x)
{
scanf("%d",&y);
n=;
memset(head,-,sizeof(head));
memset(nxt,-,sizeof(nxt));
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
memset(iscut,,sizeof(iscut));
memset(vis,,sizeof(vis));
cnt=;
e=;
n=max(max(x,y),n);
addedge(x,y);addedge(y,x);
while(scanf("%d",&x)&&x)
{
scanf("%d",&y);
n=max(max(x,y),n);
addedge(x,y);addedge(y,x);
}
tarjan(,-);//从结点一开始建立dfs树
set<int> s;
bool flag=false;
printf("Network #%d\n",++step);
for(int i=;i<=n;i++)
{
s.clear();
if(iscut[i])//对于每一个割点查找连通分量的数量
{
flag=true;
for(int j=head[i];~j;j=nxt[j])
{
s.insert(low[p[j].v]);//搜索不同的low值数量
}
}
if(s.size())
printf(" SPF node %d leaves %d subnets\n",i,s.size());
}
if(!flag)printf(" No SPF nodes\n");
printf("\n");
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,494
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,133
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,297