zoukankan      html  css  js  c++  java
  • POJ3177 Redundant Paths

    双连通图:无向图中每两个顶点都存在完全不同的两条路径

    给定一个无向图,问要给它增加多少条边可以把它变成双连通图。

    用tarjan缩点,可以得到一棵树,添加(叶子结点+1)/2条边可以使其成环,也就是答案~

    为了避开重边,这题用邻接矩阵存,wa了一晚上QAQ~

    #include<cstdio>
    #include<algorithm>
    #include<vector>
    #include<stack>
    using namespace std;
    const int maxn=5014;
    int g[maxn][maxn];
    int N,M,x,y;
    int low[maxn];
    int dfn[maxn];
    stack<int> st;
    int cnt;
    int scc;
    int pos[maxn];
    int in[maxn];
    void tarjan (int x,int pre) {
        low[x]=dfn[x]=++cnt;
        st.push(x);
        for (int i=1;i<=N;i++) {
            if (i==pre) continue;
            if (!g[x][i]) continue;
            if (!low[i]) {
                tarjan(i,x);
                low[x]=min(low[x],low[i]);
            }
            else if (!pos[i]) low[x]=min(low[x],dfn[i]);
        }
        if (low[x]==dfn[x]) {
            scc++;
            while (1) {
                int u=st.top();
                st.pop();
                low[u]=low[x];
                pos[u]=scc;
                if (u==x) break;
            }
        }
    }
    void build () {
        for (int i=1;i<=N;i++) {
            for (int j=1;j<=N;j++) {
                if (pos[i]!=pos[j]&&g[i][j]) in[pos[j]]++;
            }
        }
    }
    int main () {
        scanf ("%d %d",&N,&M);
        for (int i=0;i<M;i++) {
            scanf ("%d %d",&x,&y);
            g[x][y]=g[y][x]=1;
        }
        for (int i=1;i<=N;i++) 
        if (!low[i]) tarjan(i,i);
        build ();
        int leaves=0;
        for (int i=1;i<=scc;i++) {
            if (in[i]==1) leaves++;
        }
        printf ("%d
    ",(leaves+1)/2);
        return 0;
    }
  • 相关阅读:
    2017-5-25 母版页
    2017-5-25 分页加条件查询合体
    2017-5-23 WebForm 中的分页功能和条件查询功能
    2017-5-17 WebForm 基础
    2017-5-14 心情
    2017-5-10 小型人员管理系统
    2017-5-9 打开唯一窗体的实例操作
    2017-5-8 TreeView 实现三级联动 (递归方法)
    2017-5-7 三级联动数据库 数据保存
    2017-5-7 三级联动
  • 原文地址:https://www.cnblogs.com/zhanglichen/p/12313421.html
Copyright © 2011-2022 走看看