HDU3686 Traffic Real Time Query
按照vdcc缩点之后一条边只会属于一个新的点集,由于这棵树上满足(不是割点) - (割点) - (不是割点)的连接方法,所以求两条边之间的必经点就是(树上距离 / 2),倍增跳lca即可
考虑到缩点后树上点数的编号可能超过n,所以与树有关的数组开两倍N
又是一个模板
Code:
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std; const int N = 1e4 + ;
const int M = 1e5 + ;
const int Lg = ; int n, m, qn, tot, head[][N << ], top, sta[N];
int dfsc, dfn[N], low[N], dccCnt, root;
int bel[N], eb[M], fa[N << ][Lg], dep[N << ], id[N];
bool cut[N], vis[N << ];
vector <int> dcc[N]; struct Edge {
int to, nxt, id;
} e[M << ]; inline void add(int type, int from, int to, int eid) {
e[++tot].to = to;
e[tot].id = eid;
e[tot].nxt = head[type][from];
head[type][from] = tot;
} inline void read(int &X) {
X = ;
char ch = ;
int op = ;
for(; ch > ''|| ch < ''; ch = getchar())
if(ch == '-') op = -;
for(; ch >= '' && ch <= ''; ch = getchar())
X = (X << ) + (X << ) + ch - ;
X *= op;
} inline int min(int x, int y) {
return x > y ? y : x;
} void tarjan(int x) {
dfn[x] = low[x] = ++dfsc;
sta[++top] = x;
if(x == root && head[][x] == ) {
dcc[++dccCnt].push_back(x);
return;
} int son = ;
for(int i = head[][x]; i; i = e[i].nxt) {
int y = e[i].to;
if(!dfn[y]) {
tarjan(y);
low[x] = min(low[x], low[y]);
if(dfn[x] <= low[y]) {
++son;
if(x != root || son > ) cut[x] = ;
++dccCnt;
for(int z; ; ) {
z = sta[top--];
dcc[dccCnt].push_back(z);
if(z == y) break;
}
dcc[dccCnt].push_back(x);
}
} else low[x] = min(low[x], dfn[y]);
}
} void dfs(int x, int fat, int depth) {
vis[x] = , fa[x][] = fat, dep[x] = depth;
for(int i = ; i <= ; i++)
fa[x][i] = fa[fa[x][i - ]][i - ];
for(int i = head[][x]; i; i = e[i].nxt) {
int y = e[i].to;
if(vis[y]) continue;
dfs(y, x, depth + );
}
} inline void swap(int &x, int &y) {
int t = x;
x = y;
y = t;
} inline int getLca(int x, int y) {
if(dep[x] < dep[y]) swap(x, y);
for(int i = ; i >= ; i--)
if(dep[fa[x][i]] >= dep[y])
x = fa[x][i];
if(x == y) return x;
for(int i = ; i >= ; i--)
if(fa[x][i] != fa[y][i])
x = fa[x][i], y = fa[y][i];
return fa[x][];
} int main() {
for(; ; ) {
read(n), read(m);
if(n == && m == ) break;
tot = ;
memset(head, , sizeof(head));
for(int x, y, i = ; i <= m; i++) {
read(x), read(y);
add(, x, y, i), add(, y, x, i);
} dfsc = dccCnt = top = ;
memset(dfn, , sizeof(dfn));
memset(low, , sizeof(low));
memset(cut, , sizeof(cut));
for(int i = ; i <= n; i++) dcc[i].clear(); for(int i = ; i <= n; i++)
if(!dfn[i]) tarjan(root = i); int now = dccCnt;
for(int i = ; i <= n; i++)
if(cut[i]) id[i] = ++now; /* printf("\n");
for(int i = 1; i <= dccCnt; i++, printf("\n")) {
for(unsigned int j = 0; j < dcc[i].size(); j++)
printf("%d ", dcc[i][j]);
}
printf("\n"); */ for(int i = ; i <= dccCnt; i++) {
for(unsigned int j = ; j < dcc[i].size(); j++) {
int x = dcc[i][j];
if(cut[x]) add(, id[x], i, ), add(, i, id[x], );
bel[x] = i;
} for(unsigned int j = ; j < dcc[i].size(); j++) {
int x = dcc[i][j];
for(int k = head[][x]; k; k = e[k].nxt) {
int y = e[k].to;
if(bel[y] == i) eb[e[k].id] = i;
}
}
} /* for(int i = 1; i <= n; i++)
printf("%d ", bel[i]);
printf("\n"); for(int i = 1; i <= m; i++)
printf("%d ", eb[i]);
printf("\n"); */ memset(dep, , sizeof(dep));
memset(fa, , sizeof(fa));
memset(vis, , sizeof(vis));
for(int i = ; i <= now; i++)
if(!vis[i]) dfs(i, , ); read(qn);
for(int x, y; qn--; ) {
read(x), read(y);
x = eb[x], y = eb[y];
if(x == y) puts("");
else printf("%d\n", (dep[x] + dep[y] - * dep[getLca(x, y)]) / );
}
}
return ;
}
lyd给的std好难看
HDU3686 Traffic Real Time Query的更多相关文章
- CH#24C 逃不掉的路 和 HDU3686 Traffic Real Time Query System
逃不掉的路 CH Round #24 - 三体杯 Round #1 题目描述 现代社会,路是必不可少的.任意两个城镇都有路相连,而且往往不止一条.但有些路连年被各种XXOO,走着很不爽.按理说条条大路 ...
- HDU3686 Traffic Real Time Query【缩点+lca】
题目 City C is really a nightmare of all drivers for its traffic jams. To solve the traffic problem, t ...
- 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求 ...
- HDU3686 Traffic Real Time Query System
P.S.此题无代码,只有口胡,因为作者码炸了. 题目大意 给你一个有 \(n\) 个点, \(m\) 条边的无向图,进行 \(q\) 次询问,每次询问两个点 \(u\) \(v\),输出两个点的之间的 ...
- 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 ...
随机推荐
- New Concept English three (52)
My cousin, Harry, keeps a large curiously-shaped bottle on permanent display in his study. Despite t ...
- canvas基础学习(三)
一.图片加载控件 在canvas效果制作中常常要使用多张图片,为了提高用户体验,需要给用户提供一个图片加载中的过度页面效果.过度效果,我在项目中使用的是Sonic.js,它在git上的地址为https ...
- UVA - 11214 Guarding the Chessboard (可重复覆盖,DLX+IDA*)
题目链接 正解是IDA*+四个方向判重,但由于是个裸的可重复覆盖问题,可以用DLX水过~ 每个格子与放上皇后能干掉的标记连边,跑可重复覆盖DLX.注意要用IDA*来优化,否则会超时. #include ...
- 反编译(javap)操作和可视化界面
1.反编译(javap)操作和可视化界面 演示:
- NOIp2018集训test-10-24(am&pm)
李巨连续AK三场了,我跟南瓜打赌李巨连续AK七场,南瓜赌李巨连续AK五场. DAY1 T1 qu 按题意拿stack,queue和priority_que模拟即可.特判没有元素却要取出的情况. T2 ...
- C#程序性能优化
http://blog.csdn.net/scalzdp/article/details/34421639 程序中我们每一丝动作都会加大程序运行的负担,当刚开始学习程序的时候常常不会去考虑程序运行的效 ...
- 预备架构的工具ADMEMS矩阵
矩阵,是很多著名方法的核心.例如,制定公司层战略的方法之一是"波士顿矩阵","波士顿矩阵"又称"市场增长率-相对市场份额矩阵". " ...
- Jeesite开垦
1. userIndex里面,$ctx是在哪里定义的? 就是request.context 2. 增加新的包后,扫描配置修改1) spring-context.xml文件中,扫描非@controlle ...
- SpringBoot自动化配置之一:SpringBoot内部的一些自动化配置原理
springboot用来简化Spring框架带来的大量XML配置以及复杂的依赖管理,让开发人员可以更加关注业务逻辑的开发. 比如不使用springboot而使用SpringMVC作为web框架进行开发 ...
- [Apache]架设Apache服务器
我自己使用的是Ubuntu的操作系统, 所以我主要是记录的在ubuntu的Apache的安装和简单的配置. Apache服务器的架设: 一.命令行安装 使用下面的指令下载apache2 sudo ap ...