https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=251

 Network 

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 mostN 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
 
题目大意:
给你一个无向图,求其中割点的个数目。
输入数据
第一行一个 n 代表有 n 个点
接下来有多行,一直到读入一个 0,算整个地图的读入结束,再读入一个0,文件数据结束。
每行有第一个数字a,代表接下来的数字都 和 a 相连。 
 
 
割点:无向连通图中,如果删除某点后,图变成不连通了,则称该点为割点。
这里割点 和 桥 都是无向图里的概念,大家在这里不要混淆了。
 
求割点
一个顶点u是割点,当且仅当满足(1)或(2)
(1) u为树根,且u有多于一个子树。  
即代码中rootson >1
(2) u不为树根,且满足存在(u,v)为树枝边(或称 父子边,即u为v在搜索树中的父亲),使得 dfn(u)<=low(v)。(也就是说 V 没办法绕过 u 点到达比 u dfn要小的点)   即代码中  if(dfn[v] <= low[i])   Cut[i] = true;
注:这里所说的树是指,DFS下的搜索树。
 
求割点 Tarjan里 low  和  dfn
dfn[u]定义和前面类似,但是low[u]定义为u
或者u的子树中能够通过非父子边追溯到的
最早的节点的DFS开始时间
在Tarjan算法求割点我们要加一个数组 f[u], 判断两者是否是父子边
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<vector>
#define N 110
#define min(a, b)(a < b ? a : b) using namespace std; vector<vector<int> >G;
int low[N], dfn[N], f[N];
int n, Time, num;
bool Cut[N]; void Init()
{
G.clear();
G.resize(n + );
memset(low, , sizeof(low));//最先到达该点的时间
memset(dfn, , sizeof(dfn));//该点能到达之前点的最早时间
memset(f, , sizeof(f));//保存一个点的父节点
memset(Cut, false, sizeof(Cut));//判断该是否为割点
Time = num = ;
} void Tarjan(int u, int fa)
{
int len, v, i;
low[u] = dfn[u] = ++Time;
f[u] = fa;
len = G[u].size();
for(i = ; i < len ; i++)
{
v = G[u][i];
if(!dfn[v])
{
Tarjan(v, u);
low[u] = min(low[u], low[v]);
}
else if(fa != v)
low[u] = min(low[u], dfn[v]);
}
} void Solve()
{
int rootson = , i, v;
Tarjan(, );
for(i = ; i<= n ; i++)
{
v = f[i];
if(v == )//i的父节点为根节点
rootson++;//子树
else if(dfn[v] <= low[i])
Cut[v] = true;
}
for(i = ; i <= n ; i++)
if(Cut[i])
num++;
if(rootson > )
num++;
} int main()
{
int a, b;
char ch;
while(scanf("%d", &n), n)
{
Init();
while(scanf("%d", &a), a)
{
while(scanf("%d%c", &b, &ch))
{
G[a].push_back(b);
G[b].push_back(a);
if(ch == '\n')
break;
}
}
Solve();
printf("%d\n", num);
}
return ;
}

uva 315 Network(无向图求割点)的更多相关文章

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

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

  2. B - Network---UVA 315(无向图求割点)

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

  3. poj 1144 Network 无向图求割点

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

  4. UVA 315 Network (模板题)(无向图求割点)

    <题目链接> 题目大意: 给出一个无向图,求出其中的割点数量. 解题分析: 无向图求割点模板题. 一个顶点u是割点,当且仅当满足 (1) u为树根,且u有多于一个子树. (2) u不为树根 ...

  5. 无向图求割点 UVA 315 Network

    输入数据处理正确其余的就是套强联通的模板了 #include <iostream> #include <cstdlib> #include <cstdio> #in ...

  6. UVA - 315 Network(tarjan求割点的个数)

    题目链接:https://vjudge.net/contest/67418#problem/B 题意:给一个无向连通图,求出割点的数量.首先输入一个N(多实例,0结束),下面有不超过N行的数,每行的第 ...

  7. (连通图 模板题 无向图求割点)Network --UVA--315(POJ--1144)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  8. UVA315:Network(求割点)

    Network 题目链接:https://vjudge.net/problem/UVA-315 Description: A Telephone Line Company (TLC) is estab ...

  9. poj 1523"SPF"(无向图求割点)

    传送门 题意: 有一张联通网络,求出所有的割点: 对于割点 u ,求将 u 删去后,此图有多少个联通子网络: 对于含有割点的,按升序输出: 题解: DFS求割点入门题,不会的戳这里

随机推荐

  1. Android下HelloWorld项目的R.java文件介绍

    R.java文件介绍 HelloWorld工程中的R.java文件 package com.android.hellworld; public final class R {     public s ...

  2. 简单易懂的命名空间及use的使用

    最近一段时间在研究php框架,一直想的什么时候才能开发出自己的框架,当然这是为了提升自己的编程水平,同时能把平时学的零散的东西糅合在一块熟练应用.但是开发一个框架根本不知道如何做起,先开发什么,虽然p ...

  3. openVPN使用

    http://www.williamlong.info/archives/3814.html http://openvpn.ustc.edu.cn/ http://www.williamlong.in ...

  4. Qt之自定义界面(实现无边框、可移动)

    简述 UI设计是指对软件的人机交互.操作逻辑.界面美观的整体设计.好的UI设计不仅是让软件变得有个性.有品位,还要让软件的操作变得舒适简单.自由,充分体现软件的定位和特点. 爱美之心人皆有之.其实软件 ...

  5. WebService教程和分析

    1.1.Web Service基本概念 Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求, ...

  6. WIN7开无线

    可以的,WIN7自身就带有这个功能的,不过默认是不开启的,也可以下载一个软件来弄.1.请却仍您使用的操作系统是微软的Windows 7或者Windows server 2008 R2,正版盗版皆可.( ...

  7. HDU 1272 小希的迷宫 (水题)

    题意: 其实就是让你判断一个图是否为树,要求不能有孤立的点(没有这中情况),且只能有1个连通图,且边数+1=点数,且每个点都有边(不可能只有1个点出现). 思路: 有可能出现连续的4个0,也就是有测试 ...

  8. Java [Leetcode 189]Rotate Array

    题目描述: Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the ...

  9. LeetCode: MergekSortedLists

    Title: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its comple ...

  10. poj 2762 Going from u to v or from v to u?

    题目描述:为了让他们的儿子变得更勇敢些,Jiajia和Wind将他们带到一个大洞穴中.洞穴中有n个房间,有一些单向的通道连接某些房间.每次,Wind选择两个房间x和y,要求他们的一个儿子从一个房间走到 ...