ZOJ 1311 Network 求割点

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 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 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

 

无向连通图的割点性质

1.       考虑根节点root。如果顶点xy同是root的儿子,那么由此证明x无法通过非root的顶点与y相连,所以当根root有数量>1的儿子时,根是图的割点。

2.       考虑非根节点i,再考虑i的某个儿子节点j。易知:

          和j相连的白色节点都将成为j的子孙。

          和j相连的灰色节点都是j的祖先,由j指向i祖先的边称为后向边

          黑色节点不可能与j相连。

          如果jj的子孙都不存在指向j的祖先的后向边,那么删除顶点i后,顶点ji的祖先或者兄弟无法连通。因此,当且仅当i的某个儿子及儿子的子孙均没有指向i祖先的后向边时,i是图的割点。

 

割点的算法

dfs的基础上增加ancestor数组,ancestor[k]记录与kk的子孙相连的辈分最高的祖先所在的深度,当ancestor[j]>=deep[j](ji的儿子)jj的子孙不存在指向i祖先的后向边,则i是割点。Son表示顶点k的儿子的数量。根节点和非根节点要区别对待。

#include <iostream>
#include 
<vector>
using namespace std;

const int MAXN = 110;
vector
< vector<int> > adj;
int cut[MAXN],mark[MAXN],deep[MAXN],ancestor[MAXN];

char *read(char str[],char *p){
    
while(*&& *p!=' ') p++;
    
while(*&& *p==' ') p++;
    
return p;
}

void dfs(int u,int father,int depth){
    
int i,v,son=0;
    mark[u]
=1;
    deep[u]
=ancestor[u]=depth;
    
for(i=0;i<adj[u].size();i++){
        v
=adj[u][i];
        
if(v!=father && mark[v]==1)
            ancestor[u]
=min(ancestor[u],deep[v]);
        
if(mark[v]==0){
            dfs(v,u,depth
+1);
            son
=son+1;
            ancestor[u]
=min(ancestor[u],ancestor[v]);
            
if((father==-1 && son>1|| (father!=-1 && ancestor[v]>=deep[u]))
                cut[u]
=1;
        }

    }

    mark[u]
=2;
}

int main(){
    
int i,x,y,n,cnt;
    
char str[MAXN*10],*p;
    
while(scanf("%d",&n),n){
        adj.assign(n,vector
<int>());
        
while(scanf("%d",&x),x){
            gets(str);
            
for(p=read(str,str);sscanf(p,"%d",&y)!=EOF;p=read(str,p))
                adj[x
-1].push_back(y-1),adj[y-1].push_back(x-1);
        }

        memset(cut,
0,sizeof(cut));
        memset(mark,
0,sizeof(mark));
        
for(i=0;i<n;i++)
            
if(!mark[i]) dfs(i,-1,0);
        
for(cnt=i=0;i<n;i++)
            
if(cut[i]) cnt++;
        printf(
"%d\n",cnt);
    }

    
return 0;
}

posted on 2009-05-27 20:35 极限定律 阅读(1058) 评论(2)  编辑 收藏 引用 所属分类: ACM/ICPC

评论

# re: ZOJ 1311 Network 求割点 2009-08-13 23:05 zeus

for(i=0;i<n;i++)
if(!mark[i]) dfs(0,-1,0);
这一句应该是dfs(i,-1,0)吧?不过居然都ac  回复  更多评论   

# re: ZOJ 1311 Network 求割点 2009-08-14 20:55 极限定律

多谢,写错了。居然能AC确实有点神奇@zeus  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理


<2009年5月>
262728293012
3456789
10111213141516
17181920212223
24252627282930
31123456

导航

统计

常用链接

留言簿(10)

随笔分类

随笔档案

友情链接

搜索

最新评论

阅读排行榜

评论排行榜