#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_pairusingnamespace std;
typedef pair<int, int> PII;
inlineboolchkmin(int &a, int b){return b < a ? a = b, 1 : 0;}
inlineboolchkmax(int &a, int b){return b > a ? a = b, 1 : 0;}
inlineintread(){
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;
}
voidFile(){
#ifdef zjp_shadowfreopen ("E.in", "r", stdin);
freopen ("E.out", "w", stdout);
#endif}
constint 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;
voidDfs_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;
voidTarjan(int u, int fa = 0){
staticint 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;
}
} elsechkmin(lowlink[u], dfn[v]);
}
}
voidDfs_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);
}
inlineintGet_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];
}
inlineboolCheck(int u, int v){
if (id[u] != id[v]) returnfalse;
if ((dep[u] & 1) ^ (dep[v] & 1)) returntrue;
return (cnt[u] + cnt[v] - 2 * cnt[Get_Lca(u, v)]) > 0;
}
intmain(){
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");
}
return0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】