Network (poj1144)

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 #include<iostream>
 2 #include<string.h>
 3 #include<algorithm>
 4 #include<queue>
 5 #include<math.h>
 6 #include<stdlib.h>
 7 #include<stack>
 8 #include<stdio.h>
 9 #include<ctype.h>
10 #include<map>
11 #include<vector>
12 using namespace std;
13 vector<int>vec[1000];
14 char ans[10000];
15 bool flag[10000];
16 int pre[1000];
17 int low[1000];
18 int tr[1000];
19 int sizee = 0;
20 int dfs(int u,int fa);
21 int main(void)
22 {
23     int n;
24     while(scanf("%d",&n),n!=0)
25     {
26         sizee = 0;
27         int t;
28         memset(flag,0,sizeof(flag));
29         memset(pre,0,sizeof(pre));
30         memset(low,0,sizeof(low));
31         memset(tr,0,sizeof(tr));
32         for(int i = 0; i < 1000; i++)
33             vec[i].clear();
34         while(scanf("%d",&t),t!=0)
35         {
36             int i,j;
37             int id;
38             gets(ans);
39             int  l = strlen(ans);
40             int sum = 0;
41             for(i = 0; i <= l; )
42             {
43                 if(ans[i]>=‘0‘&&ans[i]<=‘9‘)
44                 {
45                     sum = 0;
46                     for(j = i; ans[j]!=‘ ‘&&ans[j]!=‘\0‘&&j <= l; j++)
47                     {
48                         sum = sum*10;
49                         sum+=ans[j]-‘0‘;
50                     }
51                     i = j;
52                     vec[t].push_back(sum);
53                     vec[sum].push_back(t);
54                 }
55                 else i++;
56             }
57         }
58         dfs(1,-1);
59         int sum = 0;
60         for(int i = 1; i <= n; i++)
61         {
62             sum+=tr[i];
63         }
64         printf("%d\n",sum);
65     }
66     return 0;
67 }
68 int dfs(int u,int fa)
69 {
70     pre[u] = low[u] = ++sizee;
71     int child = 0;
72     for(int i = 0; i < vec[u].size(); i++)
73     {
74         int ic  = vec[u][i];
75         if(!pre[ic])
76         {
77             child++;
78             int lowv = dfs(ic,u);
79             low[u] = min(low[u],lowv);
80             if(lowv >= pre[u])
81             {
82                 tr[u] = 1;
83             }
84         }
85         else if(pre[ic] < pre[u]&&ic!=fa)
86         {
87             low[u] = min(low[u],pre[ic]);
88         }
89     }
90     if(fa < 0&& child == 1)tr[u] = 0;
91     return low[u];
92 }
时间: 2024-10-28 09:36:08

Network (poj1144)的相关文章

【连通图|割点】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

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

来源:Central Europe 1996 思路:Tarjan求割点. 一个点$x$为割点当且仅当: 1.$x$为根结点且有两棵不相交的子树. 2.$x$不为根结点且它的子树中没有可以返回到$x$的祖先的边. 实现细节: 当$x$为根结点时,不能单纯地统计它的度,而是应该统计其不相交子树的个数,因为如果刚好是一个环,每个点的度都是$2$,但去掉这个点以后还是连通的. 1 #include<cstdio> 2 #include<vector> 3 #include<cstri

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

链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=251 http://poj.org/problem?id=1144 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82833#problem/B 代码: #include<cstdio> #include<

POJ1144 Network 题解 点双连通分量(求割点数量)

题目链接:http://poj.org/problem?id=1144 题目大意:给以一个无向图,求割点数量. 这道题目的输入和我们一般见到的不太一样. 它首先输入 \(N\)(\(\lt 100\))表示点的数量(\(N=0\)表示文件输入结束). 然后接下来每行输入一组数字. 如果这一组数字只包含一个 \(0\) ,说明本组测试数据输入结束: 否则,假设这些数可以拆分成 \(a_1,a_2,a_3, \cdots ,a_m\),则说明 \(a_1\) 这个点到 \(a_2,a_3, \cdo

求无向图的割点 (poj 1144 Network)

割点 :去掉该点后原来的图不连通(出现好几个连通分量),该点被称为割点. 注意删除某点意味着和该点关联的边也全部删除 求割点的伪代码 DFS(v1,father): dfn[v1] = low[v1] = ++dfsClock vis[v1] = true child = 0 for each egde(v1,v2) in E: if(vis[v2] == false) : //(v1,v2)是父子边 DFS(v2,v1) child++ low[v1] = Min(low[v1],low[v2

[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