http://acm.hdu.edu.cn/showproblem.php?pid=3686
推荐一道辣鸡题目,毁我青春.
所以还没调出来啊啊啊啊
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<stack>
using namespace std;
const int MAXBIT=20;
const int MAXN=40000;
const int MAXM=800000;
struct Edge{int u,v;};
int n,m,q;
int fir[MAXN+10],nxt[MAXM+10],tot;
int nfir[MAXN+10],nnxt[MAXM+10],ntot;
Edge edges[MAXM+10];
Edge nedges[MAXM+10];
void adde(int u,int v){edges[++tot]=(Edge){u,v};nxt[tot]=fir[u];fir[u]=tot;}
void nadde(int u,int v){nedges[++ntot]=(Edge){u,v};nnxt[ntot]=nfir[u];nfir[u]=ntot;}
int dfn[MAXN+10],low[MAXN+10],dfstime,in[MAXN+10],bcc;
bool iscut[MAXN+10];
stack<Edge> S;
void dfs1(int u,int fa)
{
int cnt=0;dfn[u]=low[u]=++dfstime;
for(int i=fir[u];i;i=nxt[i])
{
Edge& e=edges[i];
if(e.v==fa||e.v==u)continue;
if(!dfn[e.v])
{
S.push(e);
++cnt;
dfs1(e.v,u);
low[u]=min(low[u],low[e.v]);
if(low[e.v]>=dfn[u])
{
++bcc;iscut[u]=true;
while(1)
{
Edge& x=S.top();S.pop();
if(in[x.u]!=bcc){in[x.u]=bcc;nadde(x.u,n+bcc);nadde(n+bcc,x.u);}
if(in[x.v]!=bcc){in[x.v]=bcc;nadde(x.v,n+bcc);nadde(n+bcc,x.v);}
if(x.u==e.u&&x.v==e.v)break;
}
}
}
else if(dfn[e.v]<dfn[u])low[u]=min(low[u],dfn[e.v]);
}
if(fa==0&&cnt==1)iscut[u]=false;
}
int fa[MAXN+10][MAXBIT+5],dep[MAXN+10];
int l[MAXN+10];
void dfs2(int u,int f)
{
fa[u][0]=f;for(int i=1;i<=MAXBIT;++i)fa[u][i]=fa[fa[u][i-1]][i-1];
l[u]=iscut[u]+l[f];
dep[u]=dep[f]+1;
for(int i=nfir[u];i;i=nnxt[i])
{
Edge& e=nedges[i];
if(e.v==f||e.v==u)continue;
dfs2(e.v,u);
}
}
int lca(int x,int y)
{
if(dep[x]<dep[y])swap(x,y);
for(int i=MAXBIT;~i;--i)if(dep[fa[x][i]]>=dep[y])x=fa[x][i];
if(x==y)return x;
for(int i=MAXBIT;~i;--i)if(fa[x][i]!=fa[y][i])x=fa[x][i],y=fa[y][i];
return fa[x][0];
}
int get(int x,int y)
{
int fa=lca(x,y);
return l[x]+l[y]-2*l[fa]+iscut[fa];
}
void init()
{
memset(fir,0,sizeof fir);memset(nxt,0,sizeof nxt);memset(edges,0,sizeof edges);tot=0;
memset(nfir,0,sizeof nfir);memset(nnxt,0,sizeof nnxt);memset(nedges,0,sizeof nedges);ntot=0;
memset(dfn,0,sizeof dfn);memset(low,0,sizeof low);dfstime=0;memset(in,0,sizeof in);bcc=0;
memset(iscut,0,sizeof iscut);
while(!S.empty())S.pop();
memset(fa,0,sizeof fa);memset(dep,0,sizeof dep);memset(l,0,sizeof l);
}
int main()
{
while(scanf("%d%d",&n,&m),n)
{
init();
for(int i=1,u,v;i<=m;++i)
{
scanf("%d%d",&u,&v);
adde(u,v);adde(v,u);
}
for(int i=1;i<=n;++i)if(!dfn[i])dfs1(i,0);
for(int i=1;i<=n;++i)if(!dep[i])dfs2(i,0);
scanf("%d",&q);
while(q--)
{
int s,t;scanf("%d%d",&s,&t);
int a=edges[s<<1].u,b=edges[t<<1].u;
if(iscut[a])a=edges[s<<1].v;
if(iscut[b])b=edges[t<<1].v;
printf("%d
",get(a,b));
}
}
return 0;
}