HDU3686 Traffic Real Time Query【缩点+lca】
题目
City C is really a nightmare of all drivers for its traffic jams. To solve the traffic problem, the mayor plans to build a RTQS (Real Time Query System) to monitor all traffic situations. City C is made up of N crossings and M roads, and each road connects two crossings. All roads are bidirectional. One of the important tasks of RTQS is to answer some queries about route-choice problem. Specifically, the task is to find the crossings which a driver MUST pass when he is driving from one given road to another given road.
Input
There are multiple test cases.
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 i th line (i starts from 1)contains two integers X i and Y i, representing that road i connects crossing X i and Y i (X i≠Y i).
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<X i,Y i<=N, 0<S,T<=M
Output
For each RTQ prints a line containing a single integer representing the number of crossings which the driver MUST pass.
Sample Input
5 6
1 2
1 3
2 3
3 4
4 5
3 5
2
2 3
2 4
0 0
Sample Output
0 1
分析
大概的题目意思就是给个无向图,问从a到b的路径中有几个点必须经过。
思路:根据题意,很容易就可以想到这个题就是求a到b的路径上割点的个数。然后就可以开始缩点了。把边缩成一个点,因为每条边有且仅属于一个联通块中,然后对割点和它相邻的块建边,这样就构造了一棵树。询问a边和b边,只需要找出它们分别属于哪个块中就行,所以问题转化成了一棵树中,有些点标记了是割点,现在询问两个不为割点的点路径上有多少个割点。
这样就很容易做了,以任意一个点为树根,求出每个点到树根路径上有多少个割点,然后对于询问的两个点求一次LCA就可以求出结果了,有点小细节不多说,自己画个图就清楚了。
注意:缩点后树的点数可能是2n个。
代码
#include<cstdio>
#include <vector>
#include <algorithm>
using namespace std; const int maxn = + ;
const int maxm = + ; struct Edge {
int u, to, next, vis, id;
}edge[maxm<<]; int head[maxn<<], dfn[maxn<<], low[maxn], st[maxm], iscut[maxn], subnet[maxn], bian[maxm];
int E, time, top, btot;
vector<int> belo[maxn];
void newedge(int u, int to) {
edge[E].u = u;
edge[E].to = to;
edge[E].next = head[u];
edge[E].vis = ;
head[u] = E++;
} void init(int n) {
for(int i = ;i <= n; i++) {
head[i] = -;
dfn[i] = iscut[i] = subnet[i] = ;
belo[i].clear();
}
E = time = top = btot = ;
} void dfs(int u) {
dfn[u] = low[u] = ++time;
for(int i = head[u];i != -;i = edge[i].next) {
if(edge[i].vis) continue;
edge[i].vis = edge[i^].vis = ;
int to = edge[i].to;
st[++top] = i;
if(!dfn[to]) {
dfs(to);
low[u] = min(low[u], low[to]);
if(low[to] >= dfn[u]) {
subnet[u]++;
iscut[u] = ;
btot++;
do {
int now = st[top--];
belo[edge[now].u].push_back(btot);
belo[edge[now].to].push_back(btot);
bian[edge[now].id] = btot;
to = edge[now].u;
}while(to != u);
}
}
else
low[u] = min(low[u], low[to]);
}
} int B[maxn<<], F[maxn<<], d[maxn<<][], pos[maxn<<], tot, dep[maxn<<];
bool treecut[maxn<<];
void RMQ1(int n) {
for(int i = ;i <= n; i++) d[i][] = B[i];
for(int j = ;(<<j) <= n; j++)
for(int i = ;i + j - <= n; i++)
d[i][j] = min(d[i][j-], d[i + (<<(j-))][j-]);
} int RMQ(int L, int R) {
int k = ;
while((<<(k+)) <= R-L+) k++;
return min(d[L][k], d[R-(<<k)+][k] );
} int lca(int a, int b) {
if(pos[a] > pos[b]) swap(a, b);
int ans = RMQ(pos[a], pos[b]);
return F[ans];
} // 搜树来构造RMQ LCA
void DFS(int u) {
dfn[u] = ++time;
B[++tot] = dfn[u];
F[time] = u;
pos[u] = tot;
for(int i = head[u];i != -;i = edge[i].next){
int to = edge[i].to;
if(!dfn[to]) {
if(treecut[u])
dep[to] = dep[u] + ;
else
dep[to] = dep[u];
DFS(to);
B[++tot] = dfn[u];
}
}
} void solve(int n) {
for(int i = ;i <= n; i++) {
dfn[i] = ;
}
time = tot = ;
for(int i = ;i <= n; i++) if(!dfn[i]) {
dep[i] = ;
DFS(i);
}
RMQ1(tot);
int m, u, to;
scanf("%d", &m);
while(m--) {
scanf("%d%d", &u, &to);
u = bian[u]; to = bian[to];
if(u < || to < ) {
printf("0\n"); continue;
}
int LCA = lca(u, to);
if(u == LCA)
printf("%d\n", dep[to] - dep[u] - treecut[u]);
else if(to == LCA)
printf("%d\n", dep[u] - dep[to] - treecut[to]);
else
printf("%d\n", dep[u] + dep[to] - *dep[LCA] - treecut[LCA]);
}
} int main() {
int n, m, u, to;
while(scanf("%d%d", &n, &m) != - && n){
init(n);
for(int i = ;i <= m; i++) {
scanf("%d%d", &u, &to);
edge[E].id = i;
newedge(u, to);
edge[E].id = i;
newedge(to, u);
} for(int i = ;i <= n;i ++) if(!dfn[i]) {
dfs(i);
subnet[i]--;
if(subnet[i] <= ) iscut[i] = ;
}
int ditot = btot;
for(int i = ;i <= btot; i++) treecut[i] = ;
for(int i = ;i <= btot+n; i++) head[i] = -;
E = ;
for(int i = ;i <= n; i++) if(iscut[i]) {
sort(belo[i].begin(), belo[i].end());
ditot++;
treecut[ditot] = ;
newedge(belo[i][], ditot);
newedge(ditot, belo[i][]);
for(int j = ;j < belo[i].size(); j++) if(belo[i][j] != belo[i][j-]) {
newedge(belo[i][j], ditot);
newedge(ditot, belo[i][j]);
}
}
solve(ditot);
}
return ;
}
HDU3686 Traffic Real Time Query【缩点+lca】的更多相关文章
- CH#24C 逃不掉的路 和 HDU3686 Traffic Real Time Query System
逃不掉的路 CH Round #24 - 三体杯 Round #1 题目描述 现代社会,路是必不可少的.任意两个城镇都有路相连,而且往往不止一条.但有些路连年被各种XXOO,走着很不爽.按理说条条大路 ...
- 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 ...
- HDU3686 Traffic Real Time Query
按照vdcc缩点之后一条边只会属于一个新的点集,由于这棵树上满足(不是割点) - (割点) - (不是割点)的连接方法,所以求两条边之间的必经点就是(树上距离 / 2),倍增跳lca即可 考虑到缩点后 ...
- HDU3686 Traffic Real Time Query System
P.S.此题无代码,只有口胡,因为作者码炸了. 题目大意 给你一个有 \(n\) 个点, \(m\) 条边的无向图,进行 \(q\) 次询问,每次询问两个点 \(u\) \(v\),输出两个点的之间的 ...
- UVALive-4839 HDU-3686 Traffic Real Time Query System 题解
题目大意: 有一张无向连通图,问从一条边走到另一条边必定要经过的点有几个. 思路: 先用tarjan将双连通分量都并起来,剩下的再将割点独立出来,建成一棵树,之后记录每个点到根有几个割点,再用RMQ求 ...
- 边的双联通+缩点+LCA(HDU3686)
Traffic Real Time Query System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- 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 ...
- Traffic Real Time Query System 圆方树+LCA
题目描述 City C is really a nightmare of all drivers for its traffic jams. To solve the traffic problem, ...
随机推荐
- SpringBoot 定制 starter 启动器
个人博客网:https://wushaopei.github.io/ (你想要这里多有) 在实际项目开发中,我们常常会用到各种各样的 starter,这些starter 有的是有 springb ...
- Java实现 LeetCode 556 下一个更大元素 III(数组的翻转)
556. 下一个更大元素 III 给定一个32位正整数 n,你需要找到最小的32位整数,其与 n 中存在的位数完全相同,并且其值大于n.如果不存在这样的32位整数,则返回-1. 示例 1: 输入: 1 ...
- Java实现 LeetCode 319 灯泡开关
319. 灯泡开关 初始时有 n 个灯泡关闭. 第 1 轮,你打开所有的灯泡. 第 2 轮,每两个灯泡你关闭一次. 第 3 轮,每三个灯泡切换一次开关(如果关闭则开启,如果开启则关闭).第 i 轮,每 ...
- Java实现 LeetCode 230 二叉搜索树中第K小的元素
230. 二叉搜索树中第K小的元素 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 说明: 你可以假设 k 总是有效的,1 ≤ k ≤ 二叉搜索树元素个数. ...
- Java实现矩阵相乘问题
1 问题描述 1.1实验题目 设M1和M2是两个n×n的矩阵,设计算法计算M1×M2 的乘积. 1.2实验目的 (1)提高应用蛮力法设计算法的技能: (2)深刻理解并掌握分治法的设计思想: (3)理解 ...
- java实现控制台表格
画表格 在图形环境中很容易做出漂亮的表格.但在控制台环境中就比较困难了.有的时候可以用一些符号大略地模拟:(word文档中可能不整齐,拷贝到记事本中看) +-------+------+ |abc | ...
- Java实现信用卡校验
当你输入信用卡号码的时候,有没有担心输错了而造成损失呢?其实可以不必这么担心,因为并不是一个随便的信用卡号码都是合法的,它必须通过Luhn算法来验证通过. 该校验的过程: 1.从卡号最后一位数字开始, ...
- java实现第七届蓝桥杯打靶
打靶 题目描述 小明参加X星球的打靶比赛. 比赛使用电子感应计分系统.其中有一局,小明得了96分. 这局小明共打了6发子弹,没有脱靶. 但望远镜看过去,只有3个弹孔. 显然,有些子弹准确地穿过了前边的 ...
- 基于ABP做一个简单的系统——实战篇:1.项目准备
现阶段需要做一个小项目,体量很小,业务功能比较简单,就想到用最熟悉的.net来做,更何况现在.net core已经跨平台,也可以在linux服务器上部署.所以决定用.net core 3.1+mysq ...
- 曹工说JDK源码(2)--ConcurrentHashMap的多线程扩容,说白了,就是分段取任务
前言 先预先说明,我这边jdk的代码版本为1.8.0_11,同时,因为我直接在本地jdk源码上进行了部分修改.调试,所以,导致大家看到的我这边贴的代码,和大家的不太一样. 不过,我对源码进行修改.重构 ...