UVA-315 无向图求割点个数
题意抽象:
给定一个无向图,输出割点个数。
割点定义:删除该点后,原图变为多个连通块。
考虑一下怎么利用tarjan判定割点:
对于点u和他相连的当时还未搜到的点v,dfs后如果DFN[u]<=low[v],那么u是割点。(搜v得到的是一个不会倒卷回来的子图)
另外注意一下tarjan搜索时的起始点如果有多个儿子那么它也是割点。
AC代码:
#include<cstdio>
#include<cstring>
#define rep(i,a,b) for(int i=a;i<=b;++i)
using namespace std;
const int MAXN=;
const int MAXM=*;
int tot=;
int pointer[MAXN],DFN[MAXN],low[MAXN];
bool instack[MAXN];
int Stap[MAXN];
int Stop,cnt=,n;
int par[MAXN];bool iscut[MAXN];
int add_block[MAXN];
struct Edge
{
int to,next,vis;
Edge() {}
Edge(int b,int nxt,int visit) {to=b;next=nxt,vis=visit;}
}edge[MAXM];
inline void addedge(int a,int b)
{
edge[tot]=Edge(b,pointer[a],);
pointer[a]=tot++;
edge[tot]=Edge(a,pointer[b],);
pointer[b]=tot++;
}
void init()
{
memset(pointer,-,sizeof(pointer));
memset(par,-,sizeof(par));
memset(iscut,,sizeof(iscut));
memset(DFN,,sizeof(DFN));
memset(add_block,,sizeof(add_block));
tot=;cnt=;
int k,u;char c;
while(scanf("%d",&u)&&u)
{
while(scanf("%d%c",&k,&c)&&k)
{
addedge(u,k);
if(c=='\n') break;
}
}
}
void tarjan(int u,int pre)
{
int son=;
DFN[u]=low[u]=++cnt;
for(int j=pointer[u];j!=-;j=edge[j].next)
{
int v=edge[j].to;
if(edge[j].vis) continue;
edge[j].vis=;
edge[j^].vis=;
if(!DFN[v])
{
son++;
par[v]=j;
tarjan(v,u);
if(low[v]<low[u]) low[u]=low[v];
if(DFN[u]<=low[v]&&u!=pre)
{
iscut[u]=;
add_block[u]++;
}
}
else if(DFN[v]<low[u]) low[u]=DFN[v];
}
if(u==pre&&son>) iscut[u]=;
if(u==pre) add_block[u]=son-;
}
int main()
{
//freopen("in.txt","r",stdin);
while(scanf("%d",&n)&&n)
{
init();
rep(i,,n) if(!DFN[i]) tarjan(i,i);
int ans=;
rep(i,,n) if(iscut[i]) ans++;
printf("%d\n",ans);
}
return ;
}
UVA-315 无向图求割点个数的更多相关文章
- B - Network - uva 315(求割点)
题意:给一个无向连通图,求出割点的数量. 首先输入一个N(多实例,0结束),下面有不超过N行的数,每行的第一个数字代表后面的都和它存在边,0表示行输入的结束(很蛋疼的输入方式). 分析:割点的模板题 ...
- Network UVA - 315 无向图找割点
题意: 给你一个无向图,你需要找出来其中有几个割点 割点/割项: 1.u不为搜索起点,low[v]>=dfn[u] 2.u为搜索起点,size[ch]>=2 3.一般情况下,不建议在tar ...
- Network UVA - 315(求割点)
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> ...
- UVA 315 Network (模板题)(无向图求割点)
<题目链接> 题目大意: 给出一个无向图,求出其中的割点数量. 解题分析: 无向图求割点模板题. 一个顶点u是割点,当且仅当满足 (1) u为树根,且u有多于一个子树. (2) u不为树根 ...
- uva 315 Network(无向图求割点)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA 315 315 - Network(求割点个数)
Network A Telephone Line Company (TLC) is establishing a new telephone cable network. They are con ...
- 无向图求割点 UVA 315 Network
输入数据处理正确其余的就是套强联通的模板了 #include <iostream> #include <cstdlib> #include <cstdio> #in ...
- B - Network---UVA 315(无向图求割点)
A Telephone Line Company (TLC) is establishing a new telephone cable network. They are connectin ...
- POJ1144Network(求割点个数)
题目链接 题意:一共n割点,然后若干行,每行第一个输入一个点,然后若干个点表示与他相连,0单独一行表示一个样例的结束.然后求图中的割点个数 割点:去掉该点之后得到的图不在连通,那么该店就是割点 一般割 ...
随机推荐
- 2. Dubbo原理解析-Dubbo内核实现之基于SPI思想Dubbo内核实现(转)
转载自 斩秋的专栏 http://blog.csdn.net/quhongwei_zhanqiu/article/details/41577159 SPI接口定义 定义了@SPI注解 public ...
- Guitar Pro 添加装饰音
在使用Guitar Pro进行乐谱弹唱或者自己作曲时,我们经常会碰到在乐谱上出现一些装饰音,那么大家肯定会有问题了,装饰音是什么?如何使用Guitar Pro来添加装饰音呢? 装饰音是用来装饰旋律的临 ...
- Vue监听属性的变化
监听属性的变化watch: { counter: function (nval, oval) { alert('计数器值的变化 :' + oval + ' 变为 ' + nval + '!') }}
- Anaconda安装mysqldb模块
在anaconda里mysqldb是封在mysql-python里的,所以要先在anaconda prompt里运行 conda install mysql-python.(注意要右键选管理员身份)有 ...
- collections.deque
d = collections.deque([]) # 创建双端队列d.append('a') # 在最右边添加一个元素,此时 d=deque('a')d.appendleft('b') # 在最左 ...
- Lab 9-1
Analyze the malware found in the file Lab09-01.exe using OllyDbg and IDA Pro to answer the following ...
- canvas实现点连线动画
给定一系列坐标(x, y)点, 实现将各个点按照先后顺序连接起来的动画.还有两个要求: 1.点与点之间直接用线段连接, 不用考虑曲线 2.动画支持暂停, 继续, 重头开始播放功能 这个功能该怎么实现呢 ...
- matplotlib各图形绘制
2D图形 import numpy as np import pandas as pd from pandas import Series,DataFrame import matplotlib.py ...
- 【原创】一篇学会vue路由配置 、 动态路由 、多层路由(实例)
先来看看效果图: 为了方便讲解,我没有使用vue脚手架,如果需要的,可以留言跟我要.不多说开工: 首先,html先组上 <div id="app"> <div&g ...
- nginx upstream的五种分配方式
Nginx负载均衡选项upstream用法举例 1.轮询(weight=1) 默认选项,当weight不指定时,各服务器weight相同,每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器d ...