HDU-4612-Warm up(无向图缩点+直径)

48 篇文章 0 订阅

Warm up

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


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


 题意让求  ans=缩点后的边数-直径边数。
缩点后 两遍DFS搜直径就好,第一遍DFS求出的点p为最深点或次深点,从p开始第二次DFS求出的点q为次深点或最深点,p到q长度就是直径。


#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <algorithm>
#include <string>
#include <set>
#include <stack>
#include <map>
#include <queue>
using namespace std;
const int MAXM = 1e6+1e6+7;
const int M= 1e6+7;
const int MAXN = 200007;
int head[MAXN],h[MAXN],index;
struct node
{
    int v,vis;
    int next;
}edge[MAXM];
vector<int> e[MAXN];
void add(int u, int v)
{
    edge[index].vis=0;
    edge[index].v=v;
    edge[index].next=head[u];
    head[u]=index++;
}
int low[MAXN],DFN[MAXN],belong[MAXN];
int stack_[MAXN],top;
bool in_stack[MAXN];
int dps,cir;
void Tarjan(int u)
{
    in_stack[u]=1;
    low[u]=DFN[u]=++dps;
    stack_[top++]=u;
    for(int i=head[u]; i+1; i=edge[i].next)
    {
        int v=edge[i].v;
        if(edge[i].vis)continue;
        edge[i].vis=edge[i^1].vis=1;
        if(!DFN[v])
        {
            Tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else if(in_stack[v] && edge[i^1].v)
            low[u]=min(low[u],DFN[v]);
    }
    int p;
    if(low[u]==DFN[u])
    {
        cir++;
        do{
            p=stack_[--top];
            in_stack[p]=0;
            belong[p]=cir;
        }while(p!=u);
    }
}

int MAX_1,MAX_2,poi_1;
bool vis[MAXN];
int ans;
void DFS(int u,int deep)
{
    vis[u]=1;
    if(deep>MAX_1)MAX_1=deep,poi_1=u;
    for(int i=0; i<e[u].size(); i++)
    {
        int v=e[u][i];
        if(!vis[v])
            {
                DFS(v,deep+1);
                ans++;
            }
    }
}
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        if(n==0)break;
        index=0;
        memset(head,-1,sizeof(head));
        int a,b;
        for(int i=0; i<m; ++i)
        {
            scanf("%d%d",&a,&b);
            add(a,b);
            add(b,a);
        }
        memset(in_stack,0,sizeof(in_stack));
        memset(low,0,sizeof(low));
        memset(DFN,0,sizeof(DFN));
        dps=top=cir=0;
        Tarjan(1);
        memset(h,-1,sizeof(h));
        index=0;
        for(int i=1; i<=n; ++i)e[i].clear();
        for(int i=1; i<=n; ++i)
            for(int j=head[i]; j+1; j=edge[j].next)
            {
                int v=edge[j].v;
                if(belong[i]!=belong[v])
                {
                    e[ belong[i] ].push_back(belong[v]);
                    e[ belong[v] ].push_back(belong[i]);
                }
            }
        memset(vis,0,sizeof(vis));
        ans=MAX_1=0;
        DFS(1,0);
        int temp=ans;
        memset(vis,0,sizeof(vis));
        MAX_1=0;
        DFS(poi_1,0);
        cout<<temp-MAX_1<<endl;
    }
    return 0;
}

/*
8 8
1 2
2 3
3 4
1 5
5 6
1 7
7 8
2 5

7 7
1 2
2 3
3 4
1 5
5 6
1 7
7 8
*/


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值