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 ...
随机推荐
- MSCRM 仪表盘 控件 数量 更改(Change the maximum no. of controls on MSCRM Dashboards )
The maximum number of controls allowed on MSCRM dashboards are 6. You cannot put the more than 6 gra ...
- 关于SharePoint 的Client object model该何时load和execut query的一点自己的看法
很多人在用client object model的时候,不知道何时或者该不该load,今天看到一个观点描述这个问题,觉得很有道理,和大家分享.那就是写client object model就像写sql ...
- 解决SharePoint 2013 designer workflow 在发布的报错“负载平衡没有设置”The workflow files were saved but cannot be run.
原因是app management service没有设置好,在管理中心把他删掉,重新建一个就可以了 Provision App Management Service In SharePoint 20 ...
- Engine中执行gp工具返回的要素图层如何获取?
来自:http://zhihu.esrichina.com.cn/?/question/12087 Engine中执行gp工具返回的[解决办法]:需要用gpUtils.DecodeFeatureLay ...
- Sharepoint学习笔记—习题系列--70-573习题解析 -(Q125-Q126)
Question 125You are creating an application for SharePoint Server 2010.The application will run on a ...
- iOS小游戏打地鼠
#import "ViewController.h" #import <AudioToolbox/AudioToolbox.h> @interface ViewCont ...
- 真机调试出现Could not find Developer Disk Image问题解决办法
1.升级Xcode 2. 在使用Xcode进行真机调试的时候,有时根据真机的系统不同,会出现could not find developer disk image 错误,这是由于真机系统过高或者过低, ...
- 常用oracle函数
一.逗号拼接字段 SELECT LISTAGG(aa, ',') WITHIN GROUP (ORDER BY aa) AS AA FROM *** where id<5 输出结果例如:1,2, ...
- cordova Process finished with exit code -1
安装完cordova之后,创建一个测试项目后,运行报Process finished with exit code -1,经过查找原因,是因为gradle没有安装,在http://www.androi ...
- windows7+eclipse+hadoop2.5.2环境配置
windows7+eclipse+hadoop2.5.2环境配置 一.hadoop集群环境配置 参考我的前一篇文章(ubuntu + hadoop2.5.2分布式环境配置 http://www. ...