EricYang

Tech Spot of Eric

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
Network
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 6365   Accepted: 2988

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

求割点算法:

该算法是R.Tarjan发明的。对图深度优先搜索,定义DFS(u)为u在搜索树(以下简称为树)中被遍历到的次序号。定义Low(u)为u或u的子树中能通过非父子边追溯到的最早的节点,即DFS序号最小的节点。根据定义,则有:

Low(u)=Min
{
DFS(u)
DFS(v) (u,v)为后向边(返祖边) 等价于 DFS(v)<DFS(u)且v不为u的父亲节点
Low(v) (u,v)为树枝边(父子边)
}

一个顶点u是割点,当且仅当满足(1)或(2)
(1) u为树根,且u至少有两个子树。
(2) u不为树根,且满足存在(u,v)为树枝边(或称父子边,即u为v在搜索树中的父亲),使得DFS(u)<=Low(v)。

 

Source Code

Problem: 1144		User: BUPT_ERIC
Memory: 396K		Time: 32MS
Language: G++		Result: Accepted
Source Code
//求割点low(u)=min{dfs(u),dfs(v),low(v)}
//1)root,有两个及以上儿子
//2)non-root: low(v)>=dfs(u)
//输入方法特别虎
#include <cstdio>
#include <cstring>
#include <algorithm>

const int N=101;
int n;
bool map[N][N];
int dep[N], low[N], f[N];
int root;
bool isVisited[N];

using namespace std;

void dfs(int u, int depth)
{
    dep[u]=depth;
    low[u]=depth;
    isVisited[u]=true;
    for(int i=1; i<=n; i++)
    {
        if(map[u][i])  //u的后向节点
        {
            if(!isVisited[i])
            {
                dfs(i,depth+1);
                low[u]=min(low[u],low[i]);
                if(u!=1 && low[i]>=dep[u])   //non-root
                {
                    f[u]++;
                }
                else if(u==1)
                {
                    root++;
                }
            }
            else
            {
                low[u]=min(low[u],dep[i]);
            }
        }
    }
}

int main()
{
    int u,v;
    int ans;

    while(scanf("%d",&n)!=EOF && n)
    {
        memset(map,0,sizeof(map));
        memset(dep,0,(n+1)*sizeof(int));
        memset(low,0,(n+1)*sizeof(int));
        memset(f,0,(n+1)*sizeof(int));
        memset(isVisited,0,(n+1)*sizeof(int));
        root=0;
        ans=0;
        while(scanf("%d",&u) && u)
        {
            while(getchar()!='\n')
            {
                scanf("%d",&v);
                map[u][v]=true;
                map[v][u]=true;
            }
        }
        dfs(1,1);
        if(root>=2)
        {
            ans++;
        }
        for(int i=2; i<=n; i++)
        {
            if(f[i]>=1)
            {
                ans++;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

  

posted on 2012-05-02 22:46  Eric-Yang  阅读(359)  评论(0编辑  收藏  举报