Network UVA - 315(求割点,)
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
题意:无向图求割点,割点的概念:在一个无向图中,如果有一个顶点集合,删除这个顶点集合以及这个集合中所有顶点相关联的边以后,图的连通分量增多,就称这个点集为割点集合。此次引入一个大神很好的博客[博客链接]https://www.cnblogs.com/en-heng/p/4002658.html
(树边:(父子边)可理解为在DFS过程中访问未访问节点时所经过的边。
回边:(返祖边、后向边),可理解为在DFS过程中遇到已访问节点时所经过的边。)
这是求割点的过程,算法的时间复杂度应与DFS相同,为O(V+E)。
#include<stdio.h>
#include<string.h>
#include<stack>
#include<algorithm>
using namespace std;
const int MAXN = 105;
struct Edge{int v, next;}e[MAXN*MAXN];
int Head[MAXN], cnt;
void AddEgde(int u, int v)
{
e[cnt].v = v;
e[cnt].next = Head[u];
Head[u] = cnt++;
}
int f[MAXN], nRootSons;///记录父亲节点,和根节点的子树个数
int blsCutVetext[MAXN];///是否属于割点
int Low[MAXN], Dfn[MAXN], Index;///low保存深搜最早能够到达的点
void InIt(int N)///初始化
{
cnt = nRootSons = Index = 0;
for(int i=0; i<=N; i++)
{
Head[i] = -1;
blsCutVetext[i] = false;
Dfn[i] = false;
}
}
void Tarjan(int u, int father)
{
f[u] = father;
Low[u] = Dfn[u] = ++Index;
for(int j=Head[u]; j!=-1; j=e[j].next)
{
int v = e[j].v;
if( !Dfn[v] )//是树边,也就是没有dfs过的边
{
Tarjan(v, u);
Low[u] = min(Low[u], Low[v]);
}
else if( v != father)//是回边
Low[u] = min(Low[u], Dfn[v]);
}
}
int main()
{
int N;
while(scanf("%d", &N), N)
{
int i, u, v; char End;
InIt(N);
while(scanf("%d", &u), u)
{
while(1)
{
scanf("%d%c", &v, &End);
AddEgde(u, v);
AddEgde(v, u);
if(End == '\n')
break;
}
}
Tarjan(1, 0);
for(i=1; i<=N; i++)
{
u = f[i];
if(u == 1)
nRootSons++;
else if( Dfn[u] <= Low[i] )
blsCutVetext[u] = true;
}
if(nRootSons > 1)
blsCutVetext[1] = true;
int ans = 0;
for(i=1; i<=N; i++)
{
if(blsCutVetext[i] == true)
ans++;
}
printf("%d\n", ans);
}
return 0; }