# poj 1144 Network 【求无向图中割点总数】【点双联通入门题目】

304人阅读 评论(0)

﻿﻿
Network
 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 10441 Accepted: 4850

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.

tarjan算法：只需注意输入字符串时建边的处理

#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <algorithm>
#define MAXN 1000+10
#define MAXM 2000000+10
using namespace std;
struct Edge
{
int from, to, next;
}edge[MAXM];
bool iscut[MAXN];//判断是否为割点
int low[MAXN];//从当前节点或它的子孙出发通过回边可以到达的最低深度优先数
int dfn[MAXN];//记录该点在DFS树中的深度优先数
int recdfn;//记录当前的深度优先序数
void init()
{
top = 0;
}
{
Edge E = {u, v, head[u]};
edge[top] = E;
Edge E1 = {v, u, head[v]};
edge[top] = E1;
}
void getMap(int m)
{
int a, b;
while(m--)
{
scanf("%d%d", &a, &b);
}
}
void tarjan(int u, int fa)//u为当前节点 fa为其父节点
{
low[u] = dfn[u] = ++recdfn;
int son = 0;//记录子节点数目
for(int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if(!dfn[v])//没有查询过
{
son++;
tarjan(v, u);//求low[v]
low[u] = min(low[u], low[v]);//取子节点中low最小值
if(u != fa && low[v] >= dfn[u])//该割点不是根节点
iscut[u] = true;
}
else low[u] = min(low[u], dfn[v]);//更新通过回边到达的最低深度优先数
}
if(u == fa && son > 1)//割点是根节点 且子节点数目大于1
iscut[u] = true;
}
void find_cut(int l, int r)//节点最小的编号 到 节点最大的编号
{
memset(low, 0, sizeof(low));
memset(dfn, 0, sizeof(dfn));
memset(iscut, 0, sizeof(iscut));
recdfn = 0;
for(int i = l; i <= r; i++) if(!dfn[i]) tarjan(i, i);
}
int main()
{
int N;
int a, b;
int i, l;
char str[5010];
while(scanf("%d", &N), N)
{
init();
while(gets(str), strcmp(str, "0"))
{
l = strlen(str);
a = 0; i = 0;
while(str[i] != ' ')
{
a = a * 10 + str[i] - '0';
++i;
}
for( ; i < l; i++)
{
if(str[i] != ' ')//空格ascll码为32
{
b = 0;
while(str[i] != ' ' && i < l)
{
b = b * 10 + str[i] - '0';
++i;
}
}
}
}
find_cut(1, N);
int ans = 0;
for(i = 1; i <= N; i++)
{
if(iscut[i])
ans++;
}
printf("%d\n", ans);
}
return 0;
} 

#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <algorithm>
#define MAXN 100+10
#define MAXM 10000+10
using namespace std;
struct Edge
{
int from, to, next;
}edge[MAXM];
int dfn[MAXN];//该点的深度优先数
int low[MAXN];//从该点或它的子孙出发 通过回边可以到达的最低深度优先数
bool iscut[MAXN];//该点是否为割点
int belong[MAXN];//该点属于哪个bcc
int dfs_clock, bcc_cnt;
stack<Edge> S;//存储当前bcc中的边
vector<int> bcc[MAXN];
void init()
{
top = 0;
}
{
Edge E = {u, v, head[u]};
edge[top] = E;
Edge E1 = {v, u, head[v]};//无向图需要反向建边
edge[top] = E1;
}
int tarjan(int u, int fa)//u在DFS树中的父节点是fa
{
low[u] = dfn[u] = ++dfs_clock;
int child = 0;
for(int i = head[u]; i != -1; i = edge[i].next)
{
Edge E = edge[i];
int v = E.to;
if(!dfn[v])
{
S.push(E);
child++;
low[v] = tarjan(v, u);
low[u] = min(low[u], low[v]);
if(low[v] >= dfn[u])
{
iscut[u] = true;
bcc_cnt++;
bcc[bcc_cnt].clear();
for(;;)
{
Edge x = S.top(); S.pop();
if(belong[x.from] != bcc_cnt)
{
bcc[bcc_cnt].push_back(x.from);
belong[x.from] = bcc_cnt;
}
if(belong[x.to] != bcc_cnt)
{
bcc[bcc_cnt].push_back(x.to);
belong[x.to] = bcc_cnt;
}
if(x.from == u && x.to == v) break;
}
}
}
else if(dfn[v] < dfn[u] && v != fa)
{
S.push(E);
low[u] = min(low[u], dfn[v]);
}
}
if(fa < 0 && child == 1) iscut[u] = 0;
return low[u];
}
void find_cut(int l, int r)//左右区间 根据数据可以进行变化
{
memset(iscut, 0, sizeof(iscut));
memset(low, 0, sizeof(low));
memset(belong, 0, sizeof(belong));
memset(dfn, 0, sizeof(dfn));
dfs_clock = bcc_cnt = 0;
for(int i = l; i <= r; i++)//从左到右
if(!dfn[i]) tarjan(i, -1);
}
int main()
{
int N;
int a, b;
int i, l;
char str[5010];
while(scanf("%d", &N), N)
{
init();
while(gets(str), strcmp(str, "0"))
{
l = strlen(str);
a = 0; i = 0;
while(str[i] != ' ')
{
a = a * 10 + str[i] - '0';
++i;
}
for( ; i < l; i++)
{
if(str[i] != ' ')//空格ascll码为32
{
b = 0;
while(str[i] != ' ' && i < l)
{
b = b * 10 + str[i] - '0';
++i;
}
}
}
}
find_cut(1, N);
int ans = 0;
for(i = 1; i <= N; i++)
{
if(iscut[i])
ans++;
}
printf("%d\n", ans);
}
return 0;
} 

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：512696次
• 积分：19520
• 等级：
• 排名：第452名
• 原创：1484篇
• 转载：12篇
• 译文：0篇
• 评论：162条
文章分类