hdu 4612 Warm up(边双连通分量+缩点+dfs)

Warm up

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


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
 

Author
SYSU
 

Source
2013 Multi-University Training Contest 2
 

Recommend
zhuyuanchen520

询问如何加一条边,使得剩下的桥的数目最少,输出数目
本题要处理重边的情况。
如果本来就两条重边,不能算是桥。
网上说还会爆栈,只能C++交,手动加栈,但是我的好像没有这个问题啊,难道我的Tarjan比较吊???大笑
貌似G++手动开栈不行,所以待验证。。。
//#pragma comment(linker, "/STACK:102400000,102400000"
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<ctime>
#define eps 1e-6
#define MAX 100005
#define INF 0x3f3f3f3f
#define LL long long
#define pii pair<int,int>
#define rd(x) scanf("%d",&x)
#define rd2(x,y) scanf("%d%d",&x,&y)
using namespace std;

const int MAXN = 200010;//点数
const int MAXM = 2000010;//边数,因为是无向图,所以这个值要*2
struct Edge
{
    int to,next;
    bool cut;//是否是桥标记
}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)
{
    edge[tot].to = v;edge[tot].next = head[u];edge[tot].cut=false;
    head[u] = tot++;
}

void Tarjan(int u,int pre)
{
    int v;
    Low[u] = DFN[u] = ++Index;
    Stack[top++] = u;
    int pre_cnt=0;  ///处理重边
    Instack[u] = true;
    for(int i = head[u];i != -1;i = edge[i].next)
    {
        v = edge[i].to;
        if(v == pre && pre_cnt == 0 ){
                pre_cnt ++;
                continue;
        }
        if( !DFN[v] )
        {
            Tarjan(v,u);
            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);
    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); ///第一次dfs找距离1节点最远的节点k
    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);  ///第二次dfs找出距离k最远的节点,也就是树的直径
    int ans = 0;
    for(int i = 1;i <= block;i++)
        ans = max(ans,dep[i]);
    printf("%d\n",block-1-ans);
}
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);
            addedge(u,v);
            addedge(v,u);
        }
        solve(n);
    }
    return 0;
}


你可能感兴趣的:(hdu 4612 Warm up(边双连通分量+缩点+dfs))