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. HDU3715(二分+2-SAT)

    Go Deeper Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  2. FileWriter剖析

    集合这种容器存储数据,它只能在内存中临时存储,不能永久存储,这样会导致数据的丢失,所以出现了IO流. IO流用来处理设备之间的数据传输.可以用来做复制文件,上传文件,下载文件. 读数据是输入流,写数据 ...

  3. LeetCode题解之Climbing Stairs

    1.题目描述 2.问题分析 使用动态规划. 3.代码 int climbStairs(int n) { ){ return n; } ]; dp[] = ; dp[] = ; dp[] = ; ; i ...

  4. MySQL优化之Explain命令解读,optimizer_trace

    简述: explain为mysql提供语句的执行计划信息.可以应用在select.delete.insert.update和place语句上.explain的执行计划,只是作为语句执行过程的一个参考, ...

  5. jquery.validate,错误信息位置

    好长时间没有用jquery.validate.js这个插件了,忘得差不多了.唉,好东西还是要经常拿出来看看的,今天用jquery.validate来做一个小东西,遇到一个问题,就是错误提示信息的位置问 ...

  6. Regmap 框架:简化慢速IO接口优化性能【转】

    1. 简介 Regmap 机制是在 Linux 3.1 加入进来的特性.主要目的是减少慢速 I/O 驱动上的重复逻辑,提供一种通用的接口来操作底层硬件上的寄存器.其实这就是内核做的一次重构.Regma ...

  7. PHP 8中数据类型

    PHP  一共支持八种数据类型 4种标量数据类型 boolean布尔型   只有两个值  true 和  flase integer整形  包括正整数和负整数,无小数位 float/double 浮点 ...

  8. 一个汇编的HelloWorld!

    花了一下午时间,感觉最坑的是,书写代码的个数和编译器的坑比较多,还各种版本的编译器! 会让人“眼花缭乱”! 主要代码 将文件保存为*.asm include io32.inc .data ;数据 sr ...

  9. 用户不再sudoers文件中

    1.修改/etc/sudoers文件权限 # chmod 777 /etc/sudoers 2.编辑/etc/sudoers文件,添加要提升权限的用户: 在文件中找到root ALL=(ALL) AL ...

  10. javascript,object,IDispatchEx笔记

    //js: var testObj=new Object; //com内部: testObj=Object::InvokeEx(wFlags==DISPATCH_CONSTRUCT); //注: // ...