这题做了几个小时,基本思路肯定是求两点路径中的割点数目,思路是tarjan缩点,然后以割点和连通块作为新节点见图。转化为lca求解。
结合点——双连通分量与LCA。

 /* 3686 */
#include <iostream>
#include <sstream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 typedef struct {
int u, v, f, nxt;
} edge_t; typedef struct {
int v, nxt;
} edge; const int maxv = ;
const int maxe = ;
int head[maxv], l, top;
int pre[maxv], low[maxv];
bool iscut[maxv];
int cnt[maxv], dfs_clock, bcc_cnt;
int bn[maxe];
int S[maxe];
vi bcc[maxv];
edge_t E[maxe];
int n, m; const int maxvv = maxv * ;
int mark[maxvv];
int head_[maxvv], l_;
int cutn[maxvv];
edge E_[maxe]; int deep[maxvv], beg[maxvv];
int V[maxvv<<], D[maxvv<<]; int dp[][maxvv<<]; void init_() {
memset(head_, -, sizeof(head_));
memset(mark, , sizeof(mark));
l_ = ;
} void addEdge_(int u, int v) {
E_[l_].v = v;
E_[l_].nxt = head_[u];
head_[u] = l_++; E_[l_].v = u;
E_[l_].nxt = head_[v];
head_[v] = l_++;
} void init() {
l = dfs_clock = bcc_cnt = top = ;
memset(head, -, sizeof(head));
memset(iscut, false, sizeof(iscut));
memset(cnt, , sizeof(cnt));
memset(cutn, , sizeof(cutn));
memset(pre, , sizeof(pre));
rep(i, , n+)
bcc[i].clr();
} void addEdge(int u, int v) {
E[l].u = u;
E[l].f = ;
E[l].v = v;
E[l].nxt = head[u];
head[u] = l++; E[l].u = v;
E[l].f = ;
E[l].v = u;
E[l].nxt = head[v];
head[v] = l++;
} void tarjan(int u, int fa) {
int v, k; low[u] = pre[u] = ++dfs_clock;
for (k=head[u]; k!=-; k=E[k].nxt) {
if (E[k].f)
continue;
E[k].f = E[k^].f = ;
v = E[k].v;
S[top++] = k;
if (!pre[v]) {
tarjan(v, u);
low[u] = min(low[u], low[v]);
if (low[v] >= pre[u]) {
iscut[u] = true;
++cnt[u];
bcc_cnt++;
while () {
int kk = S[--top];
bn[kk>>] = bcc_cnt;
bcc[E[kk].u].pb(bcc_cnt);
bcc[E[kk].v].pb(bcc_cnt);
if (kk == k)
break;
}
}
} else {
low[u] = min(low[u], pre[v]);
}
}
} void dfs(int u, int fa, int d) {
mark[u] = dfs_clock;
deep[u] = d;
V[++top] = u;
D[top] = d;
beg[u] = top; int v, k; for (k=head_[u]; k!=-; k=E_[k].nxt) {
v = E_[k].v;
if (v == fa)
continue;
dfs(v, u, d+);
V[++top] = u;
D[top] = d;
}
} void init_RMQ(int n) {
int i, j; for (i=; i<=n; ++i)
dp[][i] = i;
for (j=; (<<j)<=n; ++j)
for (i=; i+(<<j)-<=n; ++i)
if (D[dp[j-][i]] < D[dp[j-][i+(<<(j-))]])
dp[j][i] = dp[j-][i];
else
dp[j][i] = dp[j-][i+(<<(j-))];
} int RMQ(int l, int r) {
if (l > r)
swap(l, r); int k = ; while (<<(k+) <= r-l+)
++k; if (D[dp[k][l]] < D[dp[k][r-(<<k)+]])
return V[dp[k][l]];
else
return V[dp[k][r-(<<k)+]];
} void solve() {
int u, v, lca; rep(i, , n+) {
if (!pre[i]) {
tarjan(i, -);
if (cnt[i] <= )
iscut[i] = false;
}
} int cid = bcc_cnt; init_();
cid = bcc_cnt;
for (u=; u<=n; ++u) {
if (!iscut[u])
continue;
sort(all(bcc[u]));
cutn[++cid] = ;
addEdge_(cid, bcc[u][]);
int sz = SZ(bcc[u]);
rep(i, , sz) {
if (bcc[u][i] != bcc[u][i-]) {
addEdge_(cid, bcc[u][i]);
}
}
} top = ;
++dfs_clock;
rep(i, , cid+) {
if (mark[i] != dfs_clock)
dfs(i, , );
} init_RMQ(top); int q;
int ans; scanf("%d", &q);
while (q--) {
scanf("%d %d", &u, &v);
u = bn[u-];
v = bn[v-];
lca = RMQ(beg[u], beg[v]);
ans = (deep[u]+deep[v] - deep[lca]* + ) / ;
printf("%d\n", ans);
}
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif int u, v; while (scanf("%d %d", &n, &m)!=EOF && (n||m)) {
init();
rep(i, , m) {
scanf("%d %d", &u, &v);
addEdge(u, v);
} solve();
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}

【HDOJ】3686 Traffic Real Time Query System的更多相关文章

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

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

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

  3. hdu 3686 Traffic Real Time Query System 点双两通分量 + LCA。这题有重边!!!

    http://acm.hdu.edu.cn/showproblem.php?pid=3686 我要把这题记录下来. 一直wa. 自己生成数据都是AC的.现在还是wa.留坑. 我感觉我现在倒下去床上就能 ...

  4. HDU 3686 Traffic Real Time Query System(点双连通)

    题意 ​ 给定一张 \(n\) 个点 \(m\) 条边的无向图,\(q\) 次询问,每次询问两边之间的必经之点个数. 思路 ​ 求两点之间必经之边的个数用的是边双缩点,再求树上距离.而对比边双和点双之 ...

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

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

  6. 【翻译】Sencha Touch2.4 The Layout System 布局

    [翻译]The Layout System 布局 In Sencha Touch there are two basic building blocks: componentsand containe ...

  7. 【DataStructure】One of queue usage: Simulation System

    Statements: This blog was written by me, but most of content  is quoted from book[Data Structure wit ...

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

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

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

随机推荐

  1. style、currentStyle、getComputedStyle区别介绍

    style.currentStyle.getComputedStyle区别介绍 来自:蓝色天空 样式表有三种方式 内嵌样式(inline Style) :是写在Tag里面的,内嵌样式只对所有的Tag有 ...

  2. winForm 打印预览

    自己很少写技术博客,虽然已经干程序员两年多了,winform开发,web开发都干过,不论项目大小对于.net的相关技术也是了解的,如mvc,wcf,wpf,silverlight,socekt通讯,n ...

  3. ACM俱乐部 字符串

    数制转换分数: 2 时间限制:1 秒 内存限制:32 兆 特殊判题: 否 提交:59 解决: 24 标签 进制转换 题目描述 求任意两个不同进制非负整数的转换(2进制-16进制),所给整数在long所 ...

  4. svn团队环境

    1.安装VisualSVN Server VisualSVN-Server-3.3.1-x64.msi 下载,并安装标准版(免费) 2.下载TortoiseSVN TortoiseSVN-1.8.11 ...

  5. seo 优化 仅针对 来拍呀www.laipaiya.com(一)

    加入百度统计代码:http://tongji.baidu.com/ 查看百度统计优化分析->seo建议 对每个页面的meta标签做修改 首页 title :来拍呀 - | 折扣好房你就来拍呀 k ...

  6. "!x++" 我之见解

    "!x++"之说,各人见解不同,但真理只有一个.我只尝试着说出一种见解,未知真相. 何如? "!x++"等价于"!(x++)". 理论分析 ...

  7. Linux内核树的建立-基于ubuntu系统

    刚看 O'REILLY 写的<LINUX 设备驱动程序>时.作者一再强调在编写驱动程序时必须 建立内核树.先前的内核只需要有一套内核头文件就够了,但因为2.6的内核模块吆喝内核源码树中的目 ...

  8. go语言实现线程池

    话说真的好久没有写博客了,最近赶新项目,工作太忙了.这一周任务比较少,又可以随便敲敲了. 逛论坛的时候突发奇想,想用go语言实现一个线程池,主要功能是:添加total个任务到线程池中,线程池开启num ...

  9. mybatis generator自动生成 实体类, sqlmap配置文件 详细介绍

    我使用的是Eclipse Luna 装了自己常用的插件, generator也是其中一个推荐下载 MyBatis_Generator_1.3.1.zip离线安装包 <?xml version=& ...

  10. C# - 高级方法参数

    可选参数 -必须有个默认值,默认值必须是字面值,常量值,新对象实例或者默认值类型值. public List<string> GetWords( string sentence, bool ...