POJ 1144 Network(Tarjan求割点)
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 12707 | Accepted: 5835 |
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
题目链接:POJ 1144
给你一个无向图求割点数目,学习Tarjan求割点的一道模版题。
无向图在Tarjan算法中有两种情况可以成为割点:
1、当前点是DFS树根,且当前点可以DFS出去的子树(子树不是孩子)不止一棵,即u==root且son>1;
2、当前点不是DFS树根,但是当前点产生的子树中存在节点low[v]>=dfn[u],即这个节点v除非返回u,否则是不具有反向边返回u祖先的能力的,那此时把u拿掉,v就和u的祖先隔开了,u此时就是也是割点,代码有一些部分是可以优化的,但还是喜欢写成最原始的tarjan
代码:
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <sstream>
#include <cstring>
#include <bitset>
#include <string>
#include <deque>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=1010;
struct edge
{
int to;
int nxt;
int id;
};
edge E[N*N];
int head[N],tot;
int dfn[N],low[N],ts,ins[N],iscut[N],st[N*N],top; void init()
{
CLR(head,-1);
tot=0;
CLR(dfn,0);
CLR(low,0);
CLR(ins,0);
CLR(iscut,0);
ts=top=0;
}
inline void add(int s,int t,int id)
{
E[tot].to=t;
E[tot].id=id;
E[tot].nxt=head[s];
head[s]=tot++;
}
void Tarjan(int u,int id,const int &rt)
{
low[u]=dfn[u]=++ts;
ins[u]=1;
st[top++]=u;
int i,v;
int son=0;
for (i=head[u]; ~i; i=E[i].nxt)
{
v=E[i].to;
if(E[i].id==id)
continue;
if(!dfn[v])
{
++son;
Tarjan(v,E[i].id,rt);
low[u]=min<int>(low[u],low[v]);
if(u==rt&&son>1)
iscut[u]=1;
else if(u!=rt&&low[v]>=dfn[u])
iscut[u]=1;
}
else if(ins[v])
low[u]=min<int>(low[u],dfn[v]);
}
if(low[u]==dfn[u])
{
do
{
v=st[--top];
ins[v]=0;
}while (u!=v);
}
}
int main(void)
{
int n,a,b,i;
while (~scanf("%d",&n)&&n)
{
init();
int id=0;
while (scanf("%d",&a)&&a)
{
while (getchar()!='\n')
{
scanf("%d",&b);
add(a,b,id);
add(b,a,id);
++id;
}
}
for (i=1; i<=n; ++i)
if(!dfn[i])
Tarjan(i,-1,i);
int r=0;
for (i=1; i<=n; ++i)
if(iscut[i])
++r;
printf("%d\n",r);
}
return 0;
}
POJ 1144 Network(Tarjan求割点)的更多相关文章
- [poj 1144]Network[Tarjan求割点]
题意: 求一个图的割点. 输入略特别: 先输入图中点的总数, 接下来每一行首先给出一个点u, 之后给出一系列与这个点相连的点(个数不定). 行数也不定, 用0作为终止. 这样的输入还是要保证以数字读入 ...
- poj 1144 (Tarjan求割点数量)
题目链接:http://poj.org/problem?id=1144 描述 一个电话线公司(简称TLC)正在建立一个新的电话线缆网络.他们连接了若干个地点分别从1到N编号.没有两个地点有相同的号码. ...
- poj 1144 Network 无向图求割点
Network Description A Telephone Line Company (TLC) is establishing a new telephone cable network. Th ...
- POJ 1144 Network (求割点)
题意: 给定一幅无向图, 求出图的割点. 割点模板:http://www.cnblogs.com/Jadon97/p/8328750.html 分析: 输入有点麻烦, 用stringsteam 会比较 ...
- poj 1144 Network 【求一个网络的割点的个数 矩阵建图+模板应用】
题目地址:http://poj.org/problem?id=1144 题目:输入一个n,代表有n个节点(如果n==0就结束程序运行). 在当下n的这一组数据,可能会有若干行数据,每行先输入一个节点a ...
- POJ 1523 SPF tarjan求割点
SPF Time Limit: 1000MS Memory Limit ...
- POJ 3694 Network(Tarjan求割边+LCA)
Network Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 10969 Accepted: 4096 Descript ...
- POJ 1144 Network —— (找割点)
这是一题找无向图的割点的模板题,割点的概念什么的就不再赘述了.这里讲一下这个模板的一个注意点. dfs中有一个child,它不等于G[u].size()!理由如下: 如上图,1的size是2,但是它的 ...
- poj 1523 SPF(tarjan求割点)
本文出自 http://blog.csdn.net/shuangde800 ------------------------------------------------------------ ...
随机推荐
- HDU 5317 RGCDQ (数论素筛)
RGCDQ Time Limit: 3000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u Submit Status ...
- AJAX,JSON用户名校验
效果 开发结构 1,src部分有两个包dao和servlet 1.1dao包下有两个数据库工具类文件 SqlHelper.java package org.guangsoft.dao; import ...
- C语言,输入一个正整数,按由大到小的顺序输出它的所有质数的因子(如180=5*3*3*2*2)
#include <iostream> using namespace std; int main() { long num; while(cin >> num){ ){ co ...
- wpa_supplicant.conf
转自:http://w1.fi/gitweb/gitweb.cgi?p=hostap.git;a=blob_plain;f=wpa_supplicant/wpa_supplicant.conf ### ...
- 企业级项目中最常用到的SQL
用SQL语句添加删除修改字段 1.增加字段 alter table docdsp add dspcode char(200) 例如: 表gwamis.d410Sctzmx添加字段f410 ...
- google svn 服务器使用(免费SVN服务器)
转自:http://hi.baidu.com/%C0%AF%B1%CA%B9%A4%D7%F7%CA%D2/blog/item/d6f6c6d7707d81d0a044df5f.html 1. 进入h ...
- 【Android】SlidingMenu属性详解(转)
原文:http://my.eoe.cn/1169143/archive/21892.html SlidingMenu简介:SlidingMenu的是一种比较新的设置界面或配置界面效果,在主界面左滑或者 ...
- 安装完最小化 RHEL/CentOS 7 后需要做的 30 件事情(二)转载自码农网
7. 安装 PHP PHP 是用于 web 基础服务的服务器端脚本语言.它也经常被用作通用编程语言.在最小化安装的 CentOS 中安装 PHP: # yum install php 安装完 php ...
- DEDE织梦常用的调用方法
DEDE织梦常用的调用常规调用: 网站名称调用:<title>{dede:global.cfg_webname/}</title> 网站关键词调用:<meta name= ...
- 【转】reduce端缓存数据过多出现FGC,导致reduce生成的数据无法写到hdfs
转自 http://blog.csdn.net/bigdatahappy/article/details/41726389 转这个目的,是因为该贴子中调优思路不错,值得学习 搜索推荐有一个job,1 ...