poj 3694 Network(双连通分量)
2024-10-11 00:59:56
题目: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 ;
}
最新文章
- VBA 获取Sheet最大行
- MySQL全文检索
- linux 安装mysql两种方式
- 软件工程——sprint 1回顾总结
- leetcode 143. Reorder List ----- java
- 常用的js代码
- com.classpath.www
- pydev出现Project interpreter not specified(eclipse+pydev)
- uva 11825 Hackers&;#39; Crackdown (状压dp,子集枚举)
- Freemodbus 1.5
- zepto js 源码 解读
- 第九周PSP&;进度条
- 内存问题排查工具 --- valgrind
- 你真的理解devDependencies和dependencies区别吗?
- python—退出ipython3的help()
- final修饰的变量是引用不能改变还是引用的对象不能改变
- System.getProperty()方法大全 (转载)
- Entity Framework + WCF 远程调用出错
- java 的exception throw try catch
- 初探boost之smart_ptr库学习笔记