POJ1144:Network(无向连通图求割点)
题目:http://poj.org/problem?id=1144
求割点。判断一个点是否是割点有两种判断情况:
如果u为割点,当且仅当满足下面的1条
1、如果u为树根,那么u必须有多于1棵子树
2、如果u不为树根,那么(u,v)为树枝边,当Low[v]>=DFN[u]时。
然后根据这两句来找割点就可以了。
模版题,就是题意看不懂。看了题解。这题算是废了,就当贴模版用吧。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <stack>
#define N 1010
using namespace std;
struct node
{
int x,y,next;
} eg[*N];
int tt,head[N],dfn[N],low[N],n,m,ti;
bool f[*N];
void init()
{
tt=;
ti=;
memset(head,-,sizeof(head));
memset(dfn,,sizeof(dfn));
memset(f,false,sizeof(f));
}
void add(int xx,int yy)
{
eg[tt].x=xx;
eg[tt].y=yy;
eg[tt].next=head[xx];
head[xx]=tt++;
}
void tarjan(int u,int fa)
{
dfn[u]=low[u]=ti++;
int child=;
for(int i=head[u]; i!=-; i=eg[i].next)
{
int v=eg[i].y;
if(v==fa) continue;
if(!dfn[v])
{
child++;
tarjan(v,u);
low[u]=min(low[u],low[v]);
if(low[v]>=dfn[u])
{
f[u]=true;
}
}
else //无向图没有横跨边
{
low[u]=min(dfn[v],low[u]);
}
}
if(fa<&&child<) f[u]=false;
}
int main()
{
int u,v;
while(scanf("%d",&n)!=EOF&&n!=)
{
init();
while(scanf("%d",&u)!=EOF&&u)
{
while(getchar()!='\n')
{
scanf("%d",&v);
add(u,v);
add(v,u);
}
}
tarjan(,-);
int sum=;
for(int i=; i<=n; i++)
{
if(f[i])
sum++;
}
printf("%d\n",sum);
}
return ;
}
或者这么写
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <stack>
#define N 1010
using namespace std;
struct node
{
int x,y,next;
} eg[*N];
int tt,head[N],dfn[N],low[N],n,m,ti;
bool f[*N];
void init()
{
tt=;
ti=;
memset(head,-,sizeof(head));
memset(dfn,,sizeof(dfn));
memset(f,false,sizeof(f));
}
void add(int xx,int yy)
{
eg[tt].x=xx;
eg[tt].y=yy;
eg[tt].next=head[xx];
head[xx]=tt++;
}
void tarjan(int u,int fa)
{
dfn[u]=low[u]=ti++;
int child=;
for(int i=head[u]; i!=-; i=eg[i].next)
{
int v=eg[i].y;
if(v==fa) continue;
if(!dfn[v])
{
child++;
tarjan(v,u);
low[u]=min(low[u],low[v]);
if(low[v]>=dfn[u]&&fa!=-)
{
f[u]=true;
}
}
else //无向图没有横跨边
{
low[u]=min(dfn[v],low[u]);
}
}
if(fa<&&child>) f[u]=true;
}
int main()
{
int u,v;
while(scanf("%d",&n)!=EOF&&n!=)
{
init();
while(scanf("%d",&u)!=EOF&&u)
{
while(getchar()!='\n')
{
scanf("%d",&v);
add(u,v);
add(v,u);
}
}
tarjan(,-);
int sum=;
for(int i=; i<=n; i++)
{
if(f[i])
sum++;
}
printf("%d\n",sum);
}
return ;
}
POJ1144:Network(无向连通图求割点)的更多相关文章
- 无向连通图求割点(tarjan算法去掉改割点剩下的联通分量数目)
poj2117 Electricity Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 3603 Accepted: 12 ...
- POJ1523:SPF(无向连通图求割点)
题目:http://poj.org/problem?id=1523 题目解析: 注意题目输入输入,防止PE,题目就是求割点,并问割点将这个连通图分成了几个子图,算是模版题吧. #include < ...
- B - Network - uva 315(求割点)
题意:给一个无向连通图,求出割点的数量. 首先输入一个N(多实例,0结束),下面有不超过N行的数,每行的第一个数字代表后面的都和它存在边,0表示行输入的结束(很蛋疼的输入方式). 分析:割点的模板题 ...
- ZOJ 2588 Burning Bridges(无向连通图求割边)
题目地址:ZOJ 2588 由于数组开小了而TLE了..这题就是一个求无向连通图最小割边.仅仅要推断dfn[u]是否<low[v],由于low指的当前所能回到的祖先的最小标号,增加low[v]大 ...
- Tarjan算法求解无向连通图的割点、割边、点双连通分量和边双连通分量的模板
历时好几天,终于完工了! 支持无向图四种功能:1.割点的求解 2.割边的求解 3.点双连通分量的求解 4.边双连通分量的求解 全部支持重边!!!!全部支持重边!!!!全部支持重边!!!! 测试数据: ...
- LOJ-1308-Ant network(蚂蚁的网络)-求割点分隔开的子图个数及乘积
网上的题解大都模糊,我可能写的也比较模糊吧,讲究看看. 大致题意: 原图没有一个割点时,特殊考虑,至少ans1=2个通风井,方案数n*(n-1)/2; 原图上有多个割点时,每个(由割点限制成几部分的) ...
- Network -UVa315(连通图求割点)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=5&page=sh ...
- 无向连通图求割边+缩点+LCA
Network Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 7082 Accepted: 2555 Descripti ...
- poj 1144 Network【双连通分量求割点总数】
Network Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 11042 Accepted: 5100 Descript ...
随机推荐
- BI产品学习笔记
理解现在--挖掘规律--预测未来------------------------------------------------------精准营销智能风控运营优化 多维分析挖掘预测敏捷BI 分析展示 ...
- callable()
callable() 用于判断一个对象是否是可调用的,函数或类都可以被调用 In [1]: callable('a') Out[1]: False In [2]: def fun(): ...: pa ...
- console.log篇
前言: 从接触变成开始,就用到了神奇的“console.log”,原来其中还有很多不为自己知道的“小秘密”,今天就深入研究一下吧 1.可以F12打开控制台,输入console.log(xxx),就可以 ...
- 开源免费天气预报接口API以及全国所有地区代码[值得收藏]
国家气象局提供的天气预报接口 接口地址: http://www.weather.com.cn/data/sk/101010100.html http://www.weather.com.cn/data ...
- PyQt4布局管理——绝对定位方式
PyQt4中的布局管理器 布局管理器是编程中重要的一部分.所谓布局管理器是指我们在窗口中安排部件位置的方法.布局管理器有两种工作方式:绝对定位方式(absolute positioning)和布局类别 ...
- LeetCode——Consecutive Numbers
Description: Write a SQL query to find all numbers that appear at least three times consecutively. + ...
- 【BZOJ4099】Trapped in the Haybales Gold STL
[BZOJ4099]Trapped in the Haybales Gold Description Farmer John has received a shipment of N large ha ...
- DGbroker故障切换示例
1.主库故障 SQL> startup ORACLE instance started. Total System Global Area bytes Fixed Size bytes Vari ...
- 原生JS去解析地址栏的链接?超好用的解决办法
在做SPA应用程序的时候,往往需要通过地址栏链接的 hash 值来进行业务逻辑: <script type="text/javascript"> //file:///C ...
- KMP的next数组性质运用
hdu2594 Simpsons' Hidden Talents Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 ...