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. HDU3062(2-SAT)

    Party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  2. AsnycLocal与ThreadLocal

    AsnycLocal与ThreadLocal AsnyncLocal与ThreadLocal都是存储线程上下文的变量,但是,在实际使用过程中两者又有区别主要的表现在: AsyncLocal变量可以在父 ...

  3. MySQL入门详解(二)---mysql事务、锁、以及优化

    MySQL 事务主要用于处理操作量大,复杂度高的数据.比如说,在一个商城系统中,用户执行购买操作,那么用户订单中应该加一条,库存要减一条,如果这两步由于意外只进行了其中一步那么就会发生很大的问题.而事 ...

  4. JavaSE——多线程

    进程和线程: 进程是指运行中的应用程序,每一个进程都有自己独立的内存空间.一个应用程序可以启动多个进程. 线程是指进程中的一个执行流程,有时也称为执行情景. 线程和进程的主要区别在于:每个进程都需要操 ...

  5. Difference between nn.softmax & softmax_cross_entropy_with_logits & softmax_cross_entropy_with_logits_v2

    nn.softmax 和 softmax_cross_entropy_with_logits 和 softmax_cross_entropy_with_logits_v2 的区别   You have ...

  6. SQLServer 远程链接MySql数据库详解

    SQLServer 远程链接MySql数据库详解 by:授客 QQ:1033553122 测试环境: Microsoft Windows XP Professional 版本2000 Service ...

  7. Unity Profiler GPU Usage(GPU使用情况)

    一般情况下性能瓶颈都在CPU上,这儿也列举下几个常见的GPU耗时函数吧. 1 Render.Mesh 绘制网格面(没批处理的面) 2 Batch.DrawStatic 静态批处理 3 Batch.Dr ...

  8. python 中* 和**的作用

    先举个 ** 使用的例子: data = {"a": 1, "b": 2} def foo(**kwargs): print kwargs foo(a=1, b ...

  9. LeetCode题解之 Search in a Binary Search Tree

    1.题目描述 2.问题分析 利用递归遍历二叉查找树. 3.代码 TreeNode* searchBST(TreeNode* root, int val) { if (root == NULL) ret ...

  10. 在 Azure Resource Manager 中为虚拟机设置密钥保管库

    Note Azure 具有两种不同的部署模型,用于创建和处理资源:Resource Manager 模型和经典模型.本文介绍使用 Resource Manager 部署模型.Azure 建议对大多数新 ...