题目:http://poj.org/problem?id=3694

 #include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
using namespace std;
const int MAXN=;
int bin[MAXN],n,m;
int low[MAXN],dfn[MAXN],father[MAXN];
int q,cut,index;
vector<int>g[MAXN]; int findx(int x)
{
return bin[x]==x?x:(bin[x]=findx(bin[x]));
}
int merge(int x,int y)
{
int fx,fy;
fx=findx(x);
fy=findx(y);
if(fx!=fy)
{
bin[fx]=fy;
return ;
}
else
return ;
} void tarjan(int i,int pre)
{
int j,k;
low[i]=dfn[i]=++index;
for (k=; k<g[i].size(); k++)
{
j = g[i][k];
if (!dfn[j])
{
tarjan(j,i);
low[i]=min(low[i],low[j]);
father[j]=i;
if (low[j] > dfn[i])
cut++;
else
merge(i,j);
}
else if (j!=pre)
{
low[i]=min(low[i],dfn[j]);
}
}
}
void lca(int u,int v)
{
while (u!=v)
{
while (dfn[u]>=dfn[v] && u!=v)
{
if (merge(u,father[u]))
cut--;
u = father[u];
}
while (dfn[v]>=dfn[u]&&u!=v)
{
if (merge(v,father[v]))
cut--;
v=father[v];
}
}
} int main()
{
int i,x,y,T=;
while(~scanf("%d%d",&n,&m)&&n||m)
{
for(i=; i<=n+; i++)
{
g[i].clear();
low[i]=dfn[i]=father[i]=;
bin[i]=i;
}
index=cut=; for(i=; i<m; i++)
{
scanf("%d%d",&x,&y);
g[x].push_back(y);
g[y].push_back(x);
}
tarjan(,-);
scanf("%d",&q); printf("Case %d:\n",T++);
while(q--)
{
scanf("%d%d",&x,&y);
lca(x,y);
printf("%d\n",cut);
}
cout<<endl;
}
return ;
}

最新文章

  1. VBA 获取Sheet最大行
  2. MySQL全文检索
  3. linux 安装mysql两种方式
  4. 软件工程——sprint 1回顾总结
  5. leetcode 143. Reorder List ----- java
  6. 常用的js代码
  7. com.classpath.www
  8. pydev出现Project interpreter not specified(eclipse+pydev)
  9. uva 11825 Hackers&amp;#39; Crackdown (状压dp,子集枚举)
  10. Freemodbus 1.5
  11. zepto js 源码 解读
  12. 第九周PSP&amp;进度条
  13. 内存问题排查工具 --- valgrind
  14. 你真的理解devDependencies和dependencies区别吗?
  15. python—退出ipython3的help()
  16. final修饰的变量是引用不能改变还是引用的对象不能改变
  17. System.getProperty()方法大全 (转载)
  18. Entity Framework + WCF 远程调用出错
  19. java 的exception throw try catch
  20. 初探boost之smart_ptr库学习笔记

热门文章

  1. SQL Server 2008 安装或卸载时提示“重启计算机失败&quot;的解决办法(转)
  2. DIV当textarea使用,在聚焦的时候将光标移动到内容的末尾
  3. php判断服务器是否支持Gzip压缩功能
  4. nginx如何实现404状态返回 200隐藏URL
  5. DB2 SQL 递归实现多行合并
  6. jsp日期控件My97DatePicker的使用
  7. Linux下crontab详解
  8. Jsonp 跨域请求实例
  9. ssh scp访问ipv6地址
  10. 1065: [NOI2008]奥运物流 - BZOJ