这题做了几个小时,基本思路肯定是求两点路径中的割点数目,思路是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. IOS 四种保存数据的方式

    在iOS开发过程中,不管是做什么应用,都会碰到数据保存的问题.将数据保存到本地,能够让程序的运行更加流畅,不会出现让人厌恶的菊花形状,使得用户体验更好.下面介绍一下数据保存的方式: 1.NSKeyed ...

  2. 为什么Facebook要将视频从Flash全面迁移到HTML5?

    英文原文:Why we chose to move to HTML5 video 编者按:Facebook 前端高级工程师 Daniel Baulig 解释了 Facebook 为什么要将视频全面迁移 ...

  3. C# 学习之旅(1)

    第一, 输入输出流都来自控制台. using System; using System.Collections.Generic; using System.Linq; using System.Tex ...

  4. 使用rar打包多个文件为exe可执行文件

    需求分析:有些机友在刷recovery的时候不知道如何刷入,于是产生写bat脚本和打包为exe可执行文件,只要机友正确安装好驱动后连接手机双击就可以刷入rec了 解决过程: 需要打包的文件 操作过程截 ...

  5. 在win7上建立本地FTP站点详细步骤

    一.安装FTP组件点击:控制面板—>程序和功能—>打开或关闭Windows功能. 勾选“FTP服务器”及“FTP服务”“FTP扩展性”,点击“确定”,安装FTP组件. 勾选Web管理工具的 ...

  6. 回顾Ado.Net

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Da ...

  7. Telerik 控件事例(鼠标拖动行,拖动列,设置行对齐,行宽,是否显示)

    People.cs using System;using System.Collections.Generic;using System.Data;using System.Linq;using Sy ...

  8. 原生JS的对象常用操作总结

       前端时间写了篇怎么判断js对象相等的文章,一直在期待大神给点消息,无奈一直杳无音讯,还是自己写个函数来进行判断,下面总结一些常用的对象操作的方法.    咋们来个先抑后扬的方式,先放出几个基本的 ...

  9. 30分钟让你了解MongoDB基本操作

    今天记录下MongoDB的基本操作,这只是最基本的,所以是应该掌握的. 数据库 数据库是一个物理容器集合.每个数据库都有自己的一套文件系统上的文件.一个单一的MongoDB服务器通常有多个数据库. 集 ...

  10. mac下设置maven环境

    在mac系统下设置maven环境. 1.首先通过终端打开    .bash_profile 2.设置maven解压后的路径地址 环境变量设置如下: MAVEN_HOME .PATH 两个变量即可 3. ...