zoukankan      html  css  js  c++  java
  • Warm up-HUD4612(树的直径+Tarjin缩点)

    http://acm.hdu.edu.cn/showproblem.php?pid=4612

    题目大意:求加一条边最小的桥数

    先用Tarjin缩点求出一棵树,然后用bfs求出树的直径,树的直径就是加一条边桥最多的呢条边。

    最后就用桥减去直径就行了

    Warm up

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


    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
     
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<iostream>
    #include<stack>
    #include<queue>
    #include<cstdlib>
    
    using namespace std;
    const int N=200010;
    struct node
    {
        int next,to;
    }edge[N*10],Edge[N*10];
    int Stack[N],low[N],dfn[N],belong[N],sum,Time,top;
    int Head[N],head[N],ans,Ans,Max,dis[N],vis[N],End;
    
    void Inn()
    {
        memset(Stack,0,sizeof(Stack));
        memset(low,0,sizeof(low));
        memset(dfn,0,sizeof(dfn));
        memset(Head,-1,sizeof(Head));
        memset(head,-1,sizeof(head));
        memset(belong,0,sizeof(belong));
        memset(dis,0,sizeof(dis));
        sum=Time=top=ans=Ans=Max=End=0;
    }
    
    void add(int from,int to)
    {
        edge[ans].to=to;
        edge[ans].next=head[from];
        head[from]=ans++;
    }
    
    void Add(int from,int to)
    {
        Edge[Ans].to=to;
        Edge[Ans].next=Head[from];
        Head[from]=Ans++;
    }
    
    void Tarjin(int u,int f)
    {
        int k=0,v;
        low[u]=dfn[u]=++Time;
        Stack[top++]=u;
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            v=edge[i].to;
            if(v==f && !k)
            {
                k++;
                continue;
            }
            if(!dfn[v])
            {
                Tarjin(v,u);
                low[u]=min(low[u],low[v]);
            }
            else 
                low[u]=min(low[u],dfn[v]);
        }
        if(dfn[u]==low[u])
        {
            sum++;
            do
            {
                v=Stack[--top];
                belong[v]=sum;
            }while(v!=u);
        }
    }
    
    void dfs(int s)
    {
        queue<int>Q;
        int u,v;
        memset(vis,0,sizeof(vis));
        dis[s]=0;
        vis[s]=1;
        Q.push(s);
        while(Q.size())
        {
            u=Q.front();
            Q.pop();
            for(int i=Head[u];i!=-1;i=Edge[i].next)
            {
                v=Edge[i].to;
                if(!vis[v])
                {
                    vis[v]=1;
                    dis[v]=dis[u]+1;
                    Q.push(v);
                    if(Max<dis[v])
                    {
                        Max=dis[v];
                        End=v;
                    }
                }
            }
        }
    }
    
    void slove(int n)
    {
        int SUM=0;
        Tarjin(1,0);
        for(int i=1;i<=n;i++)
        {
            for(int j=head[i];j!=-1;j=edge[j].next)
            {
                int u=belong[i];
                int v=belong[edge[j].to];
                if(u!=v)
                {
                    Add(u,v);
                    SUM++;
                }
            }
        }
        SUM/=2;
        dfs(1);
        dfs(End);
        printf("%d
    ",SUM-Max);
    }
    int main()
    {
        int n,m,a,b;
        while(scanf("%d %d",&n,&m),n+m)
        {
            Inn();
            while(m--)
            {
                scanf("%d %d",&a,&b);
                add(a,b);
                add(b,a);
            }
            slove(n);
        }
        return 0;
    }
  • 相关阅读:
    【vue坑】vue组件不显示,没加载dom
    vue radio绑定数据
    git修改密码遇到的坑 git -- Authentication failed for
    python全局变量
    adb无法使用,提示error: unknown host service的解决办法
    uiautomator2 init初始化失败
    【解决方案】安装lxml失败 Installing lxml
    【一般都是源的问题】ubuntu使用apt-get update更新失败
    ubuntu 安装python3.6 以及安装pip3 出现Command '('lsb_release', '-a')' returned non-zero exit status 1问题解决
    ubuntu设置python软链python3.5和python3.6同时存在,python3指令使用python3.6
  • 原文地址:https://www.cnblogs.com/linliu/p/4930776.html
Copyright © 2011-2022 走看看