# [POJ 1144] Network【Tarjan割点】

• 2018-01-09
• 0
• 0

## Problem:

 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 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.

Source

Central Europe 1996

## Solution:

Tarjan 求割点的模板题。

1. 根节点是割点，当且仅当它至少有 2 棵子树。此时若删去该节点，它的任意两棵子树不再连通
2. 非根节点是割点，当且仅当它的某一棵子树没有指向它的祖先的边。此时若删去该节点，该子树与其他部分不再连通

• low[v] ≥ dfn[u]

## Code: O(T(N+M)), M为总边数 [144K, 16MS]

```#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;

int N;

struct Edge{
int np;
Edge *nxt;
};

struct Graph{
Edge *V[102], E[10002];
int tope;

inline void clear() {tope = 0, memset(V, 0, sizeof(V));}

inline void addedge(int u, int v) {E[++tope].np = v, E[tope].nxt = V[u], V[u] = &E[tope];}
} G;

int dfn[102], dfstime;
int low[102];
int vis[102], rootchild;
bool Artic[102];

inline void Tarjan(int u){
dfn[u] = low[u] = ++dfstime, vis[u] = 1;
for(register Edge *ne = G.V[u]; ne; ne = ne->nxt){
if(!vis[ne->np]){
Tarjan(ne->np);
if(u == 1) {if(++rootchild > 1) Artic[1] = 1;}
else{
if(low[ne->np] >= dfn[u]) Artic[u] = 1;
low[u] = min(low[u], low[ne->np]);
}
}
else low[u] = min(low[u], dfn[ne->np]);
}
}

int main(){
while(scanf("%d", &N) != EOF && N){
{
int u, v;
G.clear();
while(scanf("%d", &u) != EOF && u){
while(getchar() != '\n'){
scanf("%d", &v);
}
}
}  // Limit the action scope of the variables u and v
dfstime = 0, rootchild = 0;
memset(vis, 0, sizeof(vis));
memset(Artic, 0, sizeof(Artic));
for(register int i = 1; i <= N; i++)
if(!vis[i]) Tarjan(i);
int ans = 0;
for(register int i = 1; i <= N; i++)
if(Artic[i]) ans++;
printf("%d\n", ans);
}
return 0;
}
```

#### 评论

darkleafin.cf
(该域名已过期且被抢注。。)
darkleafin.github.io

49750

https://github.com/Darkleafin

OPEN AT 2017.12.10

Please refresh the page if the code cannot be displayed normally.

https://visualgo.net/en

- Theme by Qzhai