UVALive-4839 HDU-3686 Traffic Real Time Query System 题解
题目大意:
有一张无向连通图,问从一条边走到另一条边必定要经过的点有几个。
思路:
先用tarjan将双连通分量都并起来,剩下的再将割点独立出来,建成一棵树,之后记录每个点到根有几个割点,再用RMQ求LCA计算。
注意:数组范围。
代码:
#include<cstdio>
#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=,M=;
int u[M<<],v[M<<],nex[M<<],id[M<<],hea[N<<],dfn[N<<],low[N],st[M],sub[N],edge[M],
fa[N<<][],f[N<<],pos[N<<],dep[N<<];
bool vis[M<<],iscut[N],treecut[N<<];
int tim,top,tot,cnt,num;
vector <int> belo[N]; int read()
{
int x=; char ch=getchar();
while (ch<'' || ch>'') ch=getchar();
while (ch>='' && ch<='') x=(x<<)+(x<<)+ch-,ch=getchar();
return x;
} void add(int x,int y) { v[cnt]=y,u[cnt]=x,nex[cnt]=hea[x],vis[cnt]=,hea[x]=cnt++; } void tarjan(int x)
{
dfn[x]=low[x]=++tim;
for (int i=hea[x];~i;i=nex[i])
if (!vis[i])
{
int y=v[i]; st[++top]=i;
vis[i]=vis[i^]=;
if (!dfn[y])
{
tarjan(y);
low[x]=min(low[x],low[y]);
if (low[y]>=dfn[x])
{
++sub[x],++num;
iscut[x]=;
do
{
int now=st[top--];
belo[u[now]].push_back(num);
belo[v[now]].push_back(num);
edge[id[now]]=num;
y=u[now];
}while (y^x);
}
}
else low[x]=min(low[x],dfn[y]);
}
} void dfs(int x)
{
dfn[x]=++tim; fa[++tot][]=dfn[x];
f[tim]=x; pos[x]=tot;
for (int i=hea[x];~i;i=nex[i])
{
int y=v[i];
if (!dfn[y])
{
dep[y]=dep[x]+treecut[x];
dfs(y); fa[++tot][]=dfn[x];
}
}
} void RMQ(int n)
{
for (int j=;(<<j)<=n;++j)
for (int i=;i+j-<=n;++i)
fa[i][j]=min(fa[i][j-],fa[i+(<<j-)][j-]);
} int lca(int x,int y)
{
if (pos[x]<pos[y]) swap(x,y);
int k=;
while (<<(k+)<=pos[x]-pos[y]+) ++k;
return f[min(fa[pos[y]][k],fa[pos[x]-(<<k)+][k])];
} void wk(int n)
{
int i,m,x,y;
for (tim=tot=i=;i<=n;++i) dfn[i]=;
for (i=;i<=n;++i)
if (!dfn[i]) dep[i]=,dfs(i);
RMQ(tot);
for (m=read();m--;)
{
x=edge[read()],y=edge[read()];
if (x< || y<) { puts(""); continue; }
int z=lca(x,y);
if (x==z) printf("%d\n",dep[y]-dep[x]-treecut[x]);
else if (y==z) printf("%d\n",dep[x]-dep[y]-treecut[y]);
else printf("%d\n",dep[x]+dep[y]-(dep[z]<<)-treecut[z]);
}
} int main()
{
int n,m;
while (~scanf("%d%d",&n,&m))
{
int i; cnt=top=num=tim=;
if (!(n+m)) break;
for (i=;i<=n;++i) hea[i]=-,dfn[i]=sub[i]=iscut[i]=,belo[i].clear();
for (i=;i<=m;++i)
{
int x=read(),y=read();
id[cnt]=i,add(x,y),id[cnt]=i,add(y,x);
}
for (i=;i<=n;++i)
if (!dfn[i])
{
tarjan(i);
if (--sub[i]<=) iscut[i]=;
}
for (i=;i<=num;++i) treecut[i]=;
for (i=;i<=num+n;++i) hea[i]=-;
cnt=;
for (i=;i<=n;++i)
if (iscut[i])
{
sort(belo[i].begin(),belo[i].end());
treecut[++num]=;
add(belo[i][],num),add(num,belo[i][]);
for (int j=;j<belo[i].size();++j)
if (belo[i][j]^belo[i][j-]) add(belo[i][j],num),add(num,belo[i][j]);
}
wk(num);
}
return ;
}
UVALive-4839 HDU-3686 Traffic Real Time Query System 题解的更多相关文章
- HDU 3686 Traffic Real Time Query System (图论)
		HDU 3686 Traffic Real Time Query System 题目大意 给一个N个点M条边的无向图,然后有Q个询问X,Y,问第X边到第Y边必需要经过的点有多少个. solution ... 
- HDU 3686 Traffic Real Time Query System(双连通分量缩点+LCA)(2010 Asia Hangzhou Regional Contest)
		Problem Description City C is really a nightmare of all drivers for its traffic jams. To solve the t ... 
- hdu 3686 Traffic Real Time Query System  点双两通分量 + LCA。这题有重边!!!
		http://acm.hdu.edu.cn/showproblem.php?pid=3686 我要把这题记录下来. 一直wa. 自己生成数据都是AC的.现在还是wa.留坑. 我感觉我现在倒下去床上就能 ... 
- HDU 3686 Traffic Real Time Query System(点双连通)
		题意  给定一张 \(n\) 个点 \(m\) 条边的无向图,\(q\) 次询问,每次询问两边之间的必经之点个数. 思路  求两点之间必经之边的个数用的是边双缩点,再求树上距离.而对比边双和点双之 ... 
- HDU3686 Traffic Real Time Query System 题解
		题目 City C is really a nightmare of all drivers for its traffic jams. To solve the traffic problem, t ... 
- 【Targan+LCA】HDU 3686 Traffic Real Time Query
		题目内容 洛谷链接 给出一个\(n\)个节点,\(m\)条边的无向图和两个节点\(s\)和\(t\),问这两个节点的路径中有几个点必须经过. 输入格式 第一行是\(n\)和\(m\). 接下来\(m\ ... 
- 【HDOJ】3686 Traffic Real Time Query System
		这题做了几个小时,基本思路肯定是求两点路径中的割点数目,思路是tarjan缩点,然后以割点和连通块作为新节点见图.转化为lca求解.结合点——双连通分量与LCA. /* 3686 */ #includ ... 
- CH#24C 逃不掉的路 和 HDU3686 Traffic Real Time Query System
		逃不掉的路 CH Round #24 - 三体杯 Round #1 题目描述 现代社会,路是必不可少的.任意两个城镇都有路相连,而且往往不止一条.但有些路连年被各种XXOO,走着很不爽.按理说条条大路 ... 
- Traffic Real Time Query System 圆方树+LCA
		题目描述 City C is really a nightmare of all drivers for its traffic jams. To solve the traffic problem, ... 
随机推荐
- 使用php+swoole对client数据实时更新(下)
			上一篇提到了swoole的基本使用,现在通过几行基本的语句来实现比较复杂的逻辑操作: 先说一下业务场景.我们目前的大多数应用都是以服务端+接口+客户端的方式去协调工作的,这样的好处在于不论是处在何种终 ... 
- MySQL 备份与恢复
			一.备份/恢复策略 考虑因素有: (A) 表的存储引擎是否事务性的,在数据一致性方面不太一样. (B) 确定是全备份还是增量备份, (C) 考虑采取复制的方法做异地备份,复制不能代替备份 (D) 定期 ... 
- 前端网老姚浅谈:怎么学JavaScript?
			作者:小不了链接:https://zhuanlan.zhihu.com/p/23265155来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 鉴于时不时,有同学私信问我( ... 
- (转)android 蓝牙通信编程
			转自:http://blog.csdn.net/pwei007/article/details/6015907 Android平台支持蓝牙网络协议栈,实现蓝牙设备之间数据的无线传输. 本文档描述了怎样 ... 
- ms08-067漏洞--初识渗透测试--想必很多初学者都会遇到我文中提及的各种问题
			最近读了一本书--<<渗透测试实践指南>>,测试了书中的一些例子后,开始拿ms08-067这个经典的严重漏洞练手,实践当中遇到诸多问题,好在一一解决了,获益匪浅. 在谷歌搜索的 ... 
- Cordova环境搭建 & HelloWorld
			目前的手机APP有三类:原生APP,WebAPP,HybridApp:HybridApp结合了前两类APP各自的优点,越来越流行. Cordova就是一个中间件,让我们把WebAPP打包成Hybrid ... 
- mysql 5.7 的安装配置与 navicat premium for mysql 11 的破解使用
			再安装mysql5.7 或以上的版本出现了一些问题,现在总结下,希望能给初入学习mysql的人一下帮助,大牛就不要来嘲笑小弟我了 首先准备如下: 1.下载mysql 5.7,下载地址:https:// ... 
- 关于 printf() 函数的三张表格
			函数原型: printf(Control-String, item1, item2, ...); 表一 转换说明符及作为结果的打印输出 转 换 说 明 输 出 %a 浮点数.十六进制数字和p- ... 
- 循环获取DataTable
			DataTable ddtt = tq.Table("); string strName = ""; //DataTable ddtt = dataSet.Tables[ ... 
- [转]金融业容灾技术分析 (终于看到QREP了)
			源地址:http://www.cnblogs.com/SuperXJ/p/3480929.html 数据复制技术很多,初步比较如下. 后面重点讨论银行最常用的存储复制和数据库复制..当然,我最推荐的还 ... 
