Network(图——割点 + 模板)

关于割点:
1.在无向连通图中,删除一个顶点v及其相连的边后,原图从一个连通分量变成了两个或多个连通分量,

则称顶点v为割点,同时也称关节点(Articulation Point)。

2.一个没有关节点的连通图称为重连通图(biconnected graph)。

3.若在连通图上至少删去k 个顶点才能破坏图的连通性,则称此图的连通度为k。

图的保存:
邻接矩阵或邻接表

求割点:
1.基于DFS算法

2.对根节点u,若其有两棵或两棵以上的子树,则该根结点u为割点

3.对非叶子节点u(非根节点),若其子树v的节点均没有指向u的祖先节点的回边,说明删除u之后,根结点与u的子树的节点不再连通;

则节点u为割点。

4.用 dfn[u] 记录节点u在DFS遍历过程中被遍历到的次序号

5.用 low[u] 记录节点u或v通过非父子边追溯到最早的祖先节点(即DFS次序号最小)



一开始一直没理解的一点,也是最重要的一点:

怎么判断 (u,v)是父子关系 还是回溯关系

那就是通过判断 v 这个节点是不是已经被访问过

1.如果 v 先前被访问过 那么就是 u 通过回溯得到的(也就是前面说的不是通过它的父亲节点得到的)

所以这里还要加一个判断条件 也就是 p[u] != v;

2.如果 v 没有被访问过 那么 v 就是 u 的子结点(整棵树看作从上到下,这个方向序号从小到大)


求割点模板(这里用邻接矩阵)

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define MAX 1000
int vis[MAX], mp[MAX][MAX], p[MAX], f[MAX];
int low[MAX], dfn[MAX];
int n;
void dfs(int u)
{
    static int cont = 0;
    int child = 0;
    vis[u] = 1;
    low[u] = dfn[u] = ++cont;
    for(int v = 1; v <= n; v ++ ){
        if(mp[u][v]){
            // if the (u,v) is tree arc
            if(!vis[v]){
                child ++;
                p[v] = u;
                dfs(v);
                low[u] = min(low[u], low[v]);
                //case 1
                if(p[u] == -1 && child > 1 && !f[u]){
                     printf(" 1 :%d is an articulation point\n", u);
                     f[u] = 1;
                }
                //case 2
                if(p[u] != -1 && low[v] >= dfn[u] && !f[u]){
                    printf("2 :%d is an articulation point\n", u);
                    f[u] = 1;
                }
            }
            // if the (u,v) is hui su arc
            else
                if(v != p[u]){
                    low[u] = min(low[u], dfn[v]);
                }
        }
    }
}

int main()
{
    int m;
    memset(mp, 0, sizeof(mp));
    memset(vis, 0, sizeof(vis));
    memset(p, -1, sizeof(p));
    memset(f, 0, sizeof(f));
    printf("input the nodes: \n");
    scanf("%d", &n);
    printf("input the arcs: \n");
    scanf("%d", &m);
    printf("input the %d arcs' start node and end node :\n ", m);
    for(int i = 1; i <= m; i++ ){
        int a, b;
        scanf("%d %d", &a, &b);
        mp[a][b] = mp[b][a] = 1;
    }
    dfs(1);
}

看一道题目吧。。。

Network

Time Limit: 1000ms
Memory Limit: 10000KB
64-bit integer IO format: %lld      Java class name: Main
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.

这里就是用了邻接矩阵,可以套用上面的模板

#include <iostream>
#include <stdio.h>
#include <string>
#include <string.h>
#include <sstream>
using namespace std;
int mp[100][100], vis[100], f[100], p[100];
int low[100], dfn[100];
int n, sum;

void dfs(int u)          //注意递归:每一层递归里的u都是单独的,所以后面的孩子数也是一一对应每一个u的
{
    static int cont = 0; //这里cont用了静态变量,为了不让它在递归中被自动释放
    int child = 0;       //设置u节点的孩子个数
    cont ++;             //序号数增加
    vis[u] = 1;
    dfn[u] = low[u] = cont; //初始化,先把 u 的最高祖先点归为当前的序号 序号当然是序号啦
    for(int v = 1; v <= n; v ++ ){
        if(mp[u][v]){
            if(!vis[v]){ //(u,v)是父子关系
                child ++;
                p[v] = u;
                dfs(v);  //拦腰截断
                //回溯
                low[u] = min(low[u], low[v]);
                if(p[u] == -1 && child > 1 && !f[u]){//case 1:如果u的孩子个数为 大于等于 2个 并且u没有父亲(代表他是根节点)
                    sum ++;
                    f[u] = 1;
                }
                if(p[u] != -1 && low[v] >= dfn[u] && !f[u]){//case 2:如果 u 有父亲 并且 它的孩子的最高回溯点如果高于或者等于u的点
                    sum ++;
                    f[u] = 1;
                }
            }
            else if(p[u] != v){//(u,v)是回溯关系,这里的判断还可以放在外面,好好理解这种回溯的技巧
                low[u] = min(low[u], dfn[v]);
            }
        }
    }
}

int readlist(int *a)
{
    int i = 0;
    string line;
    getline(cin, line);
    stringstream s(line);
    int t;
    while(s >> t){
        a[i++] = t;
    }
    return i;
}

int main()
{
    while(scanf("%d", &n) && n != 0){
        sum = 0;
        memset(f, 0, sizeof(f));
        memset(p, -1, sizeof(p));
        memset(mp, 0, sizeof(mp));
        memset(vis, 0, sizeof(vis));
        int c;
        scanf("%d", &c);
        while(c != 0){
            int a[n], num;
            num = readlist(a);
            for(int i = 0; i < num; i++ ){
                mp[c][a[i]] = mp[a[i]][c] = 1;
            }
            scanf("%d", &c);
        }
        dfs(1);
        printf("%d\n", sum);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值