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

来源:互联网 发布:网络棋牌 编辑:程序博客网 时间:2024/05/17 04:57

Network
Time Limit: 1000MS Memory Limit: 10000KTotal 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

55 1 2 3 4062 1 35 4 6 200

Sample Output

12

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.

题意:给出节点数N,然后下面最多N行输入边的信息,每行包括一个地方的号码,以及有直接线路通往其他地方的号码。每个网络信息的最后一行为数字0,表示该网络数据的结束。输入文件最后一行为0,表示输入文件结束。       让 你  求出无向网络中割点总数。

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

自己写的代码:


#include <cstdio>#include <cstring>#include <queue>#include <stack>#include <algorithm>#define MAXN 1000+10#define MAXM 2000000+10using namespace std;struct Edge{int from, to, next;}edge[MAXM];int head[MAXN], top;bool iscut[MAXN];//判断是否为割点int low[MAXN];//从当前节点或它的子孙出发通过回边可以到达的最低深度优先数 int dfn[MAXN];//记录该点在DFS树中的深度优先数int recdfn;//记录当前的深度优先序数 void init(){top = 0;memset(head, -1, sizeof(head));} void addEdge(int u, int v){Edge E = {u, v, head[u]};edge[top] = E;head[u] = top++;Edge E1 = {v, u, head[v]};edge[top] = E1;head[v] = top++;}void getMap(int m){int a, b;while(m--){scanf("%d%d", &a, &b);addEdge(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)//割点是根节点 且子节点数目大于1iscut[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;}addEdge(a, b);}}}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+10using namespace std;struct Edge{int from, to, next;}edge[MAXM];int head[MAXN], top;//存储指针 int add_bcc[MAXN];//去掉该点增加的bcc数目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;memset(head, -1, sizeof(head));} void addEdge(int u, int v){Edge E = {u, v, head[u]};edge[top] = E;head[u] = top++;Edge E1 = {v, u, head[v]};//无向图需要反向建边edge[top] = E1;head[v] = top++; }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;add_bcc[u]++;//增加一个bcc 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(add_bcc, 0, sizeof(add_bcc));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;}addEdge(a, b);}}}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
原创粉丝点击