Network(连通图割点)

来源:互联网 发布:淘宝小儿介入 编辑:程序博客网 时间:2024/05/18 03:59

 Network
Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu
Submit Status Practice UVA 315

Description

Download as PDF

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

连通图割点裸题。。。

相关资料:

http://blog.csdn.net/u014665013/article/details/50009399

http://blog.csdn.net/u014665013/article/details/51351810

#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#define MOD 100000#define inf 1<<29#define LL long longusing namespace std;/**  求  无向图   的割点和桥*  可以找出割点和桥,求删掉每个点后增加的连通块。*  需要注意重边的处理,可以先用矩阵存,再转邻接表,或者进行判重*/const int MAXN = 10010;const int MAXM = 2000010;struct Edge{    int to,next;    bool cut;//是否为桥的标记}edge[MAXM];int head[MAXN],tot;int Low[MAXN],DFN[MAXN],Stack[MAXN];int Index,top;bool Instack[MAXN];bool cut[MAXN];   ///记录是否是割点int add_block[MAXN];//删除一个点(i)后增加的连通块int bridge;void addedge(int u,int v){    edge[tot].to = v;edge[tot].next = head[u];edge[tot].cut = false;    head[u] = tot++;}void init(){    memset(DFN,0,sizeof(DFN));    memset(Instack,false,sizeof(Instack));    memset(add_block,0,sizeof(add_block));    memset(cut,false,sizeof(cut));    memset(head,-1,sizeof(head));    Index = top = tot = 0;    bridge = 0;}void Tarjan(int u,int pre)  ///pre是父节点,用来判断重边{   // cout<<u<<endl;    int v;    Low[u] = DFN[u] = ++Index;    Stack[top++] = u;    Instack[u] = true;    int son = 0;    int pre_cnt = 0;  ///处理重边 ,如果不需要可以去掉    for(int i = head[u];i != -1;i = edge[i].next)    {     //   cout<<v<<"__"<<endl;        v = edge[i].to;        if(v == pre && pre_cnt == 0)        {            pre_cnt++;            continue;        }        if( !DFN[v] )        {            son++;            Tarjan(v,u);            if(Low[u] > Low[v])                Low[u] = Low[v];            ///桥            ///一条无向边(u,v)是桥,当且仅当(u,v)为树枝边,且满足DFS(u)<Low(v)。            if(Low[v] > DFN[u])            {                bridge++;                edge[i].cut = true;                edge[i^1].cut = true;            }            //割点            //一个顶点u是割点,当且仅当满足(1)或(2)            //(1) u为树根,且u有多于一个子树。            //(2) u不为树根,且满足存在(u,v)为树枝边(或称父子边,即u为v在搜索树中的父亲),使得DFS(u)<=Low(v)            if(u != pre && Low[v] >= DFN[u])//不是树根            {                cut[u] = true;                add_block[u]++;            }        }        else if( Low[u] > DFN[v])             Low[u] = DFN[v];    }    //树根,分支数大于1    if(u == pre && son > 1)        cut[u] = true;    if(u == pre)       add_block[u] = son - 1;    Instack[u] = false;    top--;}void solve(int n){    Tarjan (1, 1);   // cout<<"++++++++++\n";    int ans = 0;    for (int i = 1; i <= n; ++i)    {        if (cut[i])        {            ans++;        }    }    printf("%d\n", ans);}int main(){    int n;    int u, v;    while (~scanf("%d", &n), n)    {        init();        while (scanf("%d", &u), u)        {            while(getchar() != '\n')            {                scanf("%d", &v);                addedge (u, v);                addedge (v, u);            }        }       // cout<<"=========\n";        solve(n);    }    return 0;}




0 0
原创粉丝点击