[CF1045C]Hyperspace Highways
题目大意:给一张$n$个点$m$条边的图,保证若有一个环,一定是完全子图,多次询问两个点之间的最短路径长度
题解:把完全子图缩成一个点,圆方树,方点权值设成$1$,圆点设成$0$即可。
卡点:数组开小
C++ Code:
#include <cstdio>
#include <algorithm>
#define maxn 100010
#define maxm 500010
inline int min(int a, int b) {return a < b ? a : b;}
inline int max(int a, int b) {return a > b ? a : b;} namespace Tree {
int head[maxn << 1], cnt;
struct Edge {
int to, nxt;
} e[maxn << 2];
inline void addE(int a, int b) {
e[++cnt] = (Edge) {b, head[a]}; head[a] = cnt;
e[++cnt] = (Edge) {a, head[b]}; head[b] = cnt;
} #define M 19
int nodenum, n;
int dep[maxn << 1], sum[maxn << 1], fa[M][maxn << 1];
void dfs(int u) {
for (int i = 1; i < M; i++) fa[i][u] = fa[i - 1][fa[i - 1][u]];
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].to;
if (v != fa[0][u]) {
fa[0][v] = u; dep[v] = dep[u] + 1;
sum[v] = sum[u] + (v > n);
dfs(v);
}
}
}
inline int LCA(int x, int y) {
if (x == y) return x;
if (dep[x] < dep[y]) std::swap(x, y);
for (int i = dep[x] - dep[y]; i; i &= i - 1) x = fa[__builtin_ctz(i)][x];
if (x == y) return x;
for (int i = M - 1; ~i; i--) if (fa[i][x] != fa[i][y]) x = fa[i][x], y = fa[i][y];
return fa[0][x];
}
inline int ask(int x, int y) {
int lca = LCA(x, y);
return sum[x] + sum[y] - (sum[lca] << 1) + (lca > n);
}
void init(int __n) {
n = __n;
sum[1] = 0;
dfs(1);
}
#undef M
} namespace Graph {
int head[maxn], cnt;
struct Edge {
int to, nxt;
} e[maxm << 1];
inline void addE(int a, int b) {
e[++cnt] = (Edge) {b, head[a]}; head[a] = cnt;
e[++cnt] = (Edge) {a, head[b]}; head[b] = cnt;
} using Tree::nodenum;
int DFN[maxn], low[maxn], idx;
int S[maxn], top;
void tarjan(int u) {
DFN[u] = low[u] = ++idx;
S[++top] = u;
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].to;
if (!DFN[v]) {
tarjan(v);
low[u] = min(low[u], low[v]);
if (low[v] >= DFN[u]) {
nodenum++;
Tree::addE(nodenum, u);
do {
v = S[top--];
Tree::addE(nodenum, v);
} while (v != e[i].to);
}
} else low[u] = min(low[u], DFN[v]);
}
}
void init(int n) {
tarjan(1);
Tree::init(n);
}
} int n, m, Q;
int main() {
scanf("%d%d%d", &n, &m, &Q); Tree::nodenum = Tree::n = n;
for (int i = 0, a, b; i < m; i++) {
scanf("%d%d", &a, &b);
Graph::addE(a, b);
}
Graph::init(n);
while (Q --> 0) {
int u, v;
scanf("%d%d", &u, &v);
printf("%d\n", Tree::ask(u, v));
}
return 0;
}
[CF1045C]Hyperspace Highways的更多相关文章
- Codeforces 1045C Hyperspace Highways (看题解) 圆方树
学了一下圆方树, 好神奇的东西呀. #include<bits/stdc++.h> #define LL long long #define fi first #define se sec ...
- codeforce1046 Bubble Cup 11 - Finals 题解
比赛的时候开G开了3h结果rose说一句那唯一一个AC的是羊的心态就崩了.. 这套题感觉质量挺好然后就back了下 A: AI robots 有三个限制条件:相互能够看见和智商的差.使用主席树,可以维 ...
- Bubble Cup 11 - Finals [Online Mirror, Div. 1]题解 【待补】
Bubble Cup 11 - Finals [Online Mirror, Div. 1] 一场很好玩的题啊! I. Palindrome Pairs 枚举哪种字符出现奇数次. G. AI robo ...
- H:Highways
总时间限制: 1000ms 内存限制: 65536kB描述The island nation of Flatopia is perfectly flat. Unfortunately, Flatopi ...
- Highways(prim & MST)
Highways Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 23421 Accepted: 10826 Descri ...
- poj2485 Highways
Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public h ...
- hdu 4666:Hyperspace(最远曼哈顿距离 + STL使用)
Hyperspace Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Tota ...
- poj 2485 Highways 最小生成树
点击打开链接 Highways Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19004 Accepted: 8815 ...
- poj 2485 Highways
题目连接 http://poj.org/problem?id=2485 Highways Description The island nation of Flatopia is perfectly ...
随机推荐
- jquery 操作ajax 相关方法
jQuery.get() 使用一个HTTP GET 请求从服务器加载数据. jQuery.get(url [,data] [,success(data,textStatus,jqXHR)] [dtaT ...
- 自定义扩展Compare比较方法
public static int Compare<T, V>(this T x, T y, Func<T, V> func) { return Comparer<V&g ...
- html基础之遗忘篇
a链接: ①a的href指向压缩文件可以下载压缩文件. ②a链接的打开方式可以在head内使用<base target="_blank">来整体控制打开方式. 字符实体 ...
- Linux相关常用命令
1.XShell中上传文件命令 首先需要安装rz文件上传工具: yum -y install lrzsz 然后执行以下命令,可打开本地系统的选择文件窗口:(或者直接把本地的文件拖动到SSH Shell ...
- Ball CodeForces - 12D
传送门 N ladies attend the ball in the King's palace. Every lady can be described with three values: be ...
- python——PIL(图像处理库)
PIL(Python Imaging Library,python图像处理库)提供了通用的图像处理功能,以及大量有用的基本图像操作,如图像缩放,裁剪,旋转,颜色转换等. 1.打开图像并显示 from ...
- 笔记-scrapy-去重
笔记-scrapy-去重 1. scrapy 去重 scrapy 版本:1.5.0 第一步是要找到去重的代码,scrapy在请求入列前去重,具体源码在scheduler.py: def en ...
- OpenCV学习笔记(十一) 轮廓操作
在图像中寻找轮廓 首先利用Canny算子检测图像的边缘,再利用Canny算子的输出作为 寻找轮廓函数 findContours 的输入.最后用函数 drawContours 画出轮廓.边界Counto ...
- VS Extension+NVelocity系列(二)——让VS支持 NVelocity的智能提示(上)
一.基础概念 应该庆幸的是,VS的插件是靠着MEF实现而不是MAF,这让你所做的工作减轻了许多.如果在这之前,您已经了解了MEF的原理,我想对于VS插件的编写,您应该是很容易就能理解的.看看几个VS2 ...
- MySQL高可用之MHA安装
Preface MasterHA is a tool which can be used in MySQL HA architecture.I'm gonna implement it ...