uva315 Network 【tarjan-求割点】

来源:互联网 发布:电脑软件编程郑州 编辑:程序博客网 时间:2024/05/22 07:57
                                **uva Network**

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

题目大意:求所给无向图中,有多少个割点?

无向图
割点的定义:无向连通图的割点(关键点)是指删除该结点及与其相连的边之后,无向图不再连通。上图中割顶有:1,2,5,9.

割点的性质:

    1、考虑根顶点Root。    如果顶点x和y同是Root的儿子,那么x无法通过非Root的顶点与y相连,所以当根Root有数量>1的儿子时,根是图的割顶;    2、考虑非根顶点i,设i的某个儿子是j    ①和j相连的白色结点都将成为j的子孙;    ②和j相连的灰色结点都是j的祖先,由j指向i祖先的边称为后向边;    ③黑色结点不可能与j相连    如果jj的子孙都不存在指向i祖先的后向边,那么删除顶点i后,顶点ji的祖先或者兄弟将无法连通。因此,当且仅当i的某个儿子及儿子的子孙均没有指向i祖先的后向边时,i是图的割顶。

AC代码:

# include <stdio.h># include <string.h># define MAXN 10005struct Edge{    int to;    int next;}edge[MAXN];int DFN[MAXN];int LOW[MAXN];int head[MAXN], tot;int Index;int top;int cut[MAXN]; //割点void Init(){    tot = 0;    memset(head, -1, sizeof(head));    Index = 0;    memset(DFN, 0, sizeof(DFN));    memset(LOW, 0, sizeof(LOW));    memset(cut, 0, sizeof(cut));}void addedge(int u, int v){    edge[tot].to = v;    edge[tot].next = head[u];    head[u] = tot++;}void tarjan(int u, int pre){    int v;    LOW[u] = DFN[u] = ++ Index;    int son = 0;    for (int i = head[u]; i != -1; i = edge[i].next)    {        v = edge[i].to;        if (v == pre)        {            continue;        }        if (!DFN[v])        {            son ++;            tarjan(v, u);            if (LOW[u] > LOW[v])            {                LOW[u] = LOW[v];            }            if (u != pre && LOW[v] >= DFN[u]) //当前结点不是根节点时            {                cut[u] = true;            }        }        else if (LOW[u] > DFN[v])        {            LOW[u] = DFN[v];        }    }    if (u == pre && son > 1)//当前结点是根节点时    {        cut[u] = true;    }}void solve(int n){    int i;    for (i = 1; i <= n; i++)    {        if (!DFN[i])        {            tarjan(i, i);        }    }    int ans = 0;    for (i = 1; i <= n; i++)    {        if (cut[i])        {            ans ++;        }    }    printf("%d\n", ans);}int main(void){    int n;    while (scanf("%d", &n) && n)    {        int a, b;        char ch;        Init();        while (scanf("%d", &a) && a)        {            while (scanf("%d%c", &b, &ch))            {                addedge(a, b);                addedge(b, a);                if ('\n' == ch)                {                    break;                }            }        }        solve(n);    }    return 0;}
0 0
原创粉丝点击