poj1144
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 12521 | Accepted: 5760 |
Description
possible to reach through lines every other place, however it need not be a direct connection, it can go through several exchanges. From time to time the power supply fails at a place and then the exchange does not operate. The officials from TLC realized that in such a case it can happen that besides the fact that the place with the failure is unreachable, this can also cause that some other places cannot connect to each other. In such a case we will say the place (where the failure
occured) is critical. Now the officials are trying to write a program for finding the number of all such critical places. Help them.
Input
by one space. Each block ends with a line containing just 0. The last block has only one line with N = 0;
Output
Sample Input
5
5 1 2 3 4
0
6
2 1 3
5 4 6 2
0
0
Sample Output
1
2
Hint
Source
题意:
就要给你一个图,求有多少个割点
题解:
tarjan求割点的模板
何为割点?
也就是题目中的关键点。在一个无向图中,去掉一个点,这个无向图会变成多个子图,那么这个点就叫做割点
同理,割边也是如此,如果去掉一条边,能让无向图变成多个子图,那么这条边叫做割边,所谓的桥。
那么tarjan是如何求的割点的呢?
dfn:代表这个节点的深度
low:代表这个节点能通过反向边能到达到最浅深度(初始设为这个节点的深度)
如果u为割点,当且仅当满足下面的1/2
1、如果u为树根,那么u必须有多于1棵子树
2、如果u不为树根,那么(u,v)为树枝边,当Low[v]>=DFN[u]时。
割点的求法倒是看明白了,条件1的意思是若为根,下面如果只有一颗子树,也就是整个图是强连通,那么去掉根节点,肯定不会变成多个子图,因此也不会成为割点。只有大于一颗子树,去掉根节点,才会有两个或者2个以上的子图,从而才能成为割点
条件2也比较好理解,u不为树根,那么u肯定有祖先,如果存在Low【v】>=DFN【u】时,表示u的子孙只能通过u才能访问u的祖先,这也就是说,不通过u,u的子孙无法访问u的祖先,那么如果去掉u这个节点,就会至少分为两个子图,一个是u祖先,一个是u子孙的。
ps:输入有点麻烦。以换行结尾可以写成while(getchar() != '\n'),其他没什么难度了。
AC代码:
#include<cstdio>
#include<cstring>
#include<vector>
#include<stack>
using namespace std;
#define N 1010
int n,m,cnt,pd,son,cut[N],low[N],dfn[N];
bool vis[N];
stack<int>s;
vector<int>grap[N];
void tarjan(int v){
low[v]=dfn[v]=++pd;
for(int i=;i<grap[v].size();i++){
int w=grap[v][i];
if(!dfn[w]){
tarjan(w);
low[v]=min(low[v],low[w]);
if(low[w]>=dfn[v]&&v!=) cut[v]++;
else if(v==) son++;
}
else{
low[v]=min(low[v],dfn[w]);
}
}
}
int main(){
while(scanf("%d",&n)==&&n){
int u,v;
memset(low,,sizeof low);
memset(dfn,,sizeof dfn);
memset(cut,,sizeof cut);
memset(vis,,sizeof vis);
memset(grap,,sizeof grap);
pd=;son=;cnt=;
while(scanf("%d",&u)==&&u){
while(getchar()!='\n'){
scanf("%d",&v);
grap[u].push_back(v);
grap[v].push_back(u);
}
}
tarjan();
for(int i=;i<=n;i++) if(cut[i]) cnt++;
if(son>) cnt++;
printf("%d\n",cnt);
}
return ;
}
poj1144的更多相关文章
- 【poj1144】 Network
http://poj.org/problem?id=1144 (题目链接) 题意 求无向图的割点. Solution Tarjan求割点裸题.并不知道这道题的输入是什么意思,也不知道有什么意义= =, ...
- [POJ1144][BZOJ2730]tarjan求割点
求割点 一种显然的n^2做法: 枚举每个点,去掉该点连出的边,然后判断整个图是否联通 用tarjan求割点: 分情况讨论 如果是root的话,其为割点当且仅当下方有两棵及以上的子树 其他情况 设当前节 ...
- POJ1144(割点入门题)
Network Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 11378 Accepted: 5285 Descript ...
- poj1144 tarjan求割点
poj1144 tarjan求割点 额,算法没什么好说的,只是这道题的读入非常恶心. 注意,当前点x是否是割点,与low[x]无关,只和low[son]和dfn[x]有关. 还有,默代码的时候记住分目 ...
- poj1144 求不同割点的个数
Network Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 11914 Accepted: 5519 Descript ...
- POJ1144 Network 无向图的割顶
现在打算重新学习图论的一些基础算法,包括像桥,割顶,双连通分量,强连通分量这些基础算法我都打算重敲一次,因为这些量都是可以用tarjan的算法求得的,这次的割顶算是对tarjan的那一类算法的理解的再 ...
- ZOJ1311, POJ1144 Network
题目描述:TLC电话线路公司正在新建一个电话线路网络.他们将一些地方(这些地方用1到N的整数标明,任何2个地方的标号都不相同)用电话线路连接起来.这些线路是双向的,每条线路连接2个地方,并且每个地方的 ...
- poj1144 Network【tarjan求割点】
转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4319585.html ---by 墨染之樱花 [题目链接]http://poj.org/p ...
- POJ1144(割点)
Network Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12551 Accepted: 5771 Descript ...
随机推荐
- Oracle执行计划与统计信息的一些总结
[日期:2011-08-05]来源:Linux社区 作者:wangshengfeng1986211[字体:大 中 小] 2010-07-01 15:03 1.SET AUTOTRACE ON EXP ...
- Upgrading or Redeploying SharePoint 2010 Workflows
While creating several State Machine SharePoint 2010 workflows using visual studio for a client I ha ...
- android res文件夹下面的 values-v11 、 values-v14
values-v11代表在API 11+的设备上,用该目录下的styles.xml代替res/values/styles.xml values-v14代表在API 14+的设备上,用该目录下的styl ...
- iOS第三方类库JSPatch(热更新)
---------------------------------------------------------------------------------------------------- ...
- 使用eclipse遇到问题:the-package-collides-with-a-type
相似问题:http://stackoverflow.com/questions/12236909/the-package-collides-with-a-type
- CentOS下安装实时检测网络带宽的小工具bmon
首先下载rpmforge-release扩展的rpm包 32位操作系统:wget http://www.sudu.us/Tools/bmon/rpmforge-release-0.3.6-1.el5. ...
- Asp.net MVC 4新项目中创建area的后续操作
Asp.net MVC 4新项目中创建area后,往往HomeController与area的HomeController路由发生混淆,需要手工设置一些地方避免mvc无法识别默认路由的状况. 无废话具 ...
- [转]个人源码管理:如何在本机配置自己的SVN Repository (图解)
本文转自:http://blog.csdn.net/wikijava/article/details/6245588 Repository 即源码的集中存放处,所有修改后提交的源码就是保存在这里,并在 ...
- 通过java来批量生成身份证号
通过java来批量生成身份证号,方便来测试程序. package com.diyvc.controller.user; import java.util.Calendar; import java.u ...
- 08_Queue(队列UVa 10128)
问题描述:n(1<=n<=13)个身高均不相等的人站成一排,从左向右看能看见L个人,从右向左看能看见R个人,问这个队列有多少种排法? 问题分析: 1.n个人的身高可设为1~n, 2.设d ...