链接:
https://cn.vjudge.net/problem/UVA-315
题目:
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 o cials 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 o cials are trying to write a program for nding the number of all such critical places. Help them.
Input
The input le consists of several blocks of lines. Each block describes one network. In the rst 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 le 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
题意:
给定一个无向图,问共存在多少个割点。
思路:
割点:去掉此点后此图会断开连接。一种是第一次搜索的根节点,若其子节点数超过两个,则此点去掉后图会断开连接,因此此点为割点;或者此点为搜索树的子节点,若其子节点的最早达到时间状态比其自身要晚,则说明此点不得不经过,并以此来更新其子节点,故其也是割点。
实现:
#include <iostream>
#include <algorithm>
#include <set>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <functional>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <climits>
#include <cctype>
using namespace std;
#define maxn 107
#define memset(a,b) memset(a,b,sizeof a)
struct {
int next,to;
}edge[maxn*maxn];
bool instack[maxn],cut[maxn];
int head[maxn],DFN[maxn],low[maxn],tot,ord,root;
void addedge(int from, int to) {
edge[tot].to = to;
edge[tot].next = head[from];
head[from] = tot++;
}
void tarjan(int u, int fa) {
DFN[u] = low[u] = ++ord;
instack[u] = true;
int cnt = 0;
for(int i=head[u] ; i!=-1 ; i = edge[i].next) {
int v = edge[i].to;
if(v == fa) continue;
if(DFN[v] == -1) {
tarjan(v, u);
cnt++;
low[u] = min(low[u],low[v]);
if((root == u && cnt > 1) || (u != root && low[v] >= DFN[u])) cut[u] = true;
} else if(instack[v]) low[u] = min(low[u], DFN[v])/*,instack[v] = false*/;
}
}
void init() {
memset(DFN,-1);
memset(low,0);
memset(instack,false);
memset(cut,false);
memset(head,-1);
tot = ord = 0;
}
void solve(int n) {
root = 1;
tarjan(1,-1);
int ans = 0;
for(int i=1 ; i<=n ; i++) if(cut[i]) ans++;
printf("%d\n",ans);
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
//ios_base::sync_with_stdio(false);cin.tie(0);
int n,u,v;
while(scanf("%d",&n), n != 0) {
init();
while(scanf("%d",&u), u != 0) {
while(getchar() != '\n') {
scanf("%d",&v);
addedge(u,v);
addedge(v,u);
}
}
solve(n);
}
return 0;
}