POJ 1144 Network(简单求无向图割顶数)

来源:互联网 发布:windows中文件切换 编辑:程序博客网 时间:2024/05/16 12:22

思路:简单的求无向图割点,上模板即可


#include <cstdio>#include <queue>#include <cstring>#include <iostream>#include <cstdlib>#include <algorithm>#include <vector>#include <map>#include <string>#include <set>#include <ctime>#include <cmath>#include <cctype>using namespace std;#define maxn 1000#define LL long longint cas=1,T;int n,m;int dfs_clock;            //时钟,每访问一个结点增1vector<int>G[maxn];       //图int pre[maxn];            //pre[i]表示i结点被第一次访问到的时间戳,若pre[i]==0表示还未被访问int low[maxn];            //low[i]表示i结点及其后代能通过反向边连回的最早的祖先的pre值bool iscut[maxn];         //标记i结点是不是一个割点//求出以u为根节点(u在DFS树中的父节点是fa)的树的所有割点和桥//初始调用dfs(root,-1)int dfs(int u,int fa){int lowu=pre[u]=++dfs_clock;int child = 0;                //子结点数目for (int i = 0;i<G[u].size();i++){int v = G[u][i];if (!pre[v]){child++;              //未访问过的结点才能算是u的孩子int lowv = dfs(v,u);lowu = min(lowu,lowv);if (lowv >=pre[u]){iscut[u]=1;           //u是割点//if (lowv > pre[u])       //(u,v)边时桥//printf("qiao")}}else if (pre[v] <pre[u] && v!=fa)  //v!=fa确保了(u,v)是从u到v的反向边{lowu = min(lowu,pre[v]);}}if (fa < 0 && child == 1)iscut[u]=0;             //若u是根且孩子数<=1,那么u就不是割点return low[u]=lowu;}void init(){dfs_clock = 0;memset(pre,0,sizeof(pre));memset(iscut,0,sizeof(iscut));for (int i = 1;i<=n;i++)G[i].clear();}int get(char *s){  int v = 0;  for (int i = 0;s[i];i++)  v=v*10+s[i]-'0';  return v;}int main(){while (scanf("%d",&n)!=EOF && n){init();    char str[10];while (scanf("%s",str)){if (str[0]=='0')break;int u = get(str);while (scanf("%s",str)){int v = get(str);G[u].push_back(v);G[v].push_back(u);if (getchar()=='\n')break;}}dfs(1,-1);        int ans = 0;for (int i = 1;i<=n;i++)if (iscut[i])ans++;printf("%d\n",ans);}//freopen("in","r",stdin);//scanf("%d",&T);//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);return 0;}


题目

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.



0 0
原创粉丝点击