HDU4612-Warm up(无向图强连通分量缩点)

Warm up

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


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
 

题意:有n个点,m条无向边,问加一条边,最少可以剩下几个桥

解题思路:先双连通分量缩点,形成一颗树,然后求树的直径,就是减少的桥


#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

#define LL long long
const int INF=0x3f3f3f3f;

const int N=200010;
const int M=1000010;

struct Edge
{
    int v,nt,flag;
} edge[M<<2];
int s[N],cnt;
int n,m;
int dfn[N],low[N],dep,vis[N];
int id[N],res;
bool instack[N];
int nbridge,bridge[M][2];
int maxlen;
stackst;

void AddEdge(int u,int v)
{
    edge[cnt].v=v;
    edge[cnt].nt=s[u];
    edge[cnt].flag=1;
    s[u]=cnt++;
    edge[cnt].v=u;
    edge[cnt].nt=s[v];
    edge[cnt].flag=1;
    s[v]=cnt++;
}

void tarjan(int u,int pre)  // 无向图,数据有回边.需要将其看做不同边.且边需要标记...
{
    vis[u]=true;
    dfn[u]=low[u]=++dep;
    st.push(u);
    instack[u]=true;
    for(int i=s[u]; ~i; i=edge[i].nt)
    {
        int v=edge[i].v;
        if(edge[i].flag==false) continue;
        edge[i].flag=edge[i^1].flag=false;
        if(!vis[v])
        {
            tarjan(v,u);
            low[u]=min(low[u],low[v]);
            if(dfn[u]

你可能感兴趣的:(HDU,----模板,----连通图)