[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 ...
随机推荐
- v-for的显示过滤/排序结果
对于v-for列表渲染指令,项目中很常用的额,但是我们一般可能在从后端接口拿到数据的时候就把数据通过循环整理改造成自己想要的样子了.有时候可能对于不同的列表需求,还要在data里多造一份数据. 这种做 ...
- js实现二分查找
二分查找需要数组是有序的,1.先从有序数组的最中间元素开始查找,如果和要查找的元素相等,直接返回索引,若不相等则下一步.2.如果指定的元素大于或者小于中间元素,则在大于或小于的那一半区域内查找,重复第 ...
- maven入门2
1.修改maven本地仓库位置 没有效果,在新建项目时还是转跳到默认配置 在默认仓库位置添加修改后的setting文件,失败 修改成功,原因是前面修改的是setting而我们需要修改的是default ...
- hibernate系列之四
数据库中表之间的关系: 一对一.一对多.多对多 一对多的建表原则:在多的一方创建外键指向一的一方的主键: 多对多的建表原则:创建一个中间表,中间表中至少有两个字段作为外键分别指向多对多双方的主键: 一 ...
- 详解MessageBox(),MsgBox函数的正确使用
//或者使用chr(13),chr(10)效果一样 MsgBox "a"&chr(13)&"b"&chr(10)&"c ...
- SQl 语句(常见) 新建,删除,修改表结构
2006-6-15 15:58:25 新建表:create table [表名]([自动编号字段] int IDENTITY (1,1) PRIMARY KEY ,[字段1] nVarChar(50) ...
- spring-mvc.xml的定时器配置
<!-- 设置时间 --> <bean id="myJobTrigger" class="org.springframework.scheduling. ...
- C++基础 匿名对象
以下几种情况又会匿名对象 (1)对象构造 与 匿名对象 Test t1 = Test(); 这时,Test()会构造匿名对象,并且是调用无参构造函数,然后 t1 将匿名对象扶正. (2)对象赋值 与 ...
- SpringMVC文件上传——bean的配置【org.springframework.web.multipart.commons.CommonsMultipartResolver】
一.简介 Spring MVC支持一个通用的多路上传解析器CommonsMultipartResolver,在Spring的配置文件中对CommonsMultipartResolver Bean进行配 ...
- TerminateProcess
Remarks The TerminateProcess function is used to unconditionally cause a process to exit. The state ...