HDU 3686 Traffic Real Time Query System(双连通分量缩点+LCA)(2010 Asia Hangzhou Regional Contest)
For each test case:
The first line contains two integers N and M, representing the number of the crossings and roads.
The next M lines describe the roads. In those M lines, the ith line (i starts from 1)contains two integers Xi and Yi, representing that roadi connects crossing Xi and Yi (Xi≠Yi).
The following line contains a single integer Q, representing the number of RTQs.
Then Q lines follows, each describing a RTQ by two integers S and T(S≠T) meaning that a driver is now driving on the roads and he wants to reach roadt . It will be always at least one way from roads to roadt.
The input ends with a line of “0 0”.
Please note that: 0<N<=10000, 0<M<=100000, 0<Q<=10000, 0<Xi,Yi<=N, 0<S,T<=M
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
typedef long long LL; typedef pair<int, int> PII; const int MAXV = ;
const int MAXE = ; int ans[MAXV];
vector<PII> query[MAXV << ]; struct SccGraph {
int head[MAXV << ], fa[MAXV << ], ecnt;
bool vis[MAXV << ];
int to[MAXE << ], next[MAXE << ];
int dep[MAXV << ]; void init(int n) {
memset(head, -, sizeof(int) * (n + ));
memset(vis, , sizeof(bool) * (n + ));
for(int i = ; i <= n; ++i) fa[i] = i;
ecnt = ;
} int find_set(int x) {
return fa[x] == x ? x : fa[x] = find_set(fa[x]);
} void add_edge(int u, int v) {
to[ecnt] = v; next[ecnt] = head[u]; head[u] = ecnt++;
to[ecnt] = u; next[ecnt] = head[v]; head[v] = ecnt++;
} void lca(int u, int f, int deep) {
dep[u] = deep;
for(int p = head[u]; ~p; p = next[p]) {
int &v = to[p];
if(v == f || vis[v]) continue;
lca(v, u, deep + );
fa[v] = u;
}
vis[u] = true;
for(vector<PII>::iterator it = query[u].begin(); it != query[u].end(); ++it) {
if(vis[it->first]) {
ans[it->second] = (dep[u] + dep[it->first] - * dep[find_set(it->first)]) / ;
}
}
}
} G; int head[MAXV], lowlink[MAXV], pre[MAXV], ecnt, dfs_clock;
int sccno[MAXV], scc_cnt;
int to[MAXE], next[MAXE], scc_edge[MAXE];
bool vis[MAXE], iscut[MAXV];
int stk[MAXE], top;
int n, m, q; void init() {
memset(head, -, sizeof(int) * (n + ));
memset(pre, , sizeof(int) * (n + ));
memset(iscut, , sizeof(bool) * (n + ));
memset(vis, , sizeof(bool) * ( * m));
ecnt = scc_cnt = dfs_clock = ;
} void add_edge(int u, int v) {
to[ecnt] = v; next[ecnt] = head[u]; head[u] = ecnt++;
to[ecnt] = u; next[ecnt] = head[v]; head[v] = ecnt++;
} void tarjan(int u, int f) {
pre[u] = lowlink[u] = ++dfs_clock;
int child = ;
for(int p = head[u]; ~p; p = next[p]) {
if(vis[p]) continue;
vis[p] = vis[p ^ ] = true;
stk[++top] = p;
int &v = to[p];
if(!pre[v]) {
++child;
tarjan(v, u);
lowlink[u] = min(lowlink[u], lowlink[v]);\
if(pre[u] <= lowlink[v]) {
iscut[u] = true;
++scc_cnt;
while(true) {
int t = stk[top--];
scc_edge[t] = scc_edge[t ^ ] = scc_cnt;
if(t == p) break;
}
}
} else lowlink[u] = min(lowlink[u], pre[v]);
}
if(f < && child == ) iscut[u] = false;
} void build() {
G.init(scc_cnt);
for(int p = ; p != ecnt; ++p) {
int &v = to[p];
if(iscut[v]) G.add_edge(sccno[v], scc_edge[p]);
}
} void solve() {
for(int i = ; i <= n; ++i)
if(!pre[i]) tarjan(i, );
for(int u = ; u <= n; ++u)
if(iscut[u]) sccno[u] = ++scc_cnt;
} int main() {
while(scanf("%d%d", &n, &m) != EOF) {
if(n == && m == ) break;
init();
for(int i = ; i <= m; ++i) {
int u, v;
scanf("%d%d", &u, &v);
add_edge(u, v);
}
solve();
build();
for(int i = ; i <= scc_cnt; ++i) query[i].clear();
scanf("%d", &q);
for(int i = ; i < q; ++i) {
int x, y;
scanf("%d%d", &x, &y);
x = scc_edge[x * - ]; y = scc_edge[y * - ];
query[x].push_back(make_pair(y, i));
query[y].push_back(make_pair(x, i));
}
for(int i = ; i <= scc_cnt; ++i) if(!G.vis[i]) G.lca(i, , );
for(int i = ; i < q; ++i) printf("%d\n", ans[i]);
}
}
HDU 3686 Traffic Real Time Query System(双连通分量缩点+LCA)(2010 Asia Hangzhou Regional Contest)的更多相关文章
- 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。这题有重边!!!
http://acm.hdu.edu.cn/showproblem.php?pid=3686 我要把这题记录下来. 一直wa. 自己生成数据都是AC的.现在还是wa.留坑. 我感觉我现在倒下去床上就能 ...
- HDU 3686 Traffic Real Time Query System(点双连通)
题意 给定一张 \(n\) 个点 \(m\) 条边的无向图,\(q\) 次询问,每次询问两边之间的必经之点个数. 思路 求两点之间必经之边的个数用的是边双缩点,再求树上距离.而对比边双和点双之 ...
- HDU 3685 Rotational Painting(多边形质心+凸包)(2010 Asia Hangzhou Regional Contest)
Problem Description Josh Lyman is a gifted painter. One of his great works is a glass painting. He c ...
- HDU 3689 Infinite monkey theorem(DP+trie+自动机)(2010 Asia Hangzhou Regional Contest)
Description Could you imaging a monkey writing computer programs? Surely monkeys are smart among ani ...
- 【HDOJ】3686 Traffic Real Time Query System
这题做了几个小时,基本思路肯定是求两点路径中的割点数目,思路是tarjan缩点,然后以割点和连通块作为新节点见图.转化为lca求解.结合点——双连通分量与LCA. /* 3686 */ #includ ...
- POJ3694 Network(边双连通分量+缩点+LCA)
题目大概是给一张图,动态加边动态求割边数. 本想着求出边双连通分量后缩点,然后构成的树用树链剖分+线段树去维护路径上的边数和..好像好难写.. 看了别人的解法,这题有更简单的算法: 在任意两点添边,那 ...
- 图论-桥/割点/双连通分量/缩点/LCA
基本概念: 1.割点:若删掉某点后,原连通图分裂为多个子图,则称该点为割点. 2.割点集合:在一个无向连通图中,如果有一个顶点集合,删除这个顶点集合,以及这个集合中所有顶点相关联的边以后,原图变成多个 ...
- 【Targan+LCA】HDU 3686 Traffic Real Time Query
题目内容 洛谷链接 给出一个\(n\)个节点,\(m\)条边的无向图和两个节点\(s\)和\(t\),问这两个节点的路径中有几个点必须经过. 输入格式 第一行是\(n\)和\(m\). 接下来\(m\ ...
随机推荐
- Python之list添加新元素、删除元素、替换元素
Python之list添加新元素 现在,班里有3名同学: >>> L = ['Adam', 'Lisa', 'Bart'] 今天,班里转来一名新同学 Paul,如何把新同学添加到现有 ...
- pro10
1.本次课学习到的知识点: 什么是数组?为什么要使用数组?如何定义数组? 如何引用数组元素? 2. 实验过程中遇到的问题及解决方法: 在最后一个试验中,在输出最大小标那里进行了仔细的思考,刚开始思考应 ...
- 使用多种客户端消费WCF RestFul服务(四)——Jquery篇
Jquery篇 互联网开发中少不了各类前端开发框架,其中JQUERY就是最流行之一,本篇我们就采用JQUERY来消费WCF RestFul服务,其中用到JSON基础知识,如果有想了解的朋友,请访问:& ...
- 位与(&)常用编程技巧
补充知识:1)正整数的补码与原码相同: 2)求负整数的补码:原码 符号位不变,数值位各位取反,最后整个数加1得到补码: 3)按位与& ...
- xcode5 和code6中push后方法执行的先后问题
在xocde5中 执行的顺序是 prepareForSegue .viewDidLoad. didSelectRowAtIndexPath,在xcode6中 执行的顺序是 prepareForSe ...
- ASP.NET MVC3更新出错:ObjectStateManager中已存在具有同一键的对象
程序代码: [HttpPost] public ActionResult Edit(Person person) { if (ModelState.IsValid) { Person oldperso ...
- Windows-008-VirtualBox 安装 Win7 前沿配置详解
此文主要讲述在 Windows 系统下安装虚拟机 VirtualBox,及用 VirtualBox 安装 Win7 Professional 64bit 操作系统的前沿配置为例,配以图文进行详细的讲解 ...
- emmc boot1 boot2 partition
使用mfg tool烧写android5.1的镜像之后,再使用旧版的mfg tool烧写linux或者android镜像,都不能正常启动,而且运行的uboot还是android5.1版本的uboot. ...
- 从printf("\40d\n")看转义字符
1. 八进制 十进制 十六进制 二进制:0 1 2 3 4 5 6 7 \0(或省略0,\) ,\28 按道理是错误的,但是C语言把它解释为 \2,8错误了就不考虑 十进制:0 1 2 3 ...
- apt-get -y install中的-y是什么意思?
是同意的意思.没有 -y的命令也可以执行,系统会提示你是否安装,输入y,回车,就会安装了 apt-get -y install这个指令则是跳过系统提示,直接安装.