求环总结

[ 转载自yxt大佬:%%% ]

 

可以用并查集

#include <iostream>
#include <cstdio>
using namespace std;
const int N = 200010;
int n, fa[N], ans = 0x3f3f3f3f;
int get (int x, int &cnt) { //cnt记录环的长度 
    cnt ++;
    if (fa[x] == x) return x;
    else return get(fa[x], cnt);
}
int main () {
    scanf("%d", &n);
    for (int i = 1; i <= n; i ++)
        fa[i] = i;
    for (int i = 1; i <= n; i ++) {
        int cnt = 0, f;
        scanf("%d", &f);
        if (get(f, cnt) == i) {
            ans = min(ans, cnt); //维护最小的环 
        }else
            fa[i] = f;
    }
    printf("%d", ans);
    return 0;
}

也可以用拓扑序

康板子总结那里

也可以用dfs+bfs


void dfs(int u,int k){
    dfsn[u]=k;
    vis[u]=1;
    q.push(u);
    int x=a[u];
    if(vis1[x]) return ;
    else if(vis[x]) ans=min(ans,dfsn[u]-dfsn[x]+1);
    else dfs(x,k+1);
}

int main()
{
    scanf("%d", &n);

    for(int i = 1; i <= n; i++)
    scanf("%d", &a[i]);

    for(int i = 1; i <= n; i++)
        if(!vis1[i]){
            dfs(i,1);
            while(!q.empty()) {
                int x = q.front();
                q.pop();
            vis1[x] = 1;
        }       
    }

    printf("%d\n", ans);
    return 0;
}

另,对于无向图求环:

  1. 判断N结点的无向图G是否有环

    假定:结点个数为M,边条数为E 遍历一遍,判断图分为几部分(假定为P部分,即图有 P 个连通分量) 对于每一个连通分量,如果无环则只能是树,即:边数=结点数-1 只要有一个满足 边数 > 结点数-1 原图就有环 将P个连通分量的不等式相加,就得到: 所有边数 > 所有结点数 + 连通分量个数 即: E + P > M 所以只有判断 E + P > M 就表示原图有环,否则无环.

  2. 对于每一个连通分量,单独计算其环的个数,则无向图G的总环数即为各连通分量环数总和

    前提:对一连通分量P,将其用邻接矩阵表示法来表示

    1. 用广度优先算法求出P的支撑树(即生成树),在求支撑树的过程中,用 -1表示被加入支撑树中的边。(对于无权图可以用1表示);
    2. 在邻接矩阵中寻找权值不是-1的边(当然也不是0,如果是无权图,就应该找值为1的边),假定该边连接的是节点i和j。将其边的权值改为-1;
    3. 采用深度优先遍历算法求出从顶点i到顶点j之间所有简单路径(注意给每个顶点赋不同权值。例如-1,0,1分别表示未遍历,已经遍历但还有相邻结点未遍历完,已经遍历而且相邻结点已遍历完。这样做主要是为了防止回溯到上一已访问过的结点。);
    4. 根据生成树的定义,在生成树上每增加一条边,就会有一个回路。在生成树上寻找i和j的路径。将该简单路径与边(i,j)连接即得环。输出该环;
    5. 继续在邻接矩阵中寻找权值不是-1的边,假定该边连接的两顶点是v和w。将其边的权值改为-1;
    6. 求出从顶点i到顶点j之间的所有简单路径;
    7. 分别将所求出的简单路径与边(i,j)连接即得环,输出该环;
    8. 重复执行步骤4-7,直到在邻接矩阵中没有权值是-1的边为止。
posted @ 2019-11-11 18:48  QUEKI嶺冬  阅读(185)  评论(0编辑  收藏  举报
/*! Color themes for Google Code Prettify | MIT License | github.com/jmblog/color-themes-for-google-code-prettify */ .pln{color:#4d4d4c}ol.linenums{margin-top:0;margin-bottom:0;color:#8e908c}li.L0,li.L1,li.L2,li.L3,li.L4,li.L5,li.L6,li.L7,li.L8,li.L9{padding-left:1em;background-color:#fff;list-style-type:decimal!important;}@media screen{.str{color:#718c00}.kwd{color:#8959a8}.com{color:#8e908c}.typ{color:#4271ae}.lit{color:#f5871f}.pun{color:#4d4d4c}.opn{color:#4d4d4c}.clo{color:#4d4d4c}.tag{color:#c82829}.atn{color:#f5871f}.atv{color:#3e999f}.dec{color:#f5871f}.var{color:#c82829}.fun{color:#4271ae}} /*下面是我设置背景色,字体大小和字体*/ .cnblogs-markdown code{ background:#fff!important; } .cnblogs_code,.cnblogs_code span,.cnblogs-markdown .hljs{ font-size:16px!important; } .syntaxhighlighter a, .syntaxhighlighter div, .syntaxhighlighter code, .syntaxhighlighter table, .syntaxhighlighter table td, .syntaxhighlighter table tr, .syntaxhighlighter table tbody, .syntaxhighlighter table thead, .syntaxhighlighter table caption, .syntaxhighlighter textarea { font-size: 16px!important; } .cnblogs_code, .cnblogs_code span, .cnblogs-markdown .hljs{ font-family:consolas, "Source Code Pro", monaco, monospace !important; } //以上是代码高亮 /* 文字特效 */