J 点击打开链接
#include<stdio.h>
#include<string.h>
#include<stack>
#include<string>
#include<math.h>
#include<queue>
#include<set>
#include<algorithm>
#include<iostream>
#include<vector>
#include<map>
using namespace std;
#define LL long long
#define inf 199999999
#define N 200005
int low[N],dfn[N],belong[N],pre[N],num[N],f[N],bridge[N];
vector<int>edge[N];
stack<int>s;
int cnt,n,m,Index,ans,sum,flag;
bool vis[N];
void init()
{
memset(low,0,sizeof(low));
memset(dfn,0,sizeof(dfn));
memset(vis,0,sizeof(vis));
memset(f,0,sizeof(f));
memset(bridge,0,sizeof(bridge));
memset(num,0,sizeof(num));
memset(belong,0,sizeof(belong));
memset(pre,0,sizeof(pre));
for(int i=1;i<=n;i++) edge[i].clear();
while(!s.empty()) s.pop();
flag=Index=cnt=sum=0;
}
void Lca(int u,int v)
{
while(dfn[u]>dfn[v])
{
if(bridge[u])
{
cnt--;
bridge[u]=0;
}
u=f[u];
}
while(dfn[v]>dfn[u])
{
if(bridge[v])
{
cnt--;
bridge[v]=0;
}
v=f[v];
}
while(u!=v)
{
if(bridge[u])
{
cnt--;
bridge[u]=0;
}
if(bridge[v])
{
cnt--;
bridge[v]=0;
}
u=f[u];v=f[v];
}
}
void tarjan(int u)
{
low[u]=dfn[u]=++Index;
for(int i=0;i<edge[u].size();i++)
{
int v=edge[u][i];
if(!dfn[v])
{
f[v]=u;
tarjan(v);
low[u]=min(low[u],low[v]);
if(dfn[u]<low[v])
{
cnt++;
bridge[v]=1;
}
}
else if(f[u]!=v) low[u]=min(low[u],dfn[v]);
}
}
int main()
{
int u,v,i,j,ca=1,q;
while(scanf("%d%d",&n,&m),n+m)
{
init();
for(i=0;i<m;i++)
{
scanf("%d%d",&u,&v);
edge[u].push_back(v);
edge[v].push_back(u);
}
tarjan(1);
scanf("%d",&q);
printf("Case %d:\n",ca++);
while(q--)
{
scanf("%d%d",&u,&v);
Lca(u,v);
printf("%d\n",cnt);
}
puts("");
}
return 0;
}