POJ1144(割点)

Network

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 12551   Accepted: 5771

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.

 1 //2016.9.16
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cstring>
 5 #define N 105
 6
 7 using namespace std;
 8
 9 int n, root, book[N], edge[N][N], num[N], low[N], Index;
10 //book用来记录哪些点是割点,edge以邻接矩阵保存图,num[i]为顶点i的时间戳,low[i]为顶点i不经过父顶点所能回到的最小时间戳
11
12 void dfs(int cur, int fa)
13 {
14     int child = 0;
15     Index++;
16     num[cur] = low[cur] = Index;
17     for(int i = 1; i <= n; i++)
18     {
19         if(edge[cur][i]==1)
20         {
21             if(num[i] == 0)
22             {
23                 child++;
24                 dfs(i, cur);
25                 low[cur] = min(low[cur], low[i]);
26                 if(cur!=root && low[i]>=num[cur])
27                       book[cur] = 1;
28                 if(cur==root && child==2)
29                       book[cur] = 1;
30             }else if(i != fa)
31             {
32                 low[cur] = min(low[cur], num[i]);
33             }
34         }
35     }
36     return;
37 }
38
39 int main()
40 {
41     int u, v;
42     while(scanf("%d", &n) && n)
43     {
44         memset(edge, 0, sizeof(edge));
45         memset(low, 0, sizeof(low));
46         memset(num, 0, sizeof(num));
47         memset(book, 0, sizeof(book));
48         Index = 0; root = 1;
49         while(scanf("%d", &u) && u)
50         {
51             while(getchar() != ‘\n‘)
52             {
53                 scanf("%d", &v);
54                 edge[u][v] = 1;
55                 edge[v][u] = 1;
56             }
57         }
58         dfs(1, root);
59         int cnt = 0;
60         for(int i = 1; i <= n; i++)
61               if(book[i])cnt++;
62         printf("%d\n", cnt);
63     }
64
65     return 0;
66 }
时间: 2024-11-09 20:10:15

POJ1144(割点)的相关文章

poj1144 割点割边问题 dfs暴力

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 togethe

[POJ1144][BZOJ2730]tarjan求割点

求割点 一种显然的n^2做法: 枚举每个点,去掉该点连出的边,然后判断整个图是否联通 用tarjan求割点: 分情况讨论 如果是root的话,其为割点当且仅当下方有两棵及以上的子树 其他情况 设当前节点为u,一个儿子节点为v 存在low[v]>=dfn[u],也就是说其儿子节点v能连到的最前面的点都在u的下面 也就是当u断开的时候,u之前的点与以v为根的子树必然分成两个独立的块 那么这个时候u就是割点 Network A Telephone Line Company (TLC) is estab

poj1144 tarjan求割点

poj1144 tarjan求割点 额,算法没什么好说的,只是这道题的读入非常恶心. 还有,理解tarjan一定要用递归树,配合横边回边前边树边等来想.. #include <cctype> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn=100, maxm=5000; struct Graph{ struct Edge

poj1144 tarjan求割点 裸题

Network Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11684   Accepted: 5422 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

poj1144 Network【tarjan求割点】

转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4319585.html   ---by 墨染之樱花 [题目链接]http://poj.org/problem?id=1144 [题目描述](半天才看明白...)给图求割点个数 [思路]直接套求割点的模板即可,就是要注意输入比较坑.代码见下,附注释 #include <iostream> #include <ios> #include <iomanip> #includ

POJ1144 Network(判断割点)

题目链接"点击打开链接 判断割点的个数 #include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <queue> #include <algorithm> const int N = 210; const int maxn = 500; const int maxm = 21010; const int inf =

【POJ1144】Network(割点)(模板)

题意:给定一张无向图,求割点个数 思路:感谢CC大神http://ccenjoyyourlife.blog.163.com/的讲解 割点的定义就是某个联通块中删去此点连通性发生变化的的点 有两种割点:1.U为树根,子树个数>1 2.U非树根,有U的子节点V满足low[v]>=dfn[u]表示U的V子树必须通过U去到U的上面 更新时也有两种:dfn[u]<dfn[v]时u--->v 实边 反则u--->v 虚边 实边时low[u]=min(low[u],low[v]) 虚边lo

poj1144 求不同割点的个数

Network Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11914   Accepted: 5519 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

【连通图|割点】POJ-1144 Network

Network Time Limit: 1000MS Memory Limit: 10000K 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 li