题目大意:给一张$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的更多相关文章

  1. Codeforces 1045C Hyperspace Highways (看题解) 圆方树

    学了一下圆方树, 好神奇的东西呀. #include<bits/stdc++.h> #define LL long long #define fi first #define se sec ...

  2. codeforce1046 Bubble Cup 11 - Finals 题解

    比赛的时候开G开了3h结果rose说一句那唯一一个AC的是羊的心态就崩了.. 这套题感觉质量挺好然后就back了下 A: AI robots 有三个限制条件:相互能够看见和智商的差.使用主席树,可以维 ...

  3. Bubble Cup 11 - Finals [Online Mirror, Div. 1]题解 【待补】

    Bubble Cup 11 - Finals [Online Mirror, Div. 1] 一场很好玩的题啊! I. Palindrome Pairs 枚举哪种字符出现奇数次. G. AI robo ...

  4. H:Highways

    总时间限制: 1000ms 内存限制: 65536kB描述The island nation of Flatopia is perfectly flat. Unfortunately, Flatopi ...

  5. Highways(prim & MST)

    Highways Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 23421   Accepted: 10826 Descri ...

  6. poj2485 Highways

    Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public h ...

  7. hdu 4666:Hyperspace(最远曼哈顿距离 + STL使用)

    Hyperspace Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Tota ...

  8. poj 2485 Highways 最小生成树

    点击打开链接 Highways Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19004   Accepted: 8815 ...

  9. poj 2485 Highways

    题目连接 http://poj.org/problem?id=2485 Highways Description The island nation of Flatopia is perfectly ...

随机推荐

  1. C#后台动态添加Grid表格

    前面页面: <ScrollViewer x:Name=" BorderBrush="#25A0DA" VerticalScrollBarVisibility=&qu ...

  2. 基于 win7下虚拟机的 GNSS-SDR安装过程

    最近在安装 GNSS-SDR软件时,遇到了很多问题,这里回顾了我的安装过程,罗列了所遇到的问题和解决办法.希望后来者不要再踩这些坑了! 首先,在官方文档中看到,GNSS-SDR目前并不支持直接在 Wi ...

  3. 有一段<script>代码,效果是点击<p>就会弹出信息,但是有的<p>点击会有效果,有的没有效果

    问题:有一段<script>代码,效果是点击<p>就会弹出信息,但是有的<p>点击会有效果,有的没有效果 解决: 页面代码是至上而下执行的,如果你的这个标签在< ...

  4. rsync同步备份搭建

    Rsync 是 Unix/Linux 下的一款应用软 在平常的运维中进常要对一些数据进行备份,以防止意外的服务器故障导致不可避免的后果,tar,cp只能适应一些小范围backup,对于几T甚至几P的数 ...

  5. 事件监听和window.history以及自定义创建事件

    1.事件监听window.addEventListener方法: Window.addEventListener(event, function, useCapture); useCapture:表示 ...

  6. 用python写一个类似于linux中的tree

    import os filePath = 'g:/File' j = 0 # 查找的深度计数 def tree(filePath,j): dir_now = os.listdir(filePath) ...

  7. Allowed memory size of 134217728 bytes exhausted (tried to allocate 2 bytes)

    出现  Allowed memory size of 134217728 bytes exhausted (tried to allocate 2 bytes)时在php.ini文件中配置 memor ...

  8. 【PHP项目】伪静态规则

    伪静态规则写法RewriteRule-htaccess详细语法使用 2016年03月30日 16:53:59 阅读数:20340 伪静态实际上是利用php把当前地址解析成另一种方法来访问网站,要学伪静 ...

  9. TP3.2.3 页面跳转后 Cookie 失效 —— 参考解决方案

    一.问题描述 接手一个项目,使用ThinkPhp3.2.3,在线上环境( Centos7.4 + Nginx1.14 + MySQL5.7 + PHP7.2.4 )运行没有问题, 在本地环境( php ...

  10. vue 改变我们插值的符号{{}}改为${}

    delimiters的作用是改变我们插值的符号.Vue默认的插值是双大括号{{}}.但有时我们会有需求更改这个插值的形式. delimiters:['${','}'] 现在我们的插值形式就变成了${} ...