按照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的更多相关文章

  1. CH#24C 逃不掉的路 和 HDU3686 Traffic Real Time Query System

    逃不掉的路 CH Round #24 - 三体杯 Round #1 题目描述 现代社会,路是必不可少的.任意两个城镇都有路相连,而且往往不止一条.但有些路连年被各种XXOO,走着很不爽.按理说条条大路 ...

  2. 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 ...

  3. 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 ...

  4. UVALive-4839 HDU-3686 Traffic Real Time Query System 题解

    题目大意: 有一张无向连通图,问从一条边走到另一条边必定要经过的点有几个. 思路: 先用tarjan将双连通分量都并起来,剩下的再将割点独立出来,建成一棵树,之后记录每个点到根有几个割点,再用RMQ求 ...

  5. HDU3686 Traffic Real Time Query System

    P.S.此题无代码,只有口胡,因为作者码炸了. 题目大意 给你一个有 \(n\) 个点, \(m\) 条边的无向图,进行 \(q\) 次询问,每次询问两个点 \(u\) \(v\),输出两个点的之间的 ...

  6. HDU 3686 Traffic Real Time Query System (图论)

    HDU 3686 Traffic Real Time Query System 题目大意 给一个N个点M条边的无向图,然后有Q个询问X,Y,问第X边到第Y边必需要经过的点有多少个. solution ...

  7. 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 ...

  8. Traffic Real Time Query System 圆方树+LCA

    题目描述 City C is really a nightmare of all drivers for its traffic jams. To solve the traffic problem, ...

  9. HDU Traffic Real Time Query System

    题目大意是:对于(n, m)的图,给定边a, b查询从a到b要经过的割点的最少数目. 先tarjan算法求双连通然后缩点,即对于每个割点将周围的每个双连通看成一个点与之相连.然后求解LCA即可,距离d ...

随机推荐

  1. poj3268 Silver Cow Party (SPFA求最短路)

    其实还是从一个x点出发到所有点的最短路问题.来和回只需分别处理一下逆图和原图,两次SPFA就行了. #include<iostream> #include<cstdio> #i ...

  2. 洛谷 P3223 [HNOI2012]排队

    题目描述 某中学有 n 名男同学,m 名女同学和两名老师要排队参加体检.他们排成一条直线,并且任意两名女同学不能相邻,两名老师也不能相邻,那么一共有多少种排法呢?(注意:任意两个人都是不同的) 输入输 ...

  3. webpack 开发环境

    当项目逐渐变大,webpack 的编译时间会变长,可以通过参数让编译的输出内容带有进度和颜色. $ webpack --progress --colors 如果不想每次修改模块后都重新编译,那么可以启 ...

  4. vertex shader must minimally write all four components of POSITION

    Though the POSITION semantic must be written out by the vertex shader, it cannot be read in by the p ...

  5. 【转】探秘Java中的String、StringBuilder以及StringBuffer

    探秘Java中String.StringBuilder以及StringBuffer 相信String这个类是Java中使用得最频繁的类之一,并且又是各大公司面试喜欢问到的地方,今天就来和大家一起学习一 ...

  6. MongoDB数据库的备份和恢复

    MongoDB数据库备份方式: 1.整库备份 2.单表备份 1.整库备份 备份整个数据库: mongodump -h 127.0.0.1:27000 -d park --authenticationD ...

  7. CentOS7网卡设置为桥接模式静态IP配置方法详解

    备份网络文件 [root@localhost network-scripts]# cd /etc/sysconfig/network-scripts/ [root@localhost network- ...

  8. winrm service

    今天看脚本忽然发现一个服务,叫winRM服务,这是个PowerShell的远程管理.开启它可以很大程度的方便用PowerShell操控! 下面是我找到的一些资料: 在Linux中,我们可以使用安全的S ...

  9. MySQL mysqldump 备份脚本(按照db.sql)

    mysqldump逻辑备份,按照db.sql文件区分,并压缩 #! /bin/bash #35 02 * * * mysql /data/mysqldata/scripts/mysqldump_per ...

  10. php学习之try catch

    PHP 5 添加了类似于其它语言的异常处理模块.在 PHP 代码中所产生的异常可被 throw语句抛出并被 catch 语句捕获.(注:一定要先抛才能获取) 需要进行异常处理的代码都必须放入 try ...