题目大意:给一张$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. jquery ajax参数

    //默认请求参数 var _options = { url: null, // 请求连接地址 type: 'GET', // 请求类型(get,post) data: null, // post时请求 ...

  2. VS2013使用自带的数据库 Microsoft SQL Server 2012 Express LocalDB

    注:DeptLocalDB:自己取的数据库实例名称 DeptSharedLocalDB:自己取的实例共享名称np:\\.\pipe\LOCALDB#SH7C6ED5\tsql\query:命名管道名称 ...

  3. 【CodeBase】PHP将数组键名转成变量名

    <?php /** * php 把数组中的键名所为变量名键值作为变量名 */ $arr=array('a'=>1,'b'=>2,'c'=>3,'d'=>5,'e'=> ...

  4. 【Effective C++ 读书笔记】条款03: 尽量使用 const

    关键字const多才多艺,变化多端却不高深莫测. const 修饰指针 面对指针, 你可以指出 指针自身.指针所指物.或者两者都不是 const. 如果关键字 const 出现在星号左边,表示被指物是 ...

  5. iOS-xib的使用1

    一.File‘s owner的解析过程和使用: 1. storyboard:描述软件界面:iOS5.0后出来的. xib:描述软件界面:是storyboard前身. 2. 项目环境里面的所有资源都要通 ...

  6. poj 2965 枚举+DFS

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 25343 ...

  7. JAVA运行机制

    这一篇我们来简单理解一下JAVA的运行机制 大概可以分为三大部分 1.编写程序 2.编译程序 3.运行程序 1.编写程序 编写程序就是我们前面说的源代码 这些源代码都有特殊的语法 例如main函数 他 ...

  8. 笔记-爬虫-selenium常用方法

    笔记-爬虫-selenium常用方法 1.      查找元素 常用的查找方法 find_element_by_name find_element_by_xpath find_element_by_l ...

  9. PHP.18-图片等比例缩放

    图片等比例缩放 自定义函数ImageUpdateSize($pricname, $maxx, $maxy, $pre) 1.$pricname:被缩放的图片源(路径):2.$maxx,$maxy:缩放 ...

  10. AD高级规则设置

    inpolygon 是所有的覆铜 ispad 是焊盘到焊盘的间距 IsVia 过孔间距 ispad and InComponent('S1')    设置某个器件的焊盘间距规则 ispad and H ...