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

来源:互联网 发布:诺德软件 编辑:程序博客网 时间:2024/04/24 14:12

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 41 21 31 42 30 0
 

Sample Output
0
 

Author
SYSU
 

Source
2013 Multi-University Training Contest 2 


 题意让求  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 81 22 33 41 55 61 77 82 57 71 22 33 41 55 61 77 8*/


0 0
原创粉丝点击