[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 ...
随机推荐
- java多线程同步以及线程间通信详解&消费者生产者模式&死锁&Thread.join()(多线程编程之二)
本篇我们将讨论以下知识点: 1.线程同步问题的产生 什么是线程同步问题,我们先来看一段卖票系统的代码,然后再分析这个问题: package com.zejian.test; /** * @author ...
- 2017年10月26日 git上传文件失败的文件
最近几天因为项目要用git,于是学习了一下git.今天上传项目到码云的时候,却发现总有一些文件夹上传不上去,git 也显示everything is update.找了一圈办法,都没有用,最后突然发现 ...
- java对一个int数组进行排序、去重
思路: 1.使用 HashSet 进行去重 2.将 HashSet 变为 TreeSet 3.使用 TreeSet 进行排序 4.将 Set 变为 Integer 数组 5.将 Integer 数组变 ...
- 爬虫学习(十三)——xpath基础学习
lxml的作用 lxml是HTML.xml的解析器,主要的功能是如何解析和提取HTML和xml数据 lxml和正则一样,也是使用C来实现的,是一款高性能的python HTML/xml解析器,我们可以 ...
- Fiddler(二)
该博客基于以下博客网站里的内容进行提取,实验,和补充.让我们开始 https://www.cnblogs.com/yyhh/p/5140852.html AutoResponder 允许拦截指定规则的 ...
- python -- sftp的方式下载终端文件
可以通过paramiko模块进行远程连接,然后指定文件夹,进行下载. sf = paramiko.Transport((host, port) #创建链接对象,需要终端ip以及sftp使用的端口号 ...
- manjaro安装teamviewer实现远程连接
不要安装库里面的这两个版本,安装后桌面快捷方式和命令行运行都正常显示窗口,但没有teamviewer ID和随机密码 12.x版本也不用下载尝试了 ➜ ~ teamviewer Init...Chec ...
- 如何在CentOS 7.1中安装VMware Workstation
VMware Workstation在windows环境中大家都会安装,最近公司有个需求是需要在CentOS环境中安装VMware Workstation,上官网查了下还真有Linux版本的,于是就开 ...
- ./vi: line 2: mkdir: command not found
当前两天博主在写脚本的时候,运行脚本时候总是出现此消息,很郁闷, 开始我以为可能是我mkdir的函数库依赖的问题,但是当我用其他的脚本创建 目录的时候,命令又可以用了,找了半天,终于找到了答案 --- ...
- PHP.24-TP框架商城应用实例-后台1-添加商品功能、钩子函数、在线编辑器、过滤XSS、上传图片并生成缩略图
添加商品功能 1.创建商品控制器[C] /www.test.com/shop/Admin/Controller/GoodsController.class.php <?php namespace ...