UVA 315 315 - Network(求割点个数)
| 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 用来测试模板的 这里输入用了strtok的技巧
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <string.h>
using namespace std; /*
* 求 无向图的割点和桥
* 可以找出割点和桥,求删掉每个点后增加的连通块。
* 需要注意重边的处理,可以先用矩阵存,再转邻接表,或者进行判重
*/
const int MAXN = ;
const int MAXM = ;
struct Edge
{
int to,next;
bool cut;//是否为桥的标记
}edge[MAXM];
int head[MAXN],tot;
int Low[MAXN],DFN[MAXN],Stack[MAXN];
int Index,top;
bool Instack[MAXN];
bool cut[MAXN];
int add_block[MAXN];//删除一个点后增加的连通块
int bridge; void addedge(int u,int v)
{
edge[tot].to = v;edge[tot].next = head[u];edge[tot].cut = false;
head[u] = tot++;
} void Tarjan(int u,int pre)
{
int v;
Low[u] = DFN[u] = ++Index;
Stack[top++] = u;
Instack[u] = true;
int son = ;
for(int i = head[u];i != -;i = edge[i].next)
{
v = edge[i].to;
if(v == pre)continue;
if( !DFN[v] )
{
son++;
Tarjan(v,u);
if(Low[u] > Low[v])Low[u] = Low[v];
//桥
//一条无向边(u,v)是桥,当且仅当(u,v)为树枝边,且满足DFS(u)<Low(v)。
if(Low[v] > DFN[u])
{
bridge++;
edge[i].cut = true;
edge[i^].cut = true;
}
//割点
//一个顶点u是割点,当且仅当满足(1)或(2) (1) u为树根,且u有多于一个子树。
//(2) u不为树根,且满足存在(u,v)为树枝边(或称父子边,
//即u为v在搜索树中的父亲),使得DFS(u)<=Low(v)
if(u != pre && Low[v] >= DFN[u])//不是树根
{
cut[u] = true;
add_block[u]++;
}
}
else if( Low[u] > DFN[v])
Low[u] = DFN[v];
}
//树根,分支数大于1
if(u == pre && son > )cut[u] = true;
if(u == pre)add_block[u] = son - ;
Instack[u] = false;
top--;
} void solve(int N)
{
memset(DFN,,sizeof(DFN));
memset(Instack,false,sizeof(Instack));
memset(add_block,,sizeof(add_block));
memset(cut,false,sizeof(cut));
Index = top = ;
bridge = ;
for(int i = ;i <= N;i++)
if(!DFN[i])
Tarjan(i,i);
int ans = ;
for(int i = ;i <= N;i++)
if(cut[i])
ans++;
printf("%d\n",ans);
}
void init()
{
tot = ;
memset(head,-,sizeof(head));
}
int g[][];
char buf[];
int main()
{
int n;
while(scanf("%d",&n)== && n)
{
gets(buf);
memset(g,,sizeof(g));
while(gets(buf))
{
if(strcmp(buf,"")==)break;
char *p = strtok(buf," ");
int u;
sscanf(p,"%d",&u);
p = strtok(NULL," ");
int v;
while(p)
{
sscanf(p,"%d",&v);
p = strtok(NULL," ");
g[u][v]=g[v][u]=;
}
}
init();
for(int i = ;i <= n;i++)
for(int j = i+;j <= n;j++)
if(g[i][j])
{
addedge(i,j);
addedge(j,i);
}
solve(n);
}
return ;
}
UVA 315 315 - Network(求割点个数)的更多相关文章
- POJ 1144 Network(tarjan 求割点个数)
Network Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 17016 Accepted: 7635 Descript ...
- B - Network---UVA 315(无向图求割点)
A Telephone Line Company (TLC) is establishing a new telephone cable network. They are connectin ...
- POJ1144Network(求割点个数)
题目链接 题意:一共n割点,然后若干行,每行第一个输入一个点,然后若干个点表示与他相连,0单独一行表示一个样例的结束.然后求图中的割点个数 割点:去掉该点之后得到的图不在连通,那么该店就是割点 一般割 ...
- loj 1063(求割点个数)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26780 思路:判断一个点是否是割点的两个条件:1.如果一个点v是根 ...
- UVA-315 无向图求割点个数
题意抽象: 给定一个无向图,输出割点个数. 割点定义:删除该点后,原图变为多个连通块. 考虑一下怎么利用tarjan判定割点: 对于点u和他相连的当时还未搜到的点v,dfs后如果DFN[u]<= ...
- poj 1144(求割点个数)
题目链接:http://poj.org/problem?id=1144 思路:判断一个点是否是割点的两个条件:1.如果一个点v是根结点并且它的子女个数大于等于2,则v是割点.2.如果点v不是根结点,并 ...
- (POJ 3694) Network 求桥个数
题目链接:http://poj.org/problem?id=3694Description A network administrator manages a large network. The ...
- UVA 10369 - Arctic NetWork (求最小生成树)
题意: 在南极有 N 个科研站,要把这些站用卫星和无线电连接起来,是的任意两个之间都能互相通信,如果其中任意的一个地方安装了卫星,那么就可以和其他安装卫星的互相通信,和距离没有关系,但是安装无线电 ...
- HDU2485Destroying the bus stations 拆点网络流求割点个数
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=2485 题目要求:删除最少的点,使得源点到汇点的距离大于k 思路:拆点.建图求费用小于等于k的最大流 # ...
随机推荐
- JAVA文件中获取路径及WEB应用程序获取路径方法
JAVA文件中获取路径及WEB应用程序获取路径方法 1. 基本概念的理解 `绝对路径`:你应用上的文件或目录在硬盘上真正的路径,如:URL.物理路径 例如: c:/xyz/test.txt代表了tes ...
- [HDOJ1827]Summer Holiday(强连通分量,缩点)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1827 缩点后统计入度和当前强连通分量中最小花费,然后记录入度为0的点的个数和花费和就行了. /* ━━ ...
- 关于引用mshtml的问题
今天看了个验证码识别的代码,其中引用到了mshtml.dll,找了半天原来就是microsoft.mshtml.dll.查这个dll的时候还发现了好几篇关于这个dll添加问题的文章.顺便看了下,原来这 ...
- ViewPager 滑动页(三)
需求:滑动展示页,能够使用本地数据,及获取服务器数据进行刷新操作,当滑动到最后一页时,结束当前activity,进入下一个activity: 效果图: 实现分析: 1.目录结构: 代码实现: 1.Po ...
- hdu 2544最短路——最短路的初次总结 UESTC 6th Programming Contest Online
这是一道标准的模板题,所以拿来作为这一段时间学习最短路的总结题目. 题意很简单: 有多组输入数据,每组的第一行为两个整数n, m.表示共有n个节点,m条边. 接下来有m行,每行三个整数a, b, c. ...
- Docker管理面板Crane开源了!
导读 数人云容器管理面板 Crane 开源啦!Crane 包含着数人云工程师对 Docker 最新技术的热爱和实践.希望借助开源社区的力量,让 Crane 完善自身,更好地成长起来,让更多的国内用户体 ...
- HDU5045-Contest(状压dp)
题意: 有n个学生,m道题,给出每个同学解出m个问题的概率,在解题过程中每个学生的解题数的差不大于1,求最大能解出题目数的期望 分析: n很小,知道用状压,但是比赛没做出来(脑子太死了,有一个限制条件 ...
- Hadoop 学习之 FAQ
在Hadoop的学习与使用过程中同样如此.这里为大家分享Hadoop集群设置中经常出现的一些问题,以下为译文: 1.Hadoop集群可以运行的3个模式? 单机(本地)模式 伪分布式模式 全分布式模式 ...
- 在Lua里写unity游戏笔记
gameobject.GetComponent<Transform>(); 翻译成Lua: gameObject:GetComponent (luanet.ctype (Transform ...
- LeetCode题解——3Sum
题目: 给定一个数组,找出其中和为0的所有3个数的组合.每个组合的3个数都是非递降的. 解法: 先排序再遍历,设置3个指针,第一个依次遍历,第二三个在第一个指针后面的部分里,左右夹逼查找和为第一个数的 ...