zoukankan      html  css  js  c++  java
  • POJ 3177 Redundant Paths(无向图缩点)

    Description:

    In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numbered 1..F) to another field, Bessie and the rest of the herd are forced to cross near the Tree of Rotten Apples. The cows are now tired of often being forced to take a particular path and want to build some new paths so that they will always have a choice of at least two separate routes between any pair of fields. They currently have at least one route between each pair of fields and want to have at least two. Of course, they can only travel on Official Paths when they move from one field to another. 

    Given a description of the current set of R (F-1 <= R <= 10,000) paths that each connect exactly two different fields, determine the minimum number of new paths (each of which connects exactly two fields) that must be built so that there are at least two separate routes between any pair of fields. Routes are considered separate if they use none of the same paths, even if they visit the same intermediate field along the way. 

    There might already be more than one paths between the same pair of fields, and you may also build a new path that connects the same fields as some other path.

    Input:

    Line 1: Two space-separated integers: F and R 

    Lines 2..R+1: Each line contains two space-separated integers which are the fields at the endpoints of some path.

    Output:

    Line 1: A single integer that is the number of new paths that must be built.

    Sample Input:

    7 7
    1 2
    2 3
    3 4
    2 5
    4 5
    5 6
    5 7

    Sample Output:

    2

    Hint:

    Explanation of the sample: 

    One visualization of the paths is: 
       1   2   3
    +---+---+
    | |
    | |
    6 +---+---+ 4
    / 5
    /
    /
    7 +
    Building new paths from 1 to 6 and from 4 to 7 satisfies the conditions. 
       1   2   3
    +---+---+
    : | |
    : | |
    6 +---+---+ 4
    / 5 :
    / :
    / :
    7 + - - - -
    Check some of the routes: 
    1 – 2: 1 –> 2 and 1 –> 6 –> 5 –> 2 
    1 – 4: 1 –> 2 –> 3 –> 4 and 1 –> 6 –> 5 –> 4 
    3 – 7: 3 –> 4 –> 7 and 3 –> 2 –> 5 –> 7 
    Every pair of fields is, in fact, connected by two routes. 

    It's possible that adding some other path will also solve the problem (like one from 6 to 7). Adding two paths, however, is the minimum.
     
    题意:有n个点,现在每两个点之间可能已经一条或多条路,如果让每两个点之间最少有两条路,那么最少还需要建几条路。
    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    using namespace std;
    
    const int N=50010;
    
    struct node
    {
        int v, next;
    } no[N];
    int r[N], be[N], Stack[N], vis[N], head[N], dfn[N], low[N];
    bool use[5010][5010];
    int ans, Time, top, k;
    
    void Init()
    {
        memset(head, -1, sizeof(head));
        memset(dfn, 0, sizeof(dfn));
        memset(low, 0, sizeof(low));
        memset(vis, 0, sizeof(vis));
        memset(r, 0, sizeof(r)); ///r数组存放与第i个点相邻的点与其强连通分量不同的个数
    
        Time = top = ans = k = 0;
    }
    
    void Add(int a, int b)
    {
        no[k].v = b;
        no[k].next = head[a];
    
        head[a] = k++;
    }
    
    void Tarjan(int u, int fa)
    {
        int v, i;
    
        Stack[top++] = u;
        vis[u] = 1;
        dfn[u] = low[u] = ++Time;
    
        for (i = head[u]; i != -1; i = no[i].next)
        {
            v = no[i].v;
    
            if (!dfn[v])
            {
                Tarjan(v, u);
                low[u] = min(low[u], low[v]);
            }
            else if (v != fa) low[u] = min(low[u], dfn[v]);
        }
    
        if (dfn[u] == low[u])
        {
            ++ans;
            do
            {
                v = Stack[--top];
                vis[v] = 0;
                be[v] = ans;
            }while(u != v);
        }
    }
    
    int main ()
    {
        int n, m, a, b, i, j, num;
    
        while (scanf("%d%d", &n, &m) != EOF)
        {
            Init();
            num = 0;
    
            while (m--)
            {
                scanf("%d%d", &a, &b);
    
                if (!use[a][b]) ///去重边
                {
                    Add(a, b);
                    Add(b, a);
                    use[a][b] = use[b][a] = 1;
                }
            }
    
            Tarjan(1, 1); ///因为任意的两个点都至少有一条边,所以查找一次就行
    
            for (i = 1; i <= n; i++)
            {
                for (j = head[i]; j != -1; j = no[j].next)
                {
                    if (be[i] != be[no[j].v]) ///be数组存放的是该点属于第几个强连通分量
                        r[be[i]]++;
                }
            }
    
            for (i = 1; i <= ans; i++)
                if (r[i] == 1) num++; ///只有当等于1时,该点才是叶子节点
    
            printf("%d
    ", (num+1)/2); ///让每个叶子节点至少连接两条边,只有这样才能让任意两点之间至少有两条路
        }
    
        return 0;
    }
  • 相关阅读:
    企业nginx应用实例(功能拆分记录)
    Squid代理服务器的安装与配置
    关于mysql主从架构master宕机后,请求转移问题解决办法
    记一次innobackupex备份恢复数据库过程
    elasticsearch6.6及其插件安装记录(较详细)
    redis一主二从加哨兵
    nginx反向代理proxy_pass的问题
    spring程序打包war,直接通过-jar启动,并指定spring.profiles.active参数控制多环境配置
    Debian下配置防火墙iptables
    内连接和外连接
  • 原文地址:https://www.cnblogs.com/syhandll/p/4741467.html
Copyright © 2011-2022 走看看