• 无向图连通度


     1 //vis[i]   0--尚未访问   1--正在访问   2--已经访问结束
     2 //anc[i]   该点能到达的最小序号
     3 //pre[i]   该点的序号
     4 /*==================================================*\
     5  |  无向图连通度( 割)
     6  | INIT: edge[][]邻接矩阵;vis[],pre[],anc[],deg[]置为0;
     7  | CALL: dfs(0, -1, 1, n);
     8  | k=deg[0], deg[i]+1(i=1…n-1) 为删除该节点后得到的连通图个数
     9  |  注意:0作为根比较特殊!
    10 \*==================================================*/
    11 
    12 int edge[V][V], anc[V], pre[V], vis[V], deg[V];
    13 void dfs(int cur, int father, int dep, int n) { // vertex: 0 ~ n-1
    14     int cnt = 0;
    15     vis[cur] = 1;
    16     pre[cur] = anc[cur] = dep;
    17     for (int i = 0; i < n; ++i)
    18         if (edge[cur][i]) {
    19             if (i != father && 1 == vis[i]) {
    20                 if (pre[i] < anc[cur])
    21                     anc[cur] = pre[i]; //存在回边
    22             }
    23             if (0 == vis[i]) { //树边
    24                 dfs(i, cur, dep + 1, n);
    25                 ++cnt; // 分支个数
    26                 if (anc[i] < anc[cur])
    27                     anc[cur] = anc[i];//更新anc[cur]
    28                 if ((cur == 0 && cnt > 1)//父节点并且分支个数大于1
    29                         || (cnt != 0 && anc[i] >= pre[cur]))//导致了结果不同
    30                     ++deg[cur];
    31             }
    32         }
    33     vis[cur] = 2;
    34 }
  • 相关阅读:
    【SQL】182. Duplicate Emails
    【SQL】181. Employees Earning More Than Their Managers
    【SQL】180. Consecutive Numbers
    【SQL】178. Rank Scores
    【SQL】177. Nth Highest Salary
    如何处理postman Self-signed SSL certificate blocked错误
    Radio checked 属性
    转成百分比
    内建函数
    队列
  • 原文地址:https://www.cnblogs.com/kakamilan/p/2585567.html
Copyright © 2020-2023  润新知