题目链接
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.
一道tarjan求割点的模板吧,怎样的点是割点呢?就是遇到这样的点,它的所有子节点最后也只能回到自己,而去往不了自己更上面的点,遇到这样的点,就是破坏它就可以使得它下面的节点到不了它上面的点了,是一个割点。怎么证明呢?就是dfn[u]≤low[v]即可。当然,对于起点是特殊的,根节点要特殊判断,因为没有比它low[]值更小的了。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN = 105, maxE = 1e4 + 7;
int N, head[maxN], cnt;
struct Eddge
{
int nex, to;
Eddge(int a=-1, int b=0):nex(a), to(b) {}
}edge[maxE];
inline void addEddge(int u, int v)
{
edge[cnt] = Eddge(head[u], v);
head[u] = cnt++;
}
inline void _add(int u, int v) { addEddge(u, v); addEddge(v, u); }
int dfn[maxN], low[maxN], Index;
bool iscut[maxN];
void Tarjan(int u, int fa)
{
dfn[u] = low[u] = ++Index;
int child = 0;
for(int i=head[u], v; ~i; i=edge[i].nex)
{
v = edge[i].to;
if(!dfn[v])
{
Tarjan(v, u);
low[u] = min(low[u], low[v]);
if(low[v] >= dfn[u]) iscut[u] = true;
child++;
}
else if(v != fa && dfn[v] < low[u]) low[u] = dfn[v];
}
if(fa < 0 && child == 1) iscut[u] = false;
}
inline void init()
{
cnt = Index = 0;
memset(head, -1, sizeof(head));
memset(dfn, 0, sizeof(dfn));
for(int i=1; i<=N; i++) iscut[i] = false;
}
int main()
{
while(scanf("%d", &N) && N)
{
init();
int x, y;
char ch;
while(scanf("%d", &x) && x)
{
ch = '\0';
while(ch != '\n')
{
scanf("%d%c", &y, &ch);
_add(x, y);
}
}
for(int i=1; i<=N; i++) if(!dfn[i]) Tarjan(i, -1);
int ans = 0;
for(int i=1; i<=N; i++) ans += iscut[i];
printf("%d\n", ans);
}
return 0;
}