hdu 4612 Warm up (dfs+tarjan缩点+树的直径)

5 篇文章 0 订阅
2 篇文章 0 订阅

Warm up
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 8534 Accepted Submission(s): 1962

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

题意

一个无向图,加一条边,使得图中桥边最少。

思路

先用tarjan缩点,将整个图缩成一棵树,树中的每个边都是原图中的桥,因为要添加一条边使得桥最少,于是添加的那条边肯定是加在树上相距最远的两个点上,这样可以使得最多的树上的边加入到环中,最远两个点的距离即为树的直径,两次dfs求出树的直径之后,用桥的数量减去你加的那条边抵消的桥(即树的直径)的个数,即为剩余的最少的桥。

至于求树的直径,先随便找一个点dfs,找到离他最远的点,再以这个点为源点dfs,找到最远的距离即为树的直径。

注意:因为本题中可能会有重边,所以给每条边加一个标志位,如果这条边已经访问过的话,那么把i这条边和i^1这两条边的标志位都置为1,因为邻接表保存无向图的时候,两个相邻的edge肯定就是图中一条边的两个方向。

代码

#define push_back pb
#define make_pair mk
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<cmath>
#include<map>
#include<algorithm>
#include<string>
#include<string.h>
#include<set>
#include<queue>
#include<bitset>
#include<stack>
#include<functional>
using std::pair;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<double,int>PDI;
const double PI=acos(-1);
const int maxn=2e5+10;
const int maxm=1e6+10;
const int INF = 0x3f3f3f3f;
using namespace std;
struct edge{int to,next,flag;}e[maxm<<2];
int n,m;
int id[maxn],instack[maxn],index,dfn[maxn],low[maxn],scccnt,cnt;
int S[maxn],top;
int head[maxn];
int bridge[maxm][2];
int bcnt;
int maxlen;
int po;
int vis[maxn];
vector<int>g[maxn];
void tarjan(int u)
{
    low[u]=dfn[u]=++index;
    instack[u]=1;
    S[++top]=u;
    for(int i=head[u];~i;i=e[i].next)
    {
        int v=e[i].to;
        if(!e[i].flag) continue;
        e[i].flag=e[i^1].flag=0;
        if(!dfn[v])
        {
            tarjan(v);
            low[u]=min(low[u],low[v]);
            if(dfn[u]<low[v])
            {
                bridge[bcnt][0]=u;
                bridge[bcnt++][1]=v;
            }
        }
        else if(instack[v]) low[u]=min(low[u],dfn[v]);
    }
    if(dfn[u]==low[u])
    {
        scccnt++;
        while(1)
        {
            int x=S[top--];
            id[x]=scccnt;
            instack[x]=0;
            if(x==u) break;
        }
    }
}
int dfs(int u,int dis)
{
    vis[u]=1;
    if(dis>maxlen)maxlen=dis,po=u;
    int sz=g[u].size();
    for(int i=0;i<sz;i++)
    {
        int v=g[u][i];
        if(!vis[v])
        {
            dfs(v,dis+1);
        }
    }
}
void find_scc()
{
    memset(dfn,0,sizeof(dfn));
    top=-1;
    index=0;
    bcnt=scccnt=0;
    for(int i=1;i<=n;i++) if(!dfn[i]) tarjan(i);
}
void add(int u,int v)
{
    e[cnt].to=v;
    e[cnt].next=head[u];
    e[cnt].flag=1;
    head[u]=cnt++;
}
int main()
{
    while(cin>>n>>m&&(n||m))
    {
        int u,v;
        memset(head,-1,sizeof(head));
        cnt=0;
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&u,&v);
            add(u,v);
            add(v,u);
        }
        find_scc();
        for(int i=1;i<=n;i++) g[i].clear();
        for(int i=1;i<=n;i++)
        {
            for(int j=head[i];~j;j=e[j].next)
            {
                //cout<<"a"<<endl;
                int v=e[j].to;
                if(id[v]!=id[i])
                {
                    g[id[i]].pb(id[v]);
                    g[id[v]].pb(id[i]);
                }
            }
        }
        maxlen=0;
        memset(vis,0,sizeof(vis));
        dfs(1,0);
        maxlen=0;
        memset(vis,0,sizeof(vis));
        dfs(po,0);
        //cout<<bcnt<<" "<<maxlen<<endl;
        printf("%d\n",bcnt-maxlen);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值