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;
    }
  • 相关阅读:
    C# WinForm开发系列 Socket/WCF/Rometing/Web Services
    .net(c#) 简单的软件注册功能的实现:
    来自xici网友的Ubuntu和windows xp的Super PI性能测试
    最新的Linpack测试指南-基于woodcrest机器
    CFX x86_64 version issues 无法找到可执行文件
    如何检查一个mvapich的版本?
    Intel Tools Training Notes Intel Compiler, MKLs
    Infiniband IPoIB Debug FAQ
    让CFX的license server在开机的时候就自动启动
    FFTW 3.1.2 和 2.1.5编译
  • 原文地址:https://www.cnblogs.com/linliu/p/4930776.html
Copyright © 2011-2022 走看看