• poj 3177


    Redundant Paths
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 7651   Accepted: 3337

    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.

    Source

     
    给定无向图, 含重边,求无向边双连通分量。参考了byvoid大神的算法,
    首先,主要用tarjan求强连通分量算法,但要记下反向弧,以避免把非强连通分量错当为强连通分量;
    然后,缩点,构造一颗树,求叶子节点即可。求叶节点:只要数每个缩点的出度,若==1,则为叶节点; 因为若节点非叶节点,则其出度必非1,否则必为1,这样得到叶节点数量;
    最后,两两连接叶节点,若最后还剩一个叶节点,则将其与非叶节点相连,以构造双联通分量。这样的答案是minimal...
    另外,poj 3352 应该是同一算法。
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<vector>
    #include<cstdlib>
    #include<algorithm>
    
    using namespace std;
    
    #define LL long long
    #define ULL unsigned long long
    #define UINT unsigned int
    #define MAX_INT 0x7fffffff
    #define MAX_LL 0x7fffffffffffffff
    #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
    #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
    
    #define MAXN 5555
    #define MAXM 22222
    
    struct edge{
        int u, v, nxt;
        int re;
    }e[MAXM];
    int cc,h[MAXN];
    
    inline void add(int u, int v, int f){
        e[cc]=(edge){u, v, h[u],cc+f};
        h[u]=cc++;
    }
    
    int dfn[MAXN],id[MAXN],low[MAXN];
    int s[MAXN],top,tsp,cnt;
    int vis[MAXM];
    
    void tarjan(int u){
        int v,i;
        dfn[u]=low[u]=++tsp;
        s[top++]=u;
        for(i=h[u]; i!=-1; i=e[i].nxt){
            v=e[i].v;
            if(!dfn[v]){
                vis[e[i].re]=1;       //反向边
                tarjan(v);
                low[u]=MIN(low[u], low[v]);
            }                           //如果访问过,则v为u的祖先节点,所以low[v]=dfn[v],low[u]的取值符合定义
            else if(!vis[i]) low[u]=MIN(low[u], low[v]);     //回边不是反向边
        }
        if(low[u] == dfn[u]){
            cnt++;
            do{
                v=s[--top];
                id[v]=cnt;                  //缩点
                //printf("u:%d id:%d
    ",v, id[v]);
            }while(v!=u);
        }
    }
    
    int degree[MAXN];
    int solve(int n){
        top=tsp=cnt=0;
        memset(dfn, 0, sizeof(dfn));
        memset(vis, 0, sizeof(vis));
        tarjan(1);
        memset(degree, 0, sizeof(degree));
        for(int i=0; i<cc; i++){
            int u=id[e[i].u], v=id[e[i].v];
            if(u!=v) degree[u]++;
        }
        int ans=0;
        for(int i=1; i<=cnt; i++) if(1==degree[i])
            ans++;
        //printf(" %d
    ",ans);
        return ans+1>>1;
    }
    
    int main(){
        int n,m;
       // freopen("C:\Users\Administrator\Desktop\in.txt","r",stdin);
        while(scanf(" %d %d",&n,&m)==2){
            int i,u,v;
            memset(h, -1, sizeof(h));       cc=0;
            for(i=0; i<m; i++){
                scanf(" %d %d",&u, &v);
                add(u, v, 1);      add(v, u, -1);
            }
            int ans=solve(n);
            printf("%d
    ",ans);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    【基础】Oracle 表空间和数据文件
    oracle学习笔记1:windows下oracle数据库安装及.net调用oracle数据库
    redis中文网站
    .NET中的消息队列
    .Net下的进程间的通讯 -- Windows消息队列
    1060 最复杂的数(反素数玄学dfs)
    1282 时钟(最小表示法+hash)
    1191 消灭兔子(贪心+优先队列)
    1366 贫富差距(floyed)
    1503 猪和回文(DP)
  • 原文地址:https://www.cnblogs.com/ramanujan/p/3294121.html
Copyright © 2020-2023  润新知