【HDOJ】3686 Traffic Real Time Query System
这题做了几个小时,基本思路肯定是求两点路径中的割点数目,思路是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的更多相关文章
- 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 ...
- hdu 3686 Traffic Real Time Query System 点双两通分量 + LCA。这题有重边!!!
http://acm.hdu.edu.cn/showproblem.php?pid=3686 我要把这题记录下来. 一直wa. 自己生成数据都是AC的.现在还是wa.留坑. 我感觉我现在倒下去床上就能 ...
- HDU 3686 Traffic Real Time Query System(点双连通)
题意 给定一张 \(n\) 个点 \(m\) 条边的无向图,\(q\) 次询问,每次询问两边之间的必经之点个数. 思路 求两点之间必经之边的个数用的是边双缩点,再求树上距离.而对比边双和点双之 ...
- CH#24C 逃不掉的路 和 HDU3686 Traffic Real Time Query System
逃不掉的路 CH Round #24 - 三体杯 Round #1 题目描述 现代社会,路是必不可少的.任意两个城镇都有路相连,而且往往不止一条.但有些路连年被各种XXOO,走着很不爽.按理说条条大路 ...
- 【翻译】Sencha Touch2.4 The Layout System 布局
[翻译]The Layout System 布局 In Sencha Touch there are two basic building blocks: componentsand containe ...
- 【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 ...
- 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 ...
- Traffic Real Time Query System 圆方树+LCA
题目描述 City C is really a nightmare of all drivers for its traffic jams. To solve the traffic problem, ...
随机推荐
- 将XML文件保存到DataGridView中
#region get护理单记录信息XML //将XML文件保存到DataTable private DataTable FromXML2DataTable(string XMLStr,string ...
- [OpenXml] Read/Write row/cell from excel
public static void test(){ using (SpreadsheetDocument document = SpreadsheetDocument.Open("test ...
- 类似FirePhp的Chrome.php 调试php
之前一直用firephp来调试php,主要受限Firefox启动太慢,研究了下chromephp; 写了个简单的判断模版: <?php /** * @Author: Klaus * @Date: ...
- java-----基本数据类型包装类
目的:为了方便操作基本数据类型值,将其封装为对象,在对象定义了属性和行为,丰富了改数据的操作,用于描述该对象的类也就成为基本数据类型对象包装类. 例如:int类型的取值范围:Integer------ ...
- fineui框架
http://fineui.com/demo/#/demo/layout/fit.aspx 虽然比较丑陋,但功能实用 此框架比较简单, 框架的作用你懂的,重点是要有帮助文档, 进阶型的容易上手的帮助文 ...
- ExtJS 添加图标icon
extjs控件有两个属性:一个是iconCls:另一个是icon.通过这两个属性可以对控件添加图标 1.直接引用图标路径 icon: '../icons/application_view_detail ...
- 【学习总结】Info.plist和pch文件的作用
Info.plist 建立一个工程后,会在Supporting files文件夹下看到一个“Info.plist”的文件,该文件对工程做一些运行期的配置,非常重要,不能删除 项目中其他Plis ...
- 第三方登录过程—OAuth2.0协议
---恢复内容开始--- 理清思路 1.在第三方注册成为开发者,拿到第三方给的client_id(app_id---就像你的身份证.QQ号)和client_secret(就像你的QQ密码): 2.填写 ...
- 纯CSS制作二级导航
一.问题描述 做一个类似校园网首页,主要是导航栏的设置,ul默认纵向排列,如何横向排列,同时去掉圆点. 二.问题解决 2.1 先写导航条 用两个ul嵌套,一个ul是横向导航条,另一个是每个小项目下连一 ...
- [DP] 堆盒子问题
给一堆盒子,知道每个盒子的三围(长宽高),盒子正面朝你,不能旋转摆放,按照大的放在小的下面的原则堆起来,必须是 strictly larger,同样大小的盒子不行,问怎么样堆到最大的高度? 思路:动态 ...