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
0Sample Output
1
2
题意:输入一个图,求割点数目。
判断割点的板子题。num数组存dfs搜索时的序列,low存当前节点的子节点能返回的最小根节点。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define EnewPosstr 1e-9
#define newPosI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const int maxn = 10005;
struct Edge {
int to, next;
}edge[maxn];
int dfn,ans;
int head[maxn];
int p[maxn];
int cnt;
int vis[maxn];
int low[maxn], num[maxn];
int child ;
void init() {
cnt = 0;
child = 0;
for (int i = 0; i < maxn; i++)
{
p[i] = 0;
head[i] = -1;
edge[i].next = -1;
vis[i] = 0;
low[i] = 0;
// num[i]=0;
}
dfn = 0;
ans = 0;
}
void add(int u, int v)
{
edge[cnt].to = v;
edge[cnt].next = head[u];
head[u] = cnt++;
}
void dfs(int u,int fa)
{
vis[u] = 1;
num[u] = low[u] = ++dfn;
for (int i = head[u]; i != -1; i = edge[i].next)
{
int f = edge[i].to;
if (!vis[f])
{
if(u==1) //判断根有几个子节点。
child++;
dfs(f, u);
low[u] = min(low[f], low[u]);
//更新当前节点的子节点所能回到的最小根,当搜完一条路径返回时更新(逆序更新)。
if (low[f] >= num[u] && u != 1)
//如果搜索的这条路径没有回边,侧u为割点。
p[u] = 1;
}
else if (num[f] < num[u] && f != fa) //如果发现回边,更新low值。
{
low[u] = min(low[u], num[f]);
}
if (u == 1 && child >= 2)
p[1] = 1;
}
return;
}
int main()
{
int n;
while (cin >> n&&n)
{
init();
int a;
int b;
while (scanf_s("%d",&a)&&a)
{
while (getchar() != '\n')
{
scanf_s("%d", &b);
add(a, b);
add(b, a);
}
}
dfs(1, -1);
for (int i = 1; i <= n; i++)
if (p[i])ans++;
cout << ans << endl;
}
return 0;
}