Traffic Real Time Query System,题解
题目链接
题意:
问从一条边到另一条边的必经点。
分析:
首先,问必经点,当然是要点双缩点(圆方树)啦,关键是把边映射到哪一点上,其实直接放在某联通分量的方点上就行,但是这个点并不好找,所以我们考虑一个别的办法。
我们这样去考虑,如果这个边连着的有一个点不是割点,那么就直接给它找到方点就行了,但是如果是割点呢?那么我们就找一次所有的与这两个点相邻的方点,然后找到就好了,但是这样的复杂度我们并不喜欢,我们可以考虑换一种想法,有了圆方树之后,我们要找路径间的圆点的个数,其实你想一想,如果以圆点开头圆点结尾就可以直接根据总点数求出(当然不这样写也行),然后就是开始配对,边1连接的两点和边2连接的两点相配,分别求出路径上圆点的个数(不包括起点和终点),答案是什么呢?取max,为啥,因为都不包括起点和终点,为了防止某点为割点(+1并不行,是割点不一定就要过),我们只能进行取max,然后4次lca,最后找出答案。
代码:
#include <cstdio>
#include <string>
#include <cstring>
using namespace std;
const int maxn=2e4+;
const int maxm=1e5+;
struct E{
int to;
int next;
int from;
}ed[maxm*];
int head[maxn];
int tot;
void J(int a,int b){
tot++;
ed[tot].to=b;
ed[tot].from=a;
ed[tot].next=head[a];
head[a]=tot;
}
int dfn[maxn];
int low[maxn];
int sta[maxn];
int top;
int js;
int s;
int n;
E edx[maxn*];
int headx[maxn];
int totx;
void Jx(int a,int b){
totx++;
edx[totx].to=b;
edx[totx].next=headx[a];
headx[a]=totx;
}
void tarjan(int x){//缩点
js++;
low[x]=dfn[x]=js;
top++;
sta[top]=x;
for(int i=head[x];i;i=ed[i].next){
if(dfn[ed[i].to])
low[x]=min(low[x],dfn[ed[i].to]);
else{
tarjan(ed[i].to);
low[x]=min(low[ed[i].to],low[x]);
if(low[ed[i].to]==dfn[x]){
s++;
int tmp;
do{
tmp=sta[top];
top--;
Jx(n+s,tmp);
Jx(tmp,s+n);
}while(tmp!=ed[i].to);
Jx(x,n+s);
Jx(n+s,x);
}
}
}
}
int vis[maxn];
int dep[maxn];
int fa[maxn];
void Dfs(int x){
vis[x]=;
for(int i=headx[x];i;i=edx[i].next){
if(vis[edx[i].to])
continue;
dep[edx[i].to]=dep[x]+;
fa[edx[i].to]=x;
Dfs(edx[i].to);
}
}
int lc(int a,int b){//lca
if(dep[a]<dep[b])
swap(a,b);
while(dep[a]>dep[b])
a=fa[a];
if(a==b)
return a;
while(a!=b){
a=fa[a];
b=fa[b];
}
return a;
}
int l(int a,int b){
return (dep[a]+dep[b]-*dep[lc(a,b)])/-;
}
int main(){
int m;
while(~scanf("%d%d",&n,&m)&&(m||n)){
tot=;
memset(head,,sizeof(head));
totx=;
memset(headx,,sizeof(headx));
int js1,js2;
js=;
memset(dfn,,sizeof(dfn));
top=;
s=;
memset(vis,,sizeof(vis));
for(int i=;i<=m;i++){
scanf("%d%d",&js1,&js2);
J(js1,js2);
J(js2,js1);
}
for(int i=;i<=n;i++)
if(!dfn[i])
tarjan(i);
for(int i=;i<=n+s;i++)
if(!vis[i])
Dfs(i);
int q;
scanf("%d",&q);
for(int i=;i<=q;i++){
scanf("%d%d",&js1,&js2);
printf("%d\n",max(max(l(ed[js1*].to,ed[js2*].to),l(ed[js1*].from,ed[js2*].to)),max(l(ed[js1*].to,ed[js2*].from),l(ed[js1*].from,ed[js2*].from))));//分别计算
}
}
return ;
}
Traffic Real Time Query System,题解的更多相关文章
- 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 ...
- UVALive-4839 HDU-3686 Traffic Real Time Query System 题解
题目大意: 有一张无向连通图,问从一条边走到另一条边必定要经过的点有几个. 思路: 先用tarjan将双连通分量都并起来,剩下的再将割点独立出来,建成一棵树,之后记录每个点到根有几个割点,再用RMQ求 ...
- CH#24C 逃不掉的路 和 HDU3686 Traffic Real Time Query System
逃不掉的路 CH Round #24 - 三体杯 Round #1 题目描述 现代社会,路是必不可少的.任意两个城镇都有路相连,而且往往不止一条.但有些路连年被各种XXOO,走着很不爽.按理说条条大路 ...
- 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, ...
- HDU Traffic Real Time Query System
题目大意是:对于(n, m)的图,给定边a, b查询从a到b要经过的割点的最少数目. 先tarjan算法求双连通然后缩点,即对于每个割点将周围的每个双连通看成一个点与之相连.然后求解LCA即可,距离d ...
- HDU3686 Traffic Real Time Query System
P.S.此题无代码,只有口胡,因为作者码炸了. 题目大意 给你一个有 \(n\) 个点, \(m\) 条边的无向图,进行 \(q\) 次询问,每次询问两个点 \(u\) \(v\),输出两个点的之间的 ...
- 【HDOJ】3686 Traffic Real Time Query System
这题做了几个小时,基本思路肯定是求两点路径中的割点数目,思路是tarjan缩点,然后以割点和连通块作为新节点见图.转化为lca求解.结合点——双连通分量与LCA. /* 3686 */ #includ ...
随机推荐
- html页面引用video.js播放m3u8格式视频
//head里面的内容,我是采用cdn引用的方式,因为项目太小 <head> <meta charset="utf-8" /> <title>二 ...
- lambda表达式操作DataTable番外篇
using System;using System.Collections.Generic;using System.Data;using System.Linq;using System.Text; ...
- 因为 MongoDB 没入门,我丢了一份实习工作
有时候不得不感慨一下,系统升级真的是好处多多,不仅让我有机会重构了之前的烂代码,也满足了我积极好学的虚荣心.你看,Redis 入门了.Elasticsearch 入门了,这次又要入门 MongoDB, ...
- PAT1040 Longest Symmetric String (25分) 中心扩展法+动态规划
题目 Given a string, you are supposed to output the length of the longest symmetric sub-string. For ex ...
- 性能调优必备利器之 JMH
if 快还是 switch 快?HashMap 的初始化 size 要不要指定,指定之后性能可以提高多少?各种序列化方法哪个耗时更短? 无论出自何种原因需要进行性能评估,量化指标总是必要的. 在大部分 ...
- 附022.Kubernetes_v1.18.3高可用部署架构一
kubeadm介绍 kubeadm概述 参考附003.Kubeadm部署Kubernetes. kubeadm功能 参考附003.Kubeadm部署Kubernetes. 本方案描述 本方案采用kub ...
- [noi.ac省选模拟赛20200606]赌怪
题目 点这里看题目. 分析 先特判掉\(K=2\)的情况. 首先可以考虑到一个简单 DP : \(f(i)\):前\(i\)张牌的最大贡献. 转移可以\(O(n^2)\)地枚举区间 ...
- 关于Attach *.mdf数据库联想到的备份
要求: 将SQL2008R2的*.mdf ( 当时内部版本不详,此时无挂接在MSSQL服务器上的数据库,只有*.mdf文件 ) --->>> SQL2008R2中,附加到现有SQL2 ...
- 03 . Jenkins构建之代码扫描
Sonar简介 Sonar 是一个用于代码质量管理的开放平台.通过插件机制,Sonar可以集成不同的测试工具,代码分析工具,以及持续集成工具.与持续集成工具(例如 Hudson/Jenkins 等)不 ...
- 全网最全fiddler使用教程和fiddler如何抓包(fiddler手机抓包)-笔者亲测
一.前言 抓包工具有很多,比如常用的抓包工具Httpwatch,通用的强大的抓包工具Wireshark.为什么使用fiddler?原因如下:1.Wireshark是通用的抓包工具,但是比较庞大,对于只 ...