zoukankan      html  css  js  c++  java
  • HDU4612:Warm up(缩点+树的直径)

    Warm up

    Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
    Total Submission(s): 9073    Accepted Submission(s): 2120

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4612

    Description:

    N planets are connected by M bidirectional channels that allow instant transportation. It's always possible to travel between any two planets through these channels.
    If we can isolate some planets from others by breaking only one channel , the channel is called a bridge of the transportation system.
    People don't like to be isolated. So they ask what's the minimal number of bridges they can have if they decide to build a new channel.
    Note that there could be more than one channel between two planets.

    Input:

    The input contains multiple cases.
    Each case starts with two positive integers N and M , indicating the number of planets and the number of channels.
    (2<=N<=200000, 1<=M<=1000000)
    Next M lines each contains two positive integers A and B, indicating a channel between planet A and B in the system. Planets are numbered by 1..N.
    A line with two integers '0' terminates the input.

    Output:

    For each case, output the minimal number of bridges after building a new channel in a line.

    Sample Input:

    4 4
    1 2
    1 3
    1 4
    2 3
    0 0 

    Sample Output:

    0

    题意:

    给出一个无向图,之后会加进来一条边,问加进来一条边过后,桥的最少数量为多少。

    题解:

    还是利用并查集先缩点,将无向图变为一颗树,树上每一条边都为桥。

    然后加边我们希望尽量形成最大的环,那么考虑树的直径两端的点就满足了。

    代码如下:

    #include <cstdio>
    #include <algorithm>
    #include <iostream>
    #include <cstring>
    #include <queue>
    using namespace std;
    typedef long long ll;
    const int N = 2e5+5,M = 1e6+5;
    int n,m;
    int head[N];
    struct Edge{
        int u,v,next;
        bool operator < (const Edge &A){
            if(u==A.u) return v<A.v;
            return u<A.u;
        }
    }e[M<<1],g[M<<1];
    int T,tot,cnt;
    int dfn[N],low[N],cut[N],num[N],f[N];
    void adde(int u,int v){
        e[tot].u=u;e[tot].v=v;e[tot].next=head[u];head[u]=tot++;
    }
    void init(){
        T=0;tot=0;cnt=0;
        memset(head,-1,sizeof(head));
        memset(cut,0,sizeof(cut));
        memset(dfn,0,sizeof(dfn));
        memset(num,0,sizeof(num));
        for(int i=0;i<=n+1;i++) f[i]=i;
    }
    int find(int x){
        return f[x]==x?f[x]:f[x]=find(f[x]);
    }
    void Union(int x,int y){
        int fx=find(x),fy=find(y);
        if(fx!=fy) f[fx]=fy;
    }
    void Tarjan(int u,int pre){
        dfn[u]=low[u]=++T;
        int k=0;
        for(int i=head[u];i!=-1;i=e[i].next){
            int v=e[i].v;
            if(v==pre && !k){
                k=1;
                continue ;
            }
            if(!dfn[v]){
                Tarjan(v,u);
                low[u]=min(low[u],low[v]);
            }else{
                low[u]=min(low[u],dfn[v]);
            }
            if(low[v]>dfn[u]){
                cut[v]=1;
            }else Union(u,v);
        }
    }
    int mx=0,node=1;
    void dfs(int u,int d,int pa){
        if(d>mx){
            mx=d;
            node=u;
        }
        for(int i=head[u];i!=-1;i=e[i].next){
            int v=e[i].v;
            if(v==pa) continue ;
            dfs(v,d+1,u);
        }
    }
    int main(){
        while(scanf("%d%d",&n,&m)!=EOF){
            if(!n&&!m) break ;
            init();
            for(int i=1;i<=m;i++){
                int u,v;
                scanf("%d%d",&u,&v);
                adde(u,v);adde(v,u);
                if(u>v)swap(u,v);
                g[i].u=u;g[i].v=v;
            }
            sort(g+1,g+m+1);
            Tarjan(1,0);
            memset(head,-1,sizeof(head));tot=0;
            for(int i=1;i<=m;i++){
                int u=g[i].u,v=g[i].v;
                if(g[i].u==g[i-1].u&&g[i].v==g[i-1].v) continue ;
                int fx=find(u),fy=find(v);
                if(!num[fx]) num[fx]=++cnt;
                if(!num[fy]) num[fy]=++cnt;
                if(num[fx]==num[fy]) continue ;
                adde(num[fx],num[fy]);adde(num[fy],num[fx]);
            }
            mx=0;
            dfs(1,0,-1);
            mx=0;
            dfs(node,0,-1);
            cout<<cnt-1-mx<<endl;
        }
        return 0;
    }
  • 相关阅读:
    用ASP+DLL实现WEB方式修改服务器时间
    参加了 湖南.NET俱乐部成立大会
    Asp.Net中文本换行
    一直在思考的问题
    GRIDVIEW排序 动态实现和静态实现
    在VS 2005中使用TREEVIEW控件
    GRIDVIEW 中当数据行数未满时,填充空白行
    为了自己的心身健康 合理安排生活 特做了张时间安排表
    在VS 2005后台代码中创建用户控件
    CSS IE7 IE6 Firefox多浏览器兼容(转&摘)
  • 原文地址:https://www.cnblogs.com/heyuhhh/p/10393312.html
Copyright © 2011-2022 走看看