Network
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 17016   Accepted: 7635

Description

A Telephone Line Company (TLC) is establishing a new telephone cable network. They are connecting several places numbered by integers from 1 to N . No two places have the same number. The lines are bidirectional and always connect together two places and in each place the lines end in a telephone exchange. There is one telephone exchange in each place. From each place it is
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

The input file consists of several blocks of lines. Each block describes one network. In the first line of each block there is the number of places N < 100. Each of the next at most N lines contains the number of a place followed by the numbers of some places to which there is a direct line from this place. These at most N lines completely describe the network, i.e., each direct connection of two places in the network is contained at least in one row. All numbers in one line are separated

by one space. Each block ends with a line containing just 0. The last block has only one line with N = 0;

Output

The output contains for each block except the last in the input file one line containing the number of critical places.

Sample Input

5
5 1 2 3 4
0
6
2 1 3
5 4 6 2
0
0

Sample Output

1
2

Hint

You need to determine the end of one line.In order to make it's easy to determine,there are no extra blank before the end of each line.

Source

 
分析:
好惭愧啊,现在才会求割点,学习了一个新的算法,tarjan算法,但现在只会用他来求割点和割边....
tarjan学习是参考的这位大佬的博客,写得真的简单易懂
 
code:
邻接矩阵实现:
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<set>
#include<map>
#include<list>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long LL;
int mon1[]= {,,,,,,,,,,,,};
int mon2[]= {,,,,,,,,,,,,};
int dir[][]= {{,},{,-},{,},{-,}}; int getval()
{
int ret();
char c;
while((c=getchar())==' '||c=='\n'||c=='\r');
ret=c-'';
while((c=getchar())!=' '&&c!='\n'&&c!='\r')
ret=ret*+c-'';
return ret;
} #define max_v 1005
int dfn[max_v];
int low[max_v];
int vis[max_v];
int G[max_v][max_v]; int depth,n,m,root,rt_cnt; void init()
{
root=;
depth=;
rt_cnt=;
memset(dfn,-,sizeof(dfn));
memset(G,,sizeof(G));
memset(vis,,sizeof(vis));
memset(low,,sizeof(low));
} void tarjan(int cur,int pa)
{
dfn[cur]=low[cur]=depth++;
for(int i=;i<=n;i++)
{
if(G[cur][i])
{
if(dfn[i]==-)//i没有访问过
{
tarjan(i,cur);
low[cur]=min(low[cur],low[i]);//逐步回溯更新访问过的父节点的low if(cur==root)
rt_cnt++;//统计和根结点直接相连的点的个数,来确定根结点是不是割点
else if(low[i]>=dfn[cur])
vis[cur]=;//标记当前cur点为割点 }else if(i!=pa)//访问过,但不是父节点,更新low
{
low[cur]=min(low[cur],dfn[i]);
}
}
}
}
int main()
{
while(~scanf("%d",&n)&&n)
{
init();
int temp;
while(~scanf("%d",&temp)&&temp)
{
while(getchar()!='\n')
{
int t;
scanf("%d",&t);
G[temp][t]=G[t][temp]=;
}
}
tarjan(,root);
int cnt=;
for(int i=;i<=n;i++)
if(vis[i])
cnt++;
if(rt_cnt>)
cnt++;
printf("%d\n",cnt);
}
return ;
}
邻接链表实现:
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<set>
#include<map>
#include<list>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long LL;
int mon1[]= {,,,,,,,,,,,,};
int mon2[]= {,,,,,,,,,,,,};
int dir[][]= {{,},{,-},{,},{-,}}; int getval()
{
int ret();
char c;
while((c=getchar())==' '||c=='\n'||c=='\r');
ret=c-'';
while((c=getchar())!=' '&&c!='\n'&&c!='\r')
ret=ret*+c-'';
return ret;
} #define max_v 105
int dfn[max_v];
int low[max_v];
int vis[max_v];
vector<int> vv[max_v];
int depth,n,m,root,rt_cnt; void init()
{
root=;
depth=;
rt_cnt=;
memset(dfn,-,sizeof(dfn));
memset(vis,,sizeof(vis));
memset(low,,sizeof(low));
//for(int i=1;i<=n;i++)
// vv[i].clear();
memset(vv,,sizeof(vv));
} void tarjan(int cur,int pa)
{
dfn[cur]=low[cur]=depth++;
for(int i=;i<vv[cur].size();i++)
{
int temp=vv[cur][i];
if(dfn[temp]==-)
{
tarjan(temp,cur);
low[cur]=min(low[cur],low[temp]); if(cur==root)
rt_cnt++;
else if(low[temp]>=dfn[cur])
vis[cur]=; }else if(temp!=pa)
{
low[cur]=min(low[cur],dfn[temp]);
}
}
} int main()
{
while(~scanf("%d",&n)&&n)
{
init();
int temp;
while(~scanf("%d",&temp)&&temp)
{
while(getchar()!='\n')
{
int t;
scanf("%d",&t);
vv[temp].push_back(t);
vv[t].push_back(temp);
}
}
tarjan(,root);
int cnt=;
for(int i=;i<=n;i++)
if(vis[i])
cnt++;
if(rt_cnt>)
cnt++;
printf("%d\n",cnt);
}
return ;
}

POJ 1144 Network(tarjan 求割点个数)的更多相关文章

  1. [poj 1144]Network[Tarjan求割点]

    题意: 求一个图的割点. 输入略特别: 先输入图中点的总数, 接下来每一行首先给出一个点u, 之后给出一系列与这个点相连的点(个数不定). 行数也不定, 用0作为终止. 这样的输入还是要保证以数字读入 ...

  2. poj 1144 (Tarjan求割点数量)

    题目链接:http://poj.org/problem?id=1144 描述 一个电话线公司(简称TLC)正在建立一个新的电话线缆网络.他们连接了若干个地点分别从1到N编号.没有两个地点有相同的号码. ...

  3. poj 1144 Network 无向图求割点

    Network Description A Telephone Line Company (TLC) is establishing a new telephone cable network. Th ...

  4. POJ 1144 Network (求割点)

    题意: 给定一幅无向图, 求出图的割点. 割点模板:http://www.cnblogs.com/Jadon97/p/8328750.html 分析: 输入有点麻烦, 用stringsteam 会比较 ...

  5. poj 1144 Network 【求一个网络的割点的个数 矩阵建图+模板应用】

    题目地址:http://poj.org/problem?id=1144 题目:输入一个n,代表有n个节点(如果n==0就结束程序运行). 在当下n的这一组数据,可能会有若干行数据,每行先输入一个节点a ...

  6. POJ 1523 SPF tarjan求割点

                                                                   SPF Time Limit: 1000MS   Memory Limit ...

  7. UVA 315 315 - Network(求割点个数)

     Network  A Telephone Line Company (TLC) is establishing a new telephone cable network. They are con ...

  8. POJ 3694 Network(Tarjan求割边+LCA)

    Network Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 10969   Accepted: 4096 Descript ...

  9. POJ 1144 Network —— (找割点)

    这是一题找无向图的割点的模板题,割点的概念什么的就不再赘述了.这里讲一下这个模板的一个注意点. dfs中有一个child,它不等于G[u].size()!理由如下: 如上图,1的size是2,但是它的 ...

随机推荐

  1. 你不知道的JavasScript上篇·第四章·混合对象·类

    一.类的理论 1.类的核心概念:多态 是说父类的通用行为可以被子类用更特殊的行为重写 二.类的机制 1.构造函数 类实例是有一个特殊的类方法构造的,这个方法名通常和类名一致: 类构造函数属于类,构造函 ...

  2. 【代码笔记】iOS-performSelector

    代码: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. se ...

  3. NUnit单元测试示例

    单元测试的用法 1.下载NUnit软件 安装后打开界面如图: 2.新建测试项目 添加类库项目并在NuGet管理包中添加NUnit 这里添加NuGet的NUnit包要注意保持版本和之前下载的NUnit软 ...

  4. Js利用Canvas实现图片压缩

    最近做的APP项目涉及到手机拍照上传图片,因为手机拍照的图片通常都比较大,所以上传的时候就会很慢.为此,需要对图片进行压缩处理来优化上传功能.以下是具体实现: /* * 图片压缩 * img 原始图片 ...

  5. [基础知识]row类visible使用

    使用row的visibe属性,要反向遍历rowset,因为如果正向遍历,rowset是实时变化的,行号是错误的.正确代码如下: Local integer &k; For &k = & ...

  6. Android之使用枚举利弊及替代方案

    Android上不应该使用枚举,占内存,应该使用@XXXDef注解来替代 使用 Enum 的缺点 每一个枚举值都是一个对象,在使用它时会增加额外的内存消耗,所以枚举相比与 Integer 和 Stri ...

  7. cuda和gcc版本不兼容

    gcc8.1和cuda9.0版本不兼容,比较坑. 下面是各版本cuda支持的gcc: 从CUDA 4.1版本开始,现在支持gcc 4.5.gcc 4.6和4.7不受支持. 从CUDA 5.0版本开始, ...

  8. datetime24小时格式和12小时格式

    12:DateTime.Now.ToString("hh:mm:ss") 24:DateTime.Now.ToString("HH:mm:ss")

  9. Oracle EBS GL 会计科目报错 GL_ACCESS_SET_LEDGERS

    1.会计科目设置后,总账中找不到对应账簿                                           2.原因是新版本系统物化视图有问题,参照metalink解决方案得知原路径 ...

  10. VS2010/2013 运行是很卡的加速方案

    前段时间为了一个项目而把VS2008换成了VS2010,结果原本就不堪重负的本本跑起VS2010来那更是慢得没话说,于是看了遍VS2010选项,又从网上到处找资料找优化方法,总算使我的VS2010跑得 ...