首页 技术 正文
技术 2022年11月17日
0 收藏 840 点赞 3,122 浏览 987 个字

  这是一题找无向图的割点的模板题,割点的概念什么的就不再赘述了。这里讲一下这个模板的一个注意点。

  dfs中有一个child,它不等于G[u].size()!理由如下:

POJ 1144 Network —— (找割点)

  如上图,1的size是2,但是它的child是1,因为对他进行dfs时,顺序是1-2-3…然后再等到它访问它的第二个节点3时,3已经被访问过了,不能算他的child了,这是一个误区。

  代码如下:

 #include <stdio.h>
#include <stack>
#include <algorithm>
#include <string.h>
#include <vector>
using namespace std; const int N = +; stack<int> S;
int scc_cnt;
int dfs_clock;
int dfn[N];
int low[N];
int iscut[N];
vector<int> G[N]; void dfs(int u,int fa)
{
dfn[u]=low[u]=++dfs_clock;
int child = ;
for(int i=;i<G[u].size();i++)
{
int v = G[u][i];
if(!dfn[v])
{
child++;
dfs(v,u);
low[u]=min(low[u],low[v]);
if(low[v]>=dfn[u]) iscut[u]=;
}
else if(dfn[v]<dfn[u] && v!=fa)
{
low[u]=min(low[u],dfn[v]);
}
}
if(fa == - && child == ) iscut[u]=;
} int main()
{
int n;
while(scanf("%d",&n)== && n)
{
memset(dfn,,sizeof(dfn));
memset(iscut,,sizeof(iscut));
dfs_clock=;
for(int i=;i<=n;i++) G[i].clear(); int x;
while(scanf("%d",&x)== && x)
{
while(getchar() != '\n')
{
int y;
scanf("%d",&y);
G[x].push_back(y);
G[y].push_back(x);
}
} dfs(,-);
int cnt=;
for(int i = ;i<=n;i++) if(iscut[i]) cnt++;
printf("%d\n",cnt);
}
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,581
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918