zoukankan      html  css  js  c++  java
  • HDU 4612 Warm up(2013多校2 1002 双连通分量)

    Warm up

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


    Problem 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
     
    Source
     
    Recommend
    zhuyuanchen520
     

    问加一条边,最少可以剩下几个桥。

    先双连通分量缩点,形成一颗树,然后求树的直径,就是减少的桥。

    本题要处理重边的情况。

    如果本来就两条重边,不能算是桥。

    还会爆栈,只能C++交,手动加栈了

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <stdio.h>
    #include <string.h>
    #include <iostream>
    #include <algorithm>
    #include <map>
    #include <vector>
    using namespace std;
    
    const int MAXN = 200010;//点数
    const int MAXM = 2000010;//边数,因为是无向图,所以这个值要*2
    
    
    
    
    struct Edge
    {
        int to,next;
        bool cut;//是否是桥标记
        bool cong;
    }edge[MAXM];
    int head[MAXN],tot;
    int Low[MAXN],DFN[MAXN],Stack[MAXN],Belong[MAXN];//Belong数组的值是1~block
    int Index,top;
    int block;//边双连通块数
    bool Instack[MAXN];
    int bridge;//桥的数目
    
    void addedge(int u,int v,bool pp)
    {
        edge[tot].to = v;edge[tot].next = head[u];edge[tot].cut=false;
        edge[tot].cong = pp;
        head[u] = tot++;
    }
    
    void Tarjan(int u,int pre,bool ff)
    {
        int v;
        Low[u] = DFN[u] = ++Index;
        Stack[top++] = u;
        Instack[u] = true;
        for(int i = head[u];i != -1;i = edge[i].next)
        {
            v = edge[i].to;
            if(v == pre && (!ff))continue;
            if( !DFN[v] )
            {
                Tarjan(v,u,edge[i].cong);
                if( Low[u] > Low[v] )Low[u] = Low[v];
                if(Low[v] > DFN[u])
                {
                    bridge++;
                    edge[i].cut = true;
                    edge[i^1].cut = true;
                }
            }
            else if( Instack[v] && Low[u] > DFN[v] )
                Low[u] = DFN[v];
        }
        if(Low[u] == DFN[u])
        {
            block++;
            do
            {
                v = Stack[--top];
                Instack[v] = false;
                Belong[v] = block;
            }
            while( v!=u );
        }
    }
    void init()
    {
        tot = 0;
        memset(head,-1,sizeof(head));
    }
    
    int du[MAXN];//缩点后形成树,每个点的度数
    vector<int>vec[MAXN];
    int dep[MAXN];
    void dfs(int u)
    {
        for(int i = 0;i < vec[u].size();i++)
        {
            int v = vec[u][i];
            if(dep[v]!=-1)continue;
            dep[v]=dep[u]+1;
            dfs(v);
        }
    }
    void solve(int n)
    {
        memset(DFN,0,sizeof(DFN));
        memset(Instack,false,sizeof(Instack));
        Index = top = block = 0;
        Tarjan(1,0,false);
        for(int i = 1;i <= block;i++)
            vec[i].clear();
        for(int i = 1;i <= n;i++)
           for(int j = head[i];j != -1;j = edge[j].next)
              if(edge[j].cut)
              {
                  vec[Belong[i]].push_back(Belong[edge[j].to]);
              }
        memset(dep,-1,sizeof(dep));
        dep[1]=0;
        dfs(1);
        int k = 1;
        for(int i = 1;i <= block;i++)
            if(dep[i]>dep[k])
              k = i;
        memset(dep,-1,sizeof(dep));
        dep[k]=0;
        dfs(k);
        int ans = 0;
        for(int i = 1;i <= block;i++)
            ans = max(ans,dep[i]);
        printf("%d
    ",block-1-ans);
    }
    struct NN
    {
        int u,v;
    }node[MAXM];
    bool cmp(NN a,NN b)
    {
        if(a.u != b.u)return a.u<b.u;
        else return a.v<b.v;
    }
    int main()
    {
        //freopen("in.txt","r",stdin);
        //freopen("out.txt","w",stdout);
        int n,m;
        int u,v;
        while(scanf("%d%d",&n,&m)==2)
        {
            if(n==0 && m==0)break;
            init();
            for(int i = 0;i < m;i++)
            {
                scanf("%d%d",&u,&v);
                if(u==v)continue;
                if(u>v)swap(u,v);
                node[i].u = u;
                node[i].v = v;
            }
            sort(node,node+m,cmp);
            for(int i = 0;i < m;i++)
            {
                if(i == 0 || (node[i].u!=node[i-1].u || node[i].v != node[i-1].v))
                {
                    if(i < m-1 && (node[i].u==node[i+1].u && node[i].v == node[i+1].v))
                    {
                        addedge(node[i].u,node[i].v,true);
                        addedge(node[i].v,node[i].u,true);
                    }
                    else
                    {
                        addedge(node[i].u,node[i].v,false);
                        addedge(node[i].v,node[i].u,false);
                    }
                }
            }
            solve(n);
        }
        return 0;
    }
  • 相关阅读:
    BFC是什么?如何形成BFC,有什么作用?
    z-index 有什么作用? 如何使用?
    有几种定位方式?分别是如何实现定位的?参考点是什么?使用场景是什么?
    Promise
    token
    文档碎片
    ECharts常用配置项
    for each()和map()的区别
    window.onload和$(docunment).ready的区别
    密码的两种常用加密方式
  • 原文地址:https://www.cnblogs.com/kuangbin/p/3214879.html
Copyright © 2011-2022 走看看