这题做了几个小时,基本思路肯定是求两点路径中的割点数目,思路是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. C#利用开源软件ffMpeg截取视频图片

    #region 从视频画面中截取一帧画面为图片 /// <summary> /// 从视频画面中截取一帧画面为图片 /// </summary> /// <param n ...

  2. Android Activity 注意笔记

    图1:Activity生命周期的简化图,就像一个阶梯金字塔.这图像表明每个状态中是怎么样使用回调函数使得恢复状态回到顶端,或者降低状态到达底部.Activity可以从Paused状态和stopped状 ...

  3. CLR via C# 异常管理读书笔记

    1. 设计异常类型层次结构应该浅而宽 2. 注意使用finally块清理资源 3. 不要什么都捕捉 4.得体地从异常中恢复 5.发生不可恢复的异常时回滚部分完成的操作-维持状态 6.隐藏实现细节来维系 ...

  4. 字符串做异或使用union

    #include <stdio.h> #include <sys/time.h> #include <string.h> union data { unsigned ...

  5. UVA 524

    Description   A ring is composed of n (even number) circles as shown in diagram. Put natural numbers ...

  6. 二、记一次失败的 CAS 搭建 之 证书配置

    ==================================================================================================== ...

  7. having与where区别

    having后可以跟组函数如avg(sal)而where后不可以有, 如果条件不是必须使用组函数最好还是使用where

  8. WebView重定向新开界面问题-b

    首先介绍下这个问题,iOS上WebView 如果想更贴近native,就要加载新URL的时候新开个界面,但是如果加载的链接有重定向的话,就会在中间开一个空白的界面,这个好烦.然后就是解决这个问题,采用 ...

  9. python字符串内容替换的方法(转载)

    python字符串内容替换的方法 时间:2016-03-10 06:30:46来源:网络 导读:python字符串内容替换的方法,包括单个字符替换,使用re正则匹配进行字符串模式查找与替换的方法.   ...

  10. Delphi XE5 android 黑屏的临时解决办法

            下载style 然后在deployment里添加进去 http://files.cnblogs.com/nywh2008/styles.rar 在AndroidManifest.tem ...