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 ...
随机推荐
- centos下 yum安装ngix
1.CentOS 6,先执行:rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6. ...
- MySQL学习之一数据库简介
1.什么是数据库? 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库,长期储存在计算机内.有组织的.可共享的数据集合. 数据库中的数据指的是以一定的数据模型组织.描述和储存在一起. ...
- linux下ioctl遇到的坑
在驱动编程里面经常会用到ioctl的系统调用,发现cmd = 2的时候,用户ioctl直接返回-1. 原因在于在linux-x.xx/fs/ioctl.c定义的do_vfs_ioctl函数 int d ...
- C++语言对C的增强(1)——实用性、变量检测、struct类型、C++中所有变量和函数都必须有类型、bool类型、三目运算符
1.“实用性”增强 C语言中的变量都必须在作用域开始的位置定义,C++中更强调语言的“实用性”,所有的变量都可以在需要使用时再定义. 2.变量检测加强 在C语言中,重复定义多个同名的全局变量是合法的: ...
- 二:HTML文本编译器 kindeditor-4.1.10 的使用 SpringMVC+jsp的实现
这和一篇与上一篇的区别在与,上一篇是直接请求到action我们剩下的都是我们全部手动处理, 而这一片篇是由kindeditor内部处理,图片上传到本地,基本上没什么区别,但是有一点一定要注意的就是,这 ...
- el表达式对js方法的传值
我常用于在jsp页面遍历集合和分页中的页面跳转事件. jsp: <!-- 引入jstl --> <%@ taglib prefix="c" uri="h ...
- C语言库在不同系统下的后缀
C语言的静态库与动态库对比分析,各有长短 库: 指由标准常用函数编译而成的文件,旨在提高常用函数的可重用性,减轻开发人员负担.常用的sdtio.h,math.h等 库 ...
- Java-API:javax.servlet.http.HttpServletRequest
ylbtech-Java-API:javax.servlet.http.HttpServletRequest 1.返回顶部 1. javax.servlet.http Interface HttpSe ...
- Cassandra 学习一
一 什么是Cassandra? Cassandra 是一个来自 Apache 的分布式数据库,具有高度可扩展性,可用于管理大量的结构化数据.它提供了高可用性,没有单点故障. 是一种NoSQL类型的数 ...
- C Primer Plus学习笔记(七)- 字符输入/输出和输入验证
单字符 I/O:getchar() 和 putchar() getchar() 和 putchar() 每次只处理一个字符 getchar() 和 putchar() 都不是真正的函数,它们被定义为供 ...