CodeForces 97 E. Leaders(点双连通分量 + 倍增)
题意
给你一个有 \(n\) 个点 \(m\) 条边的无向图,有 \(q\) 次询问,每次询问两个点 \(u, v\) 之间是否存在长度为奇数的简单路径。
\(1 \le n, m, q \le 10^5\)
题解
显然我们可以对于每个联通块单独处理,如果 \(u, v\) 不联通显然就不存在这条路。
然后对于每个联通块,首先随便弄一颗生成树。
- 如果这 \(u \to v\) 在树上的路径长就为奇数,显然是可以的,这个可以预处理深度就行了。
- 否则,\(u \to v\) 在树上的路径的边,只要存在一条边属于一个奇环就行了。
然后我们只要求出每条边是否属于一个奇环就行了。
单是求环的话,我们显然可以用求点双联通分量的方法来求的,求奇环需要用到下面的一个性质。
一个点双连通分量中要么每条边都在至少一个奇环上, 要么没有奇环.
这个证明是很显然的,可以自己手动画图理解。这个性质可以记住,到时候或许有用。
所以如果点双中存在一条边属于奇环,那么所有边都是在奇环上。
判断奇环可以直接判这个点连的两条边奇偶性是否相同,如果相同那么就是奇环了。
然后我们需要查询两个点的路径上是否存在一条边属于奇环,这个可以直接用树上差分做。
然后对于之前记一条边是否在奇环上,需要把这个点双中除了点双中最上面的那个点 \(u\) 全部标成 \(1\) 。
也就是我们都标到儿子上就行了。
实现起来就是 (cnt[u] + cnt[v] - 2 * cnt[Get_Lca(u, v)]) > 0
,cnt
为到根路径上标号的前缀和,用倍增求 Lca
就行了。
总结
多找性质,多思考,多记性质。
代码
具体实现看代码就行啦qwq 注意此处点双要存边才行,还要注意一些小细节。
#include <bits/stdc++.h>
#define For(i, l, r) for(register int i = (l), i##end = (int)(r); i <= i##end; ++i)
#define Fordown(i, r, l) for(register int i = (r), i##end = (int)(l); i >= i##end; --i)
#define Set(a, v) memset(a, v, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define debug(x) cout << #x << ": " << x << endl
#define DEBUG(...) fprintf(stderr, __VA_ARGS__)
#define fir first
#define sec second
#define mp make_pair
using namespace std;
typedef pair<int, int> PII;
inline bool chkmin(int &a, int b) {return b < a ? a = b, 1 : 0;}
inline bool chkmax(int &a, int b) {return b > a ? a = b, 1 : 0;}
inline int read() {
int x = 0, fh = 1; char ch = getchar();
for (; !isdigit(ch); ch = getchar()) if (ch == '-') fh = -1;
for (; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + (ch ^ 48);
return x * fh;
}
void File() {
#ifdef zjp_shadow
freopen ("E.in", "r", stdin);
freopen ("E.out", "w", stdout);
#endif
}
const int N = 1e5 + 1e3, M = N << 1;
int n, m, Logn;
int anc[N][20], dep[N];
vector<int> G[N]; bitset<N> vis1, vis2; int id[N], version;
void Dfs_Init(int u, int fa = 0) {
id[u] = version; vis1[u] = true; dep[u] = dep[anc[u][0] = fa] + 1;
For (i, 1, Logn) anc[u][i] = anc[anc[u][i - 1]][i - 1];
for (int v : G[u]) if (!vis1[v]) Dfs_Init(v, u);
}
int dfn[N], lowlink[N], cnt[N];
PII sta[M]; int top = 0;
void Tarjan(int u, int fa = 0) {
static int clk = 0;
dfn[u] = lowlink[u] = ++ clk;
for (int v : G[u]) if (v != fa && dfn[v] < dfn[u]) {
sta[++ top] = mp(u, v);
if (!dfn[v]) {
Tarjan(v, u);
chkmin(lowlink[u], lowlink[v]);
if (lowlink[v] >= dfn[u]) {
int tmp = top, x, y, flag = 0;
do {
x = sta[top].fir; y = sta[top --].sec;
if ((dep[x] & 1) == (dep[y] & 1)) { flag = 1; break; }
} while (!(x == u && y == v));
if (!flag) continue ;
top = tmp;
do {
x = sta[top].fir; y = sta[top --].sec;
cnt[x] = cnt[y] = 1;
} while (!(x == u && y == v));
cnt[u] = 0;
}
} else chkmin(lowlink[u], dfn[v]);
}
}
void Dfs_Calc(int u, int fa = 0) {
cnt[u] += cnt[fa]; vis2[u] = true;
for (int v : G[u]) if (!vis2[v]) Dfs_Calc(v, u);
}
inline int Get_Lca(int x, int y) {
if (dep[x] < dep[y]) swap(x, y);
Fordown (i, Logn, 0)
if (dep[anc[x][i]] >= dep[y]) x = anc[x][i];
if (x == y) return x;
Fordown (i, Logn, 0)
if (anc[x][i] != anc[y][i]) x = anc[x][i], y = anc[y][i];
return anc[x][0];
}
inline bool Check(int u, int v) {
if (id[u] != id[v]) return false;
if ((dep[u] & 1) ^ (dep[v] & 1)) return true;
return (cnt[u] + cnt[v] - 2 * cnt[Get_Lca(u, v)]) > 0;
}
int main () {
File();
n = read(); m = read();
Logn = ceil(log2(n));
For (i, 1, m) {
int u = read(), v = read();
G[u].push_back(v); G[v].push_back(u);
}
For (i, 1, n) if (!dfn[i]) ++ version, Dfs_Init(i), Tarjan(i), Dfs_Calc(i);
int q = read();
For (i, 1, q) {
int u = read(), v = read();
puts(Check(u, v) ? "Yes" : "No");
}
return 0;
}
CodeForces 97 E. Leaders(点双连通分量 + 倍增)的更多相关文章
- [Codeforces 555E]Case of Computer Network(Tarjan求边-双连通分量+树上差分)
[Codeforces 555E]Case of Computer Network(Tarjan求边-双连通分量+树上差分) 题面 给出一个无向图,以及q条有向路径.问是否存在一种给边定向的方案,使得 ...
- codeforces 962F.simple cycle(tarjan/点双连通分量)
题目连接:http://codeforces.com/contest/962/problem/F 题目大意是定义一个simple cycle为从一个节点开始绕环走一遍能经过simple cycle内任 ...
- Codeforces 521E - Cycling City(点双连通分量+分类讨论)
Codeforces 题面传送门 & 洛谷题面传送门 大家都是暴力找生成树然后跳路径,代码不到 50 行(暴论)的一说--好,那本蒟蒻决定提供一种代码 150 行,但复杂度也是线性的分类讨论做 ...
- Simple Cycles Edges CodeForces - 962F(点双连通分量)
题意: 求出简单环的所有边,简单环即为边在一个环内 解析: 求出点双连通分量,如果一个连通分量的点数和边数相等,则为一个简单环 点双连通分量 任意两个点都至少存在两条点不重复的路径 即任意两条边都 ...
- HDU 5458 Stability(双连通分量+LCA+并查集+树状数组)(2015 ACM/ICPC Asia Regional Shenyang Online)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5458 Problem Description Given an undirected connecte ...
- POJ2942 Knights of the Round Table[点双连通分量|二分图染色|补图]
Knights of the Round Table Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 12439 Acce ...
- 【Codefoces487E/UOJ#30】Tourists Tarjan 点双连通分量 + 树链剖分
E. Tourists time limit per test: 2 seconds memory limit per test: 256 megabytes input: standard inpu ...
- 【BZOJ-2730】矿场搭建 Tarjan 双连通分量
2730: [HNOI2012]矿场搭建 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1602 Solved: 751[Submit][Statu ...
- hihoCoder 1184 连通性二·边的双连通分量
#1184 : 连通性二·边的双连通分量 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在基本的网络搭建完成后,学校为了方便管理还需要对所有的服务器进行编组,网络所的老 ...
随机推荐
- Glad to see you! CodeForces - 810D (交互+二分)
This is an interactive problem. In the output section below you will see the information about flush ...
- 用友云开放平台之API网关
本文介绍选择API网关应考虑的几方面内容,API网关在微服务框架中的作用,API网关如何选型,用友云开放平台的API网关可以做什么. 随着互联网的快速发展,当前已步入移动互联.物联网时代.企业内部系统 ...
- 【Python3练习题 002】企业发放的奖金根据利润提成
# [Python练习题 002]企业发放的奖金根据利润提成.# 利润(I)低于或等于10万元时,奖金可提10%:利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分, ...
- react双组件传值和传参
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- React Native之微信分享(iOS Android)
React Native之微信分享(iOS Android) 在使用React Native开发项目的时候,基本都会使用到微信好友或者微信朋友圈分享功能吧,那么今天我就带大家实现以下RN微信好友以及朋 ...
- AngularJS基于MVC的复杂操作案例
AngularJS基于MVC的复杂操作案例 <!DOCTYPE html> <html> <head> <meta charset="UTF-8&q ...
- 对数log
a的x次方等于N(a>0,且a不等于1),那么数x叫做以a为底N的对数(logarithm),记作x=logaN.其中,a叫做对数的底数,N叫做真数.
- Django--权限信息操作
一 . 权限控制 表结构的设计 rbca(Role Based Access Control) 基于角色的权限控制 3个model 5张表 class User(models.Model): # ...
- Visual Studio2012调试时无法命中断点
今天在调试代码的时候发现在Debug模式下无法命中断点,然后一步步去检查原因,最后发现是在项目-->属性-->生成-->高级-->调试信息被设置为None,然后在选项中将其选择 ...
- 使用composer安装php的相关框架
使用composer来安装php的相关框架,不需要事先准备composer.json以及conmposer.lock以及composer.phar等文件: 直接在项目根目录下是使用composer r ...