Search is not available for this dataset
name
stringlengths 2
112
| description
stringlengths 29
13k
| source
int64 1
7
| difficulty
int64 0
25
| solution
stringlengths 7
983k
| language
stringclasses 4
values |
---|---|---|---|---|---|
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int N, M, Q, CN;
int a, b;
int uf[100005];
int find(int x) { return x == uf[x] ? x : uf[x] = find(uf[x]); }
int dep[100005];
int dad[100005];
int dis[100005];
int low[100005];
int cut[100005];
int com[100005];
int now;
int A[100005];
int B[100005];
int top;
vector<int> G[100005];
int tdep[2 * 100005];
int tdad[2 * 100005];
int tbio[2 * 100005];
int link[2 * 100005][19];
int comp[2 * 100005][19];
int good[2 * 100005];
vector<int> T[2 * 100005];
vector<int> g[100005];
vector<pair<int, int> > E;
queue<int> q;
int color[100005];
bool bicolorable() {
for (int i = 0; i < E.size(); ++i) {
g[E[i].first].clear();
color[E[i].first] = 0;
g[E[i].second].clear();
color[E[i].second] = 0;
}
while (!q.empty()) q.pop();
int source = 0;
for (int i = 0; i < E.size(); ++i) {
source = E[i].first;
g[E[i].first].push_back(E[i].second);
g[E[i].second].push_back(E[i].first);
}
q.push(source);
color[source] = 1;
while (!q.empty()) {
int n = q.front();
q.pop();
for (int i = 0; i < g[n].size(); ++i) {
if (color[g[n][i]] && color[g[n][i]] != 3 - color[n]) return 0;
if (!color[g[n][i]]) q.push(g[n][i]);
color[g[n][i]] = 3 - color[n];
}
}
return 1;
}
void dfs(int n, int d) {
dep[n] = d;
dis[n] = low[n] = ++now;
for (int i = 0; i < G[n].size(); ++i) {
if (G[n][i] == dad[n]) continue;
if (dis[G[n][i]] && dis[G[n][i]] < dis[n]) {
A[top] = n;
B[top] = G[n][i];
++top;
low[n] = min(low[n], dis[G[n][i]]);
} else if (!dis[G[n][i]]) {
int fa = find(n);
int fb = find(G[n][i]);
if (fa != fb) uf[fa] = fb;
A[top] = n;
B[top] = G[n][i];
++top;
dad[G[n][i]] = n;
dfs(G[n][i], d + 1);
low[n] = min(low[n], low[G[n][i]]);
if (low[G[n][i]] >= dis[n]) {
cut[n] = 1;
E.clear();
++CN;
while (A[top] != n || B[top] != G[n][i]) {
if (cut[A[top - 1]] || G[A[top - 1]].size() == 1) {
T[A[top - 1]].push_back(CN);
T[CN].push_back(A[top - 1]);
}
if (cut[B[top - 1]] || G[B[top - 1]].size() == 1) {
T[B[top - 1]].push_back(CN);
T[CN].push_back(B[top - 1]);
}
com[A[top - 1]] = com[B[top - 1]] = CN;
E.push_back(make_pair(A[top - 1], B[top - 1]));
--top;
}
good[CN] = !bicolorable();
}
}
}
}
void tdfs(int n, int d) {
tdep[n] = d;
for (int i = 0; i < T[n].size(); ++i) {
if (tbio[T[n][i]] || T[n][i] == tdad[n]) continue;
tbio[T[n][i]] = 1;
tdad[T[n][i]] = n;
tdfs(T[n][i], d + 1);
}
}
int main(void) {
scanf("%d%d", &N, &M);
for (int i = 1; i <= N; ++i) uf[i] = i;
for (int i = 0; i < M; ++i) {
scanf("%d%d", &a, &b);
G[a].push_back(b);
G[b].push_back(a);
}
CN = N;
for (int i = 1; i <= N; ++i) {
if (dis[i]) continue;
cut[i] = 1;
dfs(i, 1);
tdfs(i, 1);
}
for (int i = 1; i <= CN; ++i) {
link[i][0] = tdad[i];
comp[i][0] |= good[tdad[i]];
}
for (int i = 1; i <= 18; ++i) {
for (int j = 1; j <= CN; ++j) {
link[j][i] = link[link[j][i - 1]][i - 1];
comp[j][i] = comp[j][i - 1] || comp[link[j][i - 1]][i - 1];
}
}
scanf("%d", &Q);
for (int i = 0; i < Q; ++i) {
scanf("%d%d", &a, &b);
if (a == b)
puts("No");
else if (find(a) != find(b))
puts("No");
else if (dep[a] % 2 != dep[b] % 2)
puts("Yes");
else {
if (!cut[a]) a = com[a];
if (!cut[b]) b = com[b];
bool good_comp = good[a] || good[b];
if (tdep[a] < tdep[b]) swap(a, b);
for (int i = 18; i >= 0; --i) {
if (tdep[link[a][i]] >= tdep[b]) {
good_comp |= comp[a][i];
a = link[a][i];
}
}
for (int i = 18; i >= 0; --i) {
if (link[a][i] == link[b][i]) continue;
good_comp |= comp[a][i];
good_comp |= comp[b][i];
a = link[a][i];
b = link[b][i];
}
if (a != b) good_comp = good_comp || good[tdad[a]];
puts(good_comp ? "Yes" : "No");
}
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
vector<int> G[N];
int n, m, dfn[N], low[N], sta[N], top, dep[N], fa[N][21], scc_num, scc_id[N],
w[N], dfs_clock;
int q, num, odd[N];
bool vis[N];
void dfs(int u, int father) {
vis[u] = 1;
dep[u] = dep[father] + 1;
fa[u][0] = father;
for (int i = 1; i < 21; i++) fa[u][i] = fa[fa[u][i - 1]][i - 1];
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
if (!vis[v]) dfs(v, u);
}
}
void Tarjan(int u, int father) {
dfn[u] = low[u] = ++dfs_clock;
sta[++top] = u;
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
if (v == father) continue;
if (!dfn[v]) {
Tarjan(v, u);
low[u] = min(low[u], low[v]);
} else
low[u] = min(low[u], dfn[v]);
}
if (low[u] == dfn[u]) {
scc_num++;
while (sta[top] != u) scc_id[sta[top--]] = scc_num;
scc_id[sta[top--]] = scc_num;
}
}
void dfs1(int u, int father) {
vis[u] = 1;
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
if (!vis[v]) {
w[v] += w[u];
dfs1(v, u);
}
}
}
int LCA(int u, int v) {
if (dep[u] < dep[v]) swap(u, v);
int delta = dep[u] - dep[v];
for (int i = 0; i < 21; i++)
if (delta & (1 << i)) u = fa[u][i];
if (u == v) return u;
for (int i = 20; i >= 0; i--)
if (fa[u][i] != fa[v][i]) {
u = fa[u][i];
v = fa[v][i];
}
return fa[u][0];
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++) {
int u, v;
scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
for (int i = 1; i <= n; i++)
if (!vis[i]) dfs(i, 0);
memset(vis, 0, sizeof(vis));
for (int i = 1; i <= n; i++)
if (!dfn[i]) Tarjan(i, 0);
for (int i = 1; i <= n; i++)
if (!odd[scc_id[i]]) {
for (int e = 0; e < G[i].size(); e++) {
int v = G[i][e];
if ((dep[i] + dep[v]) % 2 == 0 && scc_id[i] == scc_id[v]) {
odd[scc_id[i]] = 1;
break;
}
}
}
for (int i = 1; i <= n; i++) {
if (scc_id[i] == scc_id[fa[i][0]] && fa[i][0] && odd[scc_id[i]]) w[i]++;
}
for (int i = 1; i <= n; i++)
if (!vis[i]) dfs1(i, 0);
scanf("%d", &q);
while (q--) {
int u, v;
scanf("%d%d", &u, &v);
int l = LCA(u, v);
if (l == 0) {
puts("No");
continue;
}
if ((dep[u] + dep[v] - dep[l] * 2) & 1)
puts("Yes");
else {
if (w[u] + w[v] != w[l] * 2)
puts("Yes");
else
puts("No");
}
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 100111;
const int MAXM = 100111;
int N, M;
int F[MAXN];
int Find(int a) { return F[a] == a ? a : F[a] = Find(F[a]); }
struct Vert {
int FE;
int Dep;
int Cnt;
int Dps;
int Dfn, Low;
bool Vis;
} V[MAXN];
struct Edge {
int x, y, next, neg;
bool u, odd;
int Bel;
} E[MAXM << 1];
int Ecnt;
void addE(int a, int b) {
++Ecnt;
E[Ecnt].x = a;
E[Ecnt].y = b;
E[Ecnt].next = V[a].FE;
V[a].FE = Ecnt;
E[Ecnt].neg = Ecnt + 1;
++Ecnt;
E[Ecnt].y = a;
E[Ecnt].x = b;
E[Ecnt].next = V[b].FE;
V[b].FE = Ecnt;
E[Ecnt].neg = Ecnt - 1;
}
int DFN;
void DFS(int at) {
V[at].Dps = DFN;
V[at].Vis = true;
for (int k = V[at].FE, to; k > 0; k = E[k].next) {
to = E[k].y;
if (V[to].Vis) continue;
E[k].u = true;
E[E[k].neg].u = true;
V[to].Dep = V[at].Dep + 1;
DFS(to);
}
}
int St[MAXM << 1], Top;
int Bcnt;
bool Odd[MAXM << 1];
void PBC(int at, int e = 0) {
++DFN;
V[at].Low = V[at].Dfn = DFN;
for (int k = V[at].FE, to; k > 0; k = E[k].next) {
if (k == E[e].neg) continue;
to = E[k].y;
if (V[to].Dfn) {
if (V[to].Dfn < V[at].Dfn) St[++Top] = k;
V[at].Low = min(V[at].Low, V[to].Dfn);
} else {
St[++Top] = k;
PBC(to, k);
V[at].Low = min(V[at].Low, V[to].Low);
}
}
if (V[at].Low >= V[E[e].x].Dfn) {
++Bcnt;
while (Top > 1 && St[Top] != e) {
E[St[Top]].Bel = Bcnt;
--Top;
}
E[St[Top]].Bel = Bcnt;
--Top;
}
}
void Push(int at) {
V[at].Vis = true;
for (int k = V[at].FE, to; k > 0; k = E[k].next) {
to = E[k].y;
if (V[to].Vis) continue;
V[to].Cnt = V[at].Cnt + E[k].odd;
Push(to);
if (!E[k].odd) F[to] = at;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin >> N >> M;
for (int i = 1, a, b; i <= M; ++i) {
cin >> a >> b;
addE(a, b);
}
for (int i = 1; i <= N; ++i)
if (!V[i].Vis) {
++DFN;
DFS(i);
}
for (int i = 1, a, b; i <= Ecnt; ++i) {
if (E[i].u) continue;
a = E[i].x;
b = E[i].y;
if (V[a].Dep < V[b].Dep) swap(a, b);
if ((V[a].Dep - V[b].Dep) & 1) continue;
E[i].odd = true;
}
for (int i = 1; i <= N; ++i)
if (!V[i].Dfn) {
Top = 0;
PBC(i);
}
for (int i = 1; i <= Ecnt; ++i)
if (E[i].Bel && E[i].odd) Odd[E[i].Bel] = true;
for (int i = 1; i <= Ecnt; ++i)
if (Odd[E[i].Bel]) E[i].odd = true;
for (int i = 1; i <= Ecnt; ++i)
if (E[E[i].neg].odd) E[i].odd = true;
for (int i = 1; i <= N; ++i) V[i].Vis = false;
for (int i = 1; i <= N; ++i) F[i] = i;
for (int i = 1; i <= N; ++i)
if (!V[i].Vis) Push(i);
int Qcnt;
cin >> Qcnt;
for (int i = 1, a, b; i <= Qcnt; ++i) {
cin >> a >> b;
if (V[a].Dps != V[b].Dps)
puts("No");
else {
if (V[a].Dep < V[b].Dep) swap(a, b);
if ((V[a].Dep - V[b].Dep) & 1)
puts("Yes");
else if (Find(a) != Find(b))
puts("Yes");
else
puts("No");
}
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
const int N = 1e5 + 50;
int n, m, q, head[N], nxt[N << 1], ver[N << 1], f[N], cnt;
int fa[N], dep[N], siz[N], son[N], top[N], d[N];
int tot, dfn[N], low[N];
std::pair<int, int> st[N];
int stop;
bool vis[N];
inline int find(int x) {
while (x != f[x]) x = f[x] = f[f[x]];
return x;
}
inline void add(int u, int v) {
nxt[++cnt] = head[u];
ver[cnt] = v;
head[u] = cnt;
}
void dfs1(int x) {
siz[x] = 1;
vis[x] = true;
int maxson = -1;
for (int i = head[x]; i; i = nxt[i]) {
int y = ver[i];
if (!vis[y]) {
fa[y] = x;
dep[y] = dep[x] + 1;
dfs1(y);
siz[x] += siz[y];
if (siz[y] > maxson) {
son[x] = y;
maxson = siz[y];
}
}
}
}
void dfs2(int u, int topf) {
top[u] = topf;
if (son[u]) {
dfs2(son[u], topf);
for (int i = head[u]; i; i = nxt[i]) {
int v = ver[i];
if (fa[v] == u && v != son[u]) {
dfs2(v, v);
}
}
}
}
inline int LCA(int u, int v) {
while (top[u] != top[v]) {
if (dep[top[u]] > dep[top[v]])
u = fa[top[u]];
else
v = fa[top[v]];
}
return dep[u] > dep[v] ? v : u;
}
void tarjan(int u) {
dfn[u] = low[u] = ++tot;
for (int i = head[u]; i; i = nxt[i]) {
int v = ver[i];
std::pair<int, int> cur = std::make_pair(u, v);
if (fa[v] == u) {
st[++stop] = cur;
tarjan(v);
low[u] = std::min(low[u], low[v]);
if (low[v] >= dfn[u]) {
int f = 0;
for (int j = stop;; --j) {
if ((dep[st[j].first] & 1) == (dep[st[j].second] & 1)) {
f = 1;
break;
}
if (st[j] == cur) break;
}
do {
if (st[stop].first != u) d[st[stop].first] = f;
if (st[stop].second != u) d[st[stop].second] = f;
} while (st[stop--] != cur);
}
} else if (dfn[v] < dfn[u] && fa[u] != v) {
low[u] = std::min(low[u], dfn[v]);
st[++stop] = cur;
}
}
}
inline void calc(int u) {
d[u] += d[fa[u]];
for (int i = head[u]; i; i = nxt[i]) {
int v = ver[i];
if (fa[v] == u) calc(v);
}
}
template <int T>
struct fast_io {
char p[T], q[T], *p1, *p2, *q1, *q2;
fast_io() {
p1 = p2 = p;
q1 = q, q2 = q + T;
}
inline char gc() {
return p1 == p2 && (p2 = (p1 = p) + fread(p, 1, T, stdin), p1 == p2)
? EOF
: *p1++;
}
inline void pc(char c) {
if (q1 == q2) q2 = (q1 = q) + fwrite(q, 1, T, stdout);
*q1++ = c;
}
~fast_io() { fwrite(q, 1, q1 - q, stdout); }
};
fast_io<1 << 18> io;
inline long long read() {
long long res = 0;
char ch;
do ch = io.gc();
while (ch < 48 || ch > 57);
do res = res * 10 + ch - 48, ch = io.gc();
while (ch >= 48 && ch <= 57);
return res;
}
inline void read(char *s) {
char ch;
do ch = io.gc();
while (!isalpha(ch));
do *s++ = ch, ch = io.gc();
while (isalpha(ch));
*s = 0;
}
inline int rb() {
char ch;
do ch = io.gc();
while (ch < 48 || ch > 57);
return ch - 48;
}
inline void put(long long x) {
if (x < 0) io.pc('-'), x = -x;
if (x >= 10) put(x / 10);
io.pc(48 + x % 10);
}
inline void output(long long x, char ch = ' ') {
put(x);
io.pc(ch);
}
inline void outputs(const char *s) {
while (*s) io.pc(*s++);
io.pc('\n');
}
inline bool solve(int u, int v) {
if (find(u) != find(v)) {
return false;
}
if ((dep[u] ^ dep[v]) & 1) return true;
int lca = LCA(u, v);
return d[u] + d[v] - (d[lca] << 1) > 0;
}
int main() {
n = read(), m = read();
for (int i = 1; i <= n; ++i) f[i] = i;
for (int i = 1; i <= m; ++i) {
int u = read(), v = read();
add(u, v), add(v, u);
u = find(u), v = find(v);
if (u != v) f[u] = v;
}
q = read();
for (int i = 1; i <= n; ++i) {
if (!vis[i]) {
dfs1(i);
dfs2(i, i);
tarjan(i);
calc(i);
}
}
while (q--) {
int u = read(), v = read();
outputs(solve(u, v) ? "Yes" : "No");
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 100000;
int n, m;
unordered_set<int> g[MAXN];
int color[MAXN];
int low[MAXN];
int depth[MAXN];
stack<pair<int, int> > edge_stack;
vector<pair<int, int> > edges_to_remove;
int COMP_ID;
int comp_before[MAXN];
int comp_after[MAXN];
void remove_comp(int u, int v, bool odd) {
pair<int, int> uv(u, v);
while (true) {
pair<int, int> top = edge_stack.top();
edge_stack.pop();
if (odd) edges_to_remove.push_back(top);
if (top == uv) break;
}
}
bool dfs_before(int u, int p, int d) {
comp_before[u] = COMP_ID;
depth[u] = d;
color[u] = d % 2;
low[u] = d - 1;
bool u_odd = false;
for (int v : g[u]) {
if (v == p) continue;
if (depth[v] == -1) {
edge_stack.emplace(u, v);
bool v_odd = dfs_before(v, u, d + 1);
if (p == -1) {
assert(low[v] >= d);
}
if (low[v] >= d) {
remove_comp(u, v, v_odd);
} else {
u_odd = u_odd or v_odd;
low[u] = min(low[u], low[v]);
}
} else if (depth[v] < d) {
assert(d - depth[v] >= 2);
edge_stack.emplace(u, v);
low[u] = min(low[u], depth[v]);
if (color[u] == color[v]) {
u_odd = true;
}
} else {
}
}
return u_odd;
}
void dfs_after(int u, int p) {
comp_after[u] = COMP_ID;
for (int v : g[u]) {
if (v == p or comp_after[v] != -1) continue;
dfs_after(v, u);
}
}
int main() {
scanf("%d%d", &n, &m);
while (m--) {
int a, b;
scanf("%d%d", &a, &b);
--a, --b;
g[a].insert(b);
g[b].insert(a);
}
memset(depth, -1, sizeof(depth[0]) * n);
COMP_ID = 0;
for (int u = 0; u <= n - 1; ++u) {
if (depth[u] == -1) {
dfs_before(u, -1, 0);
COMP_ID++;
}
}
for (auto& e : edges_to_remove) {
g[e.first].erase(e.second);
g[e.second].erase(e.first);
}
memset(comp_after, -1, sizeof(comp_after[0]) * n);
COMP_ID = 0;
for (int u = 0; u <= n - 1; ++u) {
if (comp_after[u] == -1) {
dfs_after(u, -1);
COMP_ID++;
}
}
int q;
scanf("%d", &q);
while (q--) {
int a, b;
scanf("%d%d", &a, &b);
--a, --b;
if (comp_before[a] != comp_before[b])
puts("No");
else if (comp_after[a] != comp_after[b])
puts("Yes");
else if (color[a] != color[b])
puts("Yes");
else
puts("No");
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
static char B[10000010];
char *e = B + 10000000, *p = e;
char Getchar() {
if (p == e) fread(B, 1, 10000000, stdin), p = B;
return *(p++);
}
void read(int &ret) {
ret = 0;
char c;
while (c = Getchar(), c < '0' || c > '9')
;
do (ret *= 10) += c - '0';
while (c = Getchar(), c >= '0' && c <= '9');
}
int n, m, q;
int to[200010], nxt[200010], fst[100010], tot = 1;
void add(int u, int v) {
to[++tot] = v;
nxt[tot] = fst[u];
fst[u] = tot;
}
int d[100010], bl[100010], cnt;
int stk[200010], no[100010], top, bccno;
int anc[100010][17], dfn[100010], clk;
int dfs(int u) {
bl[u] = cnt;
d[u] = d[anc[u][0]] + 1;
int low = dfn[u] = ++clk, lowv;
for (int e = fst[u]; e; e = nxt[e]) {
int v = to[e];
if (!dfn[v]) {
stk[++top] = e;
anc[v][0] = u, lowv = dfs(v);
low = min(low, lowv);
if (lowv >= dfn[u]) {
++bccno;
do no[stk[top] >> 1] = bccno;
while (stk[top--] != e);
}
} else if (dfn[v] < dfn[u] && v != anc[u][0]) {
low = min(low, dfn[v]);
stk[++top] = e;
}
}
return low;
}
int z[17] = {1};
bool val[100010][17], ok[100010];
bool work(int u, int v) {
int l = 0, i;
bool ok = 0;
if (d[u] < d[v]) swap(u, v);
i = 16;
while (!ok && d[u] != d[v]) {
while (d[anc[u][i]] < d[v]) --i;
ok = val[u][i], u = anc[u][i];
l += z[i];
}
i = 16;
while (!ok && u != v) {
while (i && anc[u][i] == anc[v][i]) --i;
l += z[i + 1], ok = val[u][i] | val[v][i];
u = anc[u][i], v = anc[v][i];
}
return ok || l & 1;
}
int main() {
for (int i = 1; i <= 16; ++i) z[i] = z[i - 1] << 1;
read(n), read(m);
for (int i = 1, u, v; i <= m; ++i) {
read(u), read(v);
add(u, v), add(v, u);
}
for (int i = 1; i <= n; ++i)
if (!dfn[i]) ++cnt, dfs(i);
for (int e = 2, u, v; e <= tot; e += 2) {
u = to[e], v = to[e | 1];
if (d[u] < d[v]) swap(u, v);
if (anc[u][0] != v && !((d[u] - d[v]) & 1)) ok[no[e >> 1]] = 1;
}
for (int e = 2, u, v; e <= tot; e += 2) {
u = to[e], v = to[e | 1];
if (d[u] < d[v]) swap(u, v);
if (anc[u][0] == v) val[u][0] = ok[no[e >> 1]];
}
for (int i = 1; i <= 16; ++i)
for (int u = 1; u <= n; ++u) {
anc[u][i] = anc[anc[u][i - 1]][i - 1];
val[u][i] = val[u][i - 1] | val[anc[u][i - 1]][i - 1];
}
read(q);
for (int i = 1, u, v; i <= q; ++i) {
read(u), read(v);
puts(bl[u] == bl[v] && work(u, v) ? "Yes" : "No");
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
int read() {
char k = getchar();
int x = 0;
while (k < '0' || k > '9') k = getchar();
while (k >= '0' && k <= '9') x = x * 10 + k - '0', k = getchar();
return x;
}
const int MX = 2e5 + 23;
int n, m;
int ORG[MX], CAC[MX], tot = 1;
struct edge {
int node, next, w;
} h[MX * 6];
void addedge(int u, int v, int *head, int flg = 1) {
h[++tot] = (edge){v, head[u], 1}, head[u] = tot;
if (flg) addedge(v, u, head, 0);
}
int __[MX];
void init() {
for (int i = 1; i < MX; ++i) __[i] = i;
}
int find(int x) { return __[x] == x ? x : __[x] = find(__[x]); }
void link(int u, int v) { __[find(u)] = find(v); }
int DFN[MX], low[MX], stk[MX], stktop, cnt, jishu[MX];
int pcnt, oddcyc, color[MX];
void tarjan(int x, int c = 0, int *head = ORG) {
DFN[x] = low[x] = ++cnt, stk[++stktop] = x;
color[x] = c;
for (int i = head[x], d; i; i = h[i].next) {
int odd = oddcyc;
if (h[i].w) {
if (color[d = h[i].node] == c) ++oddcyc;
h[i].w = h[i ^ 1].w = 0;
}
if (!DFN[d = h[i].node]) {
int QWQ = odd;
tarjan(d, c ^ 1);
odd = (oddcyc - odd) > 0;
low[x] = std::min(low[x], low[d]);
if (low[d] == DFN[x]) {
jishu[++pcnt] = odd;
color[pcnt] = 0;
for (int tmp = 0; tmp != d; --stktop) {
tmp = stk[stktop];
addedge(pcnt, tmp, CAC);
;
;
}
addedge(x, pcnt, CAC);
;
;
oddcyc = QWQ;
}
} else if (DFN[d] < low[x])
low[x] = DFN[d];
}
}
int hson[MX], size[MX], dep[MX], fa[MX];
int Sji[MX];
void dfs(int x, int *head = CAC) {
Sji[x] += jishu[x];
;
;
size[x] = 1;
for (int i = head[x], d; i; i = h[i].next) {
if ((d = h[i].node) == fa[x]) continue;
fa[d] = x, dep[d] = dep[x] + 1;
Sji[d] += Sji[x];
dfs(d);
size[x] += size[d];
if (size[d] > size[hson[x]]) hson[x] = d;
}
}
int top[MX];
void dfs2(int x, int topf, int *head = CAC) {
top[x] = topf;
for (int i = head[x], d; i; i = h[i].next) {
if ((d = h[i].node) == fa[x]) continue;
dfs2(d, d == hson[x] ? topf : d);
}
}
int LCA(int x, int y) {
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]]) std::swap(x, y);
x = fa[top[x]];
}
return dep[x] < dep[y] ? x : y;
}
int query(int x, int y) {
if (x == y) return 0;
if (find(x) != find(y)) return 0;
int lca = LCA(x, y);
if (Sji[x] + Sji[y] - 2 * Sji[lca] + jishu[lca]) return 1;
if (color[x] ^ color[y]) return 1;
return 0;
}
int main() {
memset(color, -1, sizeof color);
init();
pcnt = n = read(), m = read();
for (int i = 1, u, v; i <= m; ++i) {
u = read(), v = read();
addedge(u, v, ORG);
link(u, v);
}
for (int i = 1; i <= n; ++i)
if (!DFN[i]) tarjan(i);
for (int i = 1; i <= pcnt; ++i)
;
;
memset(DFN, 0, sizeof DFN);
for (int i = 1; i <= n; ++i) {
if (!DFN[find(i)]) {
DFN[find(i)] = 1;
dfs(find(i));
dfs2(find(i), find(i));
}
}
int q = read();
while (q--) {
int u = read(), v = read();
printf("%s\n", query(u, v) ? "Yes" : "No");
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
template <class BidirIt>
BidirIt prev(BidirIt it,
typename iterator_traits<BidirIt>::difference_type n = 1) {
advance(it, -n);
return it;
}
template <class ForwardIt>
ForwardIt next(ForwardIt it,
typename iterator_traits<ForwardIt>::difference_type n = 1) {
advance(it, n);
return it;
}
const double EPS = 1e-9;
const double PI = 3.141592653589793238462;
template <typename T>
inline T sq(T a) {
return a * a;
}
const int MAXN = 2e5 + 5;
const int LOGN = 19;
vector<pair<int, int> > gr[MAXN];
vector<int> tree[MAXN];
int fr[MAXN], to[MAXN];
int num[MAXN], low[MAXN], counter;
int prv[MAXN];
bool arti[MAXN];
int numbc, curroot;
int who[MAXN], bel[MAXN], edbel[MAXN];
vector<int> comp[MAXN];
stack<int> S;
int col[MAXN], val[MAXN];
int par[MAXN][LOGN], lvl[MAXN];
int cc[MAXN], which_cc = 1;
bool visit[MAXN];
void dfs(int u) {
assert(!visit[u]);
visit[u] = true;
cc[u] = which_cc;
num[u] = low[u] = counter++;
int kids = 0;
for (auto v : gr[u]) {
if (num[v.first] == -1) {
kids++;
prv[v.first] = u;
S.push(v.second);
dfs(v.first);
if (low[v.first] >= num[u]) {
arti[u] = true;
++numbc;
while (true) {
int i = S.top();
S.pop();
comp[numbc].push_back(i);
edbel[i] = numbc;
if (i == v.second) break;
}
}
low[u] = min(low[u], low[v.first]);
} else if (prv[u] != v.first && num[v.first] < num[u]) {
S.push(v.second);
low[u] = min(low[u], num[v.first]);
}
}
if (u == curroot) arti[u] = (kids > 1);
}
void paint(int u) {
assert(!visit[u]);
visit[u] = true;
for (auto v : gr[u]) {
if (col[v.first] == -1) {
col[v.first] = 1 - col[u];
paint(v.first);
} else if (col[v.first] == col[u]) {
val[edbel[v.second]] = 1;
}
}
}
void init(int u) {
if (visit[u]) {
if (who[u] != -1)
puts("articulation");
else
puts("random");
fflush(stdout);
}
assert(!visit[u]);
visit[u] = true;
for (int j = 1; j < LOGN; j++) {
if (par[u][j - 1] != -1) par[u][j] = par[par[u][j - 1]][j - 1];
}
for (auto v : tree[u]) {
if (v != par[u][0]) {
par[v][0] = u;
val[v] += val[u];
lvl[v] = lvl[u] + 1;
init(v);
}
}
}
int LCA(int u, int v) {
if (lvl[u] > lvl[v]) swap(u, v);
for (int j = LOGN - 1; j >= 0; j--)
if (lvl[u] + (1 << j) <= lvl[v]) v = par[v][j];
if (u == v) return u;
for (int j = LOGN - 1; j >= 0; j--)
if (par[u][j] != -1 && par[u][j] != par[v][j]) u = par[u][j], v = par[v][j];
return par[u][0];
}
int main() {
int n, m;
scanf("%d %d", &n, &m);
for (int i = 1; i <= m; i++) {
scanf("%d %d", &fr[i], &to[i]);
gr[fr[i]].push_back(make_pair(to[i], i));
gr[to[i]].push_back(make_pair(fr[i], i));
}
counter = 0;
numbc = 0;
memset(num, -1, sizeof num);
memset(visit, false, sizeof visit);
for (int i = 1; i <= n; i++) {
if (num[i] == -1) {
prv[i] = -1;
curroot = i;
dfs(i);
which_cc++;
}
}
memset(visit, false, sizeof visit);
memset(col, -1, sizeof col);
for (int i = 1; i <= n; i++) {
if (col[i] == -1) {
col[i] = 1;
paint(i);
}
}
memset(who, -1, sizeof who);
memset(bel, -1, sizeof bel);
for (int i = 1, sz = numbc; i <= sz; i++) {
for (auto it : comp[i]) {
if (arti[fr[it]]) {
tree[i].push_back(fr[it]);
} else {
bel[fr[it]] = i;
}
if (arti[to[it]]) {
tree[i].push_back(to[it]);
} else {
bel[to[it]] = i;
}
}
sort((tree[i]).begin(), (tree[i]).end());
tree[i].resize(unique((tree[i]).begin(), (tree[i]).end()) -
tree[i].begin());
for (auto &v : tree[i]) {
if (bel[v] == -1) {
bel[v] = ++numbc;
who[numbc] = v;
}
v = bel[v];
tree[v].push_back(i);
}
}
memset(par, -1, sizeof par);
memset(visit, false, sizeof visit);
for (int i = 1; i <= numbc; i++) {
if (par[i][0] == -1) init(i);
}
int q;
scanf("%d", &q);
while (q--) {
int u, v;
scanf("%d %d", &u, &v);
if (cc[u] != cc[v] || u == v) {
puts("No");
continue;
}
int cu = bel[u], cv = bel[v];
int lca = LCA(cu, cv);
if (val[cu] + val[cv] - val[lca] -
(par[lca][0] == -1 ? 0 : val[par[lca][0]]) >
0) {
puts("Yes");
} else {
puts(col[u] != col[v] ? "Yes" : "No");
}
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 200005, maxm = 400005;
struct edge {
int to, next;
} e[maxm * 2];
int h[maxn], tot;
int n, m, q;
struct Edge {
int u, v;
bool operator<(const Edge &a) const { return u != a.u ? u < a.u : v < a.v; }
};
stack<Edge> S;
vector<Edge> bcc_e[maxn];
map<Edge, int> odd;
inline void add(int u, int v) {
e[++tot] = (edge){v, h[u]};
h[u] = tot;
}
int Time, dfn[maxn], low[maxn], bcc_cnt, bccno[maxn];
vector<int> bcc[maxn];
bool iscut[maxn];
void tarjan(int u, int fa) {
dfn[u] = low[u] = ++Time;
int child = 0;
for (int i = h[u]; i; i = e[i].next) {
int v = e[i].to;
Edge E = (Edge){u, v};
if (!dfn[v]) {
S.push(E);
++child;
tarjan(v, u);
low[u] = min(low[u], low[v]);
if (low[v] >= dfn[u]) {
iscut[u] = true;
bcc_cnt++;
for (;;) {
Edge x = S.top();
S.pop();
bcc_e[bcc_cnt].push_back(x);
if (bccno[x.u] != bcc_cnt)
bcc[bcc_cnt].push_back(x.u), bccno[x.u] = bcc_cnt;
if (bccno[x.v] != bcc_cnt)
bcc[bcc_cnt].push_back(x.v), bccno[x.v] = bcc_cnt;
if (x.u == u && x.v == v) break;
}
}
} else if (dfn[v] < dfn[u] && v != fa) {
S.push(E);
low[u] = min(low[u], dfn[v]);
}
}
if (fa == 0 && child == 1) iscut[u] = false;
}
void find_bcc() {
for (int i = 1; i <= n; ++i)
if (!dfn[i]) tarjan(i, 0);
}
int now, col[maxn];
bool color(int u) {
for (int i = h[u]; i; i = e[i].next) {
int v = e[i].to;
if (bccno[v] != now) continue;
if (col[v] == col[u]) return false;
if (!col[v]) {
col[v] = 3 - col[u];
if (!color(v)) return false;
}
}
return true;
}
void color_bcc() {
fill(bccno + 1, bccno + n + 1, 0);
for (int i = 1; i <= bcc_cnt; ++i) {
for (int v : bcc[i]) bccno[v] = i;
int u = bcc[i][0];
col[u] = 1;
now = i;
if (!color(u))
for (Edge E : bcc_e[i]) odd[E] = true;
for (int v : bcc[i]) col[v] = 0;
}
}
int ufs[maxn], U[maxn], V[maxn];
int find(int x) {
if (ufs[x] == 0) return x;
return ufs[x] = find(ufs[x]);
}
bool merge(int u, int v) {
int x = find(u), y = find(v);
if (x == y) return false;
ufs[x] = y;
return true;
}
void build_tree() {
for (int i = 1, j = 1; i < n; ++i) {
while (j <= m && !merge(U[j], V[j])) ++j;
if (j > m) break;
add(U[j], V[j]);
add(V[j], U[j]);
}
}
int logn = 1;
int deep[maxn], f[maxn][20], unv[maxn][20];
int flag[maxn];
void dfs(int u, int fa) {
flag[u] = now;
f[u][0] = fa;
deep[u] = deep[fa] + 1;
for (int i = h[u], v; i; i = e[i].next) {
v = e[i].to;
if (v == fa) continue;
unv[v][0] = odd.count((Edge){u, v}) | odd.count((Edge){v, u});
dfs(v, u);
}
}
void get_fa() {
for (int &j = logn; (1 << j) <= n; ++j)
for (int i = 1; i <= n; ++i)
f[i][j] = f[f[i][j - 1]][j - 1],
unv[i][j] = unv[i][j - 1] | unv[f[i][j - 1]][j - 1];
}
bool lca(int u, int v) {
if (deep[u] > deep[v]) swap(u, v);
int ans = 0, p = deep[v] - deep[u], len = 0;
for (int i = 0; (1 << i) <= n; ++i)
if (p & (1 << i)) ans |= unv[v][i], v = f[v][i], len += (1 << i);
if (u == v) return ans || (len & 1);
for (int i = logn; i >= 0; --i)
if (f[u][i] != f[v][i]) {
ans |= unv[v][i] | unv[u][i];
u = f[u][i];
v = f[v][i];
len += (1 << i) << 1;
}
ans |= unv[v][0] | unv[u][0];
len += 2;
return ans || (len & 1);
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; ++i) {
scanf("%d%d", &U[i], &V[i]);
add(U[i], V[i]);
add(V[i], U[i]);
}
find_bcc();
color_bcc();
fill(h + 1, h + n + 1, 0);
tot = 0;
build_tree();
now = 0;
for (int i = 1; i <= n; ++i)
if (!flag[i]) ++now, dfs(i, 0);
get_fa();
scanf("%d", &q);
for (int x, y, i = 1; i <= q; ++i) {
scanf("%d%d", &x, &y);
if (flag[x] == flag[y] && lca(x, y))
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
inline char gc() {
static char now[1 << 16], *S, *T;
if (S == T) {
T = (S = now) + fread(now, 1, 1 << 16, stdin);
if (S == T) return EOF;
}
return *S++;
}
inline int read() {
int x = 0, f = 1;
char c = gc();
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = gc();
}
while (c >= '0' && c <= '9') {
x = (x << 3) + (x << 1) + c - 48;
c = gc();
}
return x * f;
}
int n, m, q;
namespace tree {
struct adj {
int to, nxt;
} e[100010 << 1];
int hd[100010], cnt = 1, dep[100010], f[100010][17], val[100010];
inline void ins(int x, int y) {
e[++cnt] = (adj){y, hd[x]};
hd[x] = cnt;
}
void dfs(int x) {
for (int i = 1; i <= 16; ++i) f[x][i] = f[f[x][i - 1]][i - 1];
for (int i = hd[x]; i; i = e[i].nxt) {
int y = e[i].to;
if (dep[y]) continue;
dep[y] = dep[x] + 1;
f[y][0] = x;
dfs(y);
}
}
inline int getlca(int x, int y) {
if (dep[x] < dep[y]) {
swap(x, y);
}
for (int i = 16; i >= 0; --i) {
if (dep[f[x][i]] >= dep[y]) {
x = f[x][i];
}
}
if (x == y) return x;
for (int i = 16; i >= 0; --i) {
if (f[x][i] != f[y][i]) {
x = f[x][i];
y = f[y][i];
}
}
return f[x][0];
}
bool vis[100010];
void DFS(int x) {
vis[x] = 1;
for (int i = hd[x]; i; i = e[i].nxt) {
if (e[i].to == f[x][0]) continue;
val[e[i].to] += val[x];
DFS(e[i].to);
}
}
inline void gao() {
memset(vis, 0, sizeof(vis));
for (int i = 1; i <= n; ++i) {
if (!vis[i]) DFS(i);
}
}
} // namespace tree
struct adj {
int to, nxt, flag;
} e[100010 << 1];
int hd[100010], cnt = 1;
inline void addedge(int x, int y) {
e[++cnt] = (adj){y, hd[x], 0};
hd[x] = cnt;
}
int Fa[100010];
int getfa(int x) { return (Fa[x] == x) ? x : (Fa[x] = getfa(Fa[x])); }
int fa_gen[100010];
int getfa_gen(int x) {
return (fa_gen[x] == x) ? x : (fa_gen[x] = getfa_gen(fa_gen[x]));
}
inline void gen_tree() {
for (int i = 1; i <= n; ++i) fa_gen[i] = i;
for (int i = 2; i <= cnt; i += 2) {
int x = e[i ^ 1].to, y = e[i].to;
if (getfa_gen(x) != getfa_gen(y)) {
fa_gen[getfa_gen(x)] = getfa_gen(y);
e[i].flag = e[i ^ 1].flag = 1;
tree::ins(x, y);
tree::ins(y, x);
}
}
for (int i = 1; i <= n; ++i)
if (!(tree::dep[i])) tree::dep[i] = 1, tree::dfs(i);
}
int cor[100010];
namespace dcc {
int dfn[100010], low[100010], tim = 0, ok[100010 << 1];
stack<int> sta, temp;
bool dfs(int x) {
for (int i = hd[x]; i; i = e[i].nxt) {
if (!ok[i]) continue;
int y = e[i].to;
if (cor[y] == (cor[x] ^ 1)) continue;
if (cor[y] == cor[x]) return 0;
cor[y] = (cor[x] ^ 1);
if (!dfs(y)) return 0;
}
return 1;
}
void tarjan(int x, int f) {
dfn[x] = low[x] = ++tim;
for (int i = hd[x]; i; i = e[i].nxt) {
int y = e[i].to;
if (y == f) continue;
if (!dfn[y]) {
sta.push(i);
tarjan(y, x);
low[x] = min(low[x], low[y]);
if (low[y] >= dfn[x]) {
while (true) {
int edg = sta.top();
sta.pop();
ok[edg] = ok[edg ^ 1] = 1;
cor[e[edg ^ 1].to] = cor[e[edg].to] = -1;
temp.push(edg);
if (e[edg ^ 1].to == x && e[edg].to == y) break;
}
cor[x] = 0;
bool flag = dfs(x);
while (!temp.empty()) {
int edg = temp.top();
temp.pop();
int v = e[edg].to, u = e[edg ^ 1].to;
if (tree::dep[u] < tree::dep[v]) swap(u, v);
if (e[edg].flag && !flag) {
tree::val[u] = 1;
}
ok[edg] = ok[edg ^ 1] = 0;
}
}
} else if (dfn[y] < dfn[x])
sta.push(i), low[x] = min(low[x], dfn[y]);
}
}
inline void init() {
for (int i = 1; i <= n; ++i) tarjan(i, 0);
}
} // namespace dcc
int main() {
n = read();
m = read();
for (int i = 1; i <= n; ++i) Fa[i] = i;
for (int i = 1; i <= m; ++i) {
int x = read(), y = read();
addedge(x, y);
addedge(y, x);
Fa[getfa(x)] = getfa(y);
}
gen_tree();
dcc::init();
tree::gao();
q = read();
while (q--) {
int x = read(), y = read();
if (getfa(x) != getfa(y)) {
puts("No");
continue;
}
int lca = tree::getlca(x, y);
if ((tree::dep[x] + tree::dep[y] - 2 * tree::dep[lca]) & 1)
puts("Yes");
else {
if (tree::val[x] + tree::val[y] - 2 * tree::val[lca] > 0)
puts("Yes");
else
puts("No");
}
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int n, m, Q, num = 0, top = 0, tot = 0, d[110010], h[110010], fa[110010][20],
id[110010], cut[110010], dfn[110010], low[110010], vis[110010];
pair<int, int> q[110010 << 1];
struct edge {
int x, y, next;
} mp[110010 << 1];
inline char gc() {
static char *S, *T, buf[1 << 16];
if (T == S) {
T = (S = buf) + fread(buf, 1, 1 << 16, stdin);
if (T == S) return EOF;
}
return *S++;
}
inline int read() {
int x = 0, f = 1;
char ch = gc();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = gc();
}
while ('0' <= ch && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = gc();
return x * f;
}
inline void ins(int x, int y) {
mp[++num].x = x;
mp[num].y = y;
mp[num].next = h[x];
h[x] = num;
mp[++num].x = y;
mp[num].y = x;
mp[num].next = h[y];
h[y] = num;
}
void dfs1(int x, int f) {
for (int i = 1; i <= 17; i++) {
if (!fa[x][i - 1]) break;
fa[x][i] = fa[fa[x][i - 1]][i - 1];
}
id[x] = f;
for (int i = h[x]; i; i = mp[i].next) {
int y = mp[i].y;
if (d[y]) continue;
d[y] = d[x] + 1;
fa[y][0] = x;
dfs1(y, f);
}
}
void tarjan(int x, int f) {
dfn[x] = low[x] = ++tot;
for (int i = h[x]; i; i = mp[i].next) {
int y = mp[i].y;
if (y == f || dfn[y] >= dfn[x]) continue;
q[++top] = make_pair(x, y);
if (!dfn[y]) {
tarjan(y, x);
low[x] = min(low[x], low[y]);
if (low[y] >= dfn[x]) {
int tmp = top, u, v, flag = 0;
do {
u = q[top].first, v = q[top--].second;
if ((d[u] & 1) == (d[v] & 1)) {
flag = 1;
break;
}
} while (!(x == u && y == v));
if (!flag) continue;
top = tmp;
do {
u = q[top].first, v = q[top--].second;
cut[u] = cut[v] = 1;
} while (!(x == u && y == v));
cut[x] = 0;
}
} else
low[x] = min(low[x], dfn[y]);
}
}
inline void dfs(int x) {
cut[x] += cut[fa[x][0]];
vis[x] = 1;
for (int i = h[x]; i; i = mp[i].next) {
int y = mp[i].y;
if (vis[y]) continue;
dfs(y);
}
}
inline int lca(int x, int y) {
if (d[x] < d[y]) swap(x, y);
int len = d[x] - d[y];
for (int i = 0; i <= 17; i++) {
if (len & (1 << i)) x = fa[x][i];
}
if (x == y) return x;
for (int i = 17; i >= 0; i--) {
if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i];
}
return fa[x][0];
}
int main() {
n = read();
m = read();
for (int i = 1; i <= m; i++) ins(read(), read());
for (int i = 1; i <= n; i++) {
if (!d[i]) d[i] = 1, dfs1(i, i), tarjan(i, 0), dfs(i);
}
Q = read();
for (int i = 1; i <= Q; i++) {
int x = read(), y = read();
if (id[x] != id[y]) {
puts("No");
continue;
}
int z = lca(x, y);
if ((d[x] + d[y] - 2 * d[z]) & 1) {
puts("Yes");
continue;
}
if (cut[x] + cut[y] - 2 * cut[z] > 0)
puts("Yes");
else
puts("No");
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 1000000 + 10;
struct Edge {
int next, v;
Edge() {}
Edge(int next, int v) : next(next), v(v) {}
} e[maxn << 1];
struct Node {
int val, pos;
Node() {}
Node(int val, int pos) : val(val), pos(pos) {}
};
int n, m;
int head[maxn], cnt;
int dfn[maxn], low[maxn], timer;
int dep[maxn];
int dfs_seq[maxn];
int pre[maxn];
int anc[maxn][20];
int dis[maxn][20];
int st[maxn], top;
int bcc_num[maxn], bcc_cnt;
set<int> bcc[maxn];
bool odd[maxn], cut[maxn];
int vis[maxn], num;
bool up[maxn], used[maxn];
inline int read() {
int x = 0;
char ch = getchar();
while (ch < '0' || ch > '9') ch = getchar();
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x;
}
inline void AddEdge(int u, int v) {
e[++cnt] = Edge(head[u], v);
head[u] = cnt;
e[++cnt] = Edge(head[v], u);
head[v] = cnt;
}
void dfs(int u, int fa = 0) {
vis[u] = num;
int chi = 0;
dfs_seq[++timer] = u;
dfn[u] = low[u] = timer;
for (int i = head[u]; i; i = e[i].next) {
int v = e[i].v;
if (!dfn[v]) {
dep[v] = dep[u] + 1;
chi++;
pre[v] = u;
st[++top] = i;
dfs(v, u);
low[u] = min(low[u], low[v]);
if (low[v] >= dfn[u]) {
cut[u] = 1;
bcc[++bcc_cnt].clear();
int pos;
do {
pos = st[top--];
if (bcc_num[e[pos].v] != bcc_cnt)
bcc_num[e[pos].v] = bcc_cnt, bcc[bcc_cnt].insert(e[pos].v);
if (bcc_num[e[pos ^ 1].v] != bcc_cnt)
bcc_num[e[pos ^ 1].v] = bcc_cnt, bcc[bcc_cnt].insert(e[pos ^ 1].v);
if (used[pos] || used[pos ^ 1]) odd[bcc_cnt] = 1;
} while (pos != i);
}
} else if (dfn[v] < dfn[u] && v != fa) {
st[++top] = i;
low[u] = min(low[u], dfn[v]);
if (!((dep[u] - dep[v]) & 1)) used[i] = used[i ^ 1] = 1;
}
}
if (!fa && chi < 2) cut[u] = 0;
}
inline Node LCA(int u, int v) {
if (dep[u] < dep[v]) swap(u, v);
int res = 0;
int h = dep[u] - dep[v], d = 0;
while (h) {
if (h & 1) {
res += dis[u][d];
u = anc[u][d];
}
d++;
h >>= 1;
}
if (u == v) return Node(res, u);
for (int i = 18; i >= 0; i--)
if (anc[u][i] != anc[v][i]) {
res += dis[u][i] + dis[v][i];
u = anc[u][i], v = anc[v][i];
}
return Node(res + dis[u][0] + dis[v][0], anc[u][0]);
}
int main() {
if (fopen("sunset.in", "r") != NULL) {
freopen("sunset.in", "r", stdin);
freopen("sunset.out", "w", stdout);
}
cnt = 1;
n = read(), m = read();
for (int i = 1, u, v; i <= m; i++) {
u = read(), v = read();
AddEdge(u, v);
}
for (int i = 1; i <= n; i++)
if (!vis[i]) {
num++;
dep[i] = 1;
dfs(i);
}
for (int i = 1; i <= bcc_cnt; i++) {
if (odd[i])
for (set<int>::iterator SB = bcc[i].begin(); SB != bcc[i].end(); SB++) {
int u = *SB;
if (bcc[i].find(pre[u]) != bcc[i].end()) up[u] = 1;
}
}
for (int i = 1; i <= n; i++) {
int u = dfs_seq[i];
anc[u][0] = pre[u];
dis[u][0] = up[u];
for (int j = 0; anc[anc[u][j]][j]; j++) {
anc[u][j + 1] = anc[anc[u][j]][j];
dis[u][j + 1] = dis[anc[u][j]][j] + dis[u][j];
}
}
int q = read(), x, y;
for (int kase = 1; kase <= q; kase++) {
x = read(), y = read();
if (vis[x] != vis[y] || x == y) {
puts("No");
continue;
}
Node cur = LCA(x, y);
int lca = cur.pos;
if ((dep[x] + dep[y] - 2 * dep[lca]) & 1) {
puts("Yes");
continue;
}
if (cur.val)
puts("Yes");
else
puts("No");
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
inline int read() {
register int x = 0, f = 1;
register char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = 0;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + (ch ^ '0');
ch = getchar();
}
return f ? x : -x;
}
const int N = 1e5 + 5;
int n, m, head[N], num, vis[N], col[N], id;
vector<int> E[N];
int dep[N], fa[N], top[N], son[N], siz[N];
inline void dfs1(int u, int pa) {
vis[u] = 1;
col[u] = id;
dep[u] = dep[fa[u] = pa] + 1;
siz[u] = 1;
for (int v : E[u]) {
if (vis[v]) continue;
dfs1(v, u);
siz[u] += siz[v];
if (siz[son[u]] < siz[v]) son[u] = v;
}
}
inline void dfs2(int u) {
vis[u] = 1;
if (!top[u]) top[u] = u;
if (son[u]) {
top[son[u]] = top[u];
dfs2(son[u]);
}
for (int v : E[u]) {
if (vis[v] || v == son[u]) continue;
dfs2(v);
}
}
inline int lca(int x, int y) {
while (top[x] ^ top[y])
if (dep[top[x]] < dep[top[y]])
y = fa[top[y]];
else
x = fa[top[x]];
return dep[x] < dep[y] ? x : y;
}
int dfn[N], low[N], dfc, stk[N], tp, cnt[N], tot, c[N], flg;
vector<int> V[N];
inline void Tarjan(int u, int pa) {
dfn[u] = low[u] = ++dfc;
stk[++tp] = u;
for (int v : E[u]) {
if (v == pa) continue;
if (!dfn[v]) {
Tarjan(v, u);
low[u] = min(low[u], low[v]);
if (dfn[u] <= low[v]) {
V[++tot].push_back((u));
for (int x = 0; x ^ v; --tp) x = stk[tp], V[tot].push_back((x));
}
} else
low[u] = min(low[u], dfn[v]);
}
}
inline void Dfs(int u, int ccc) {
if (flg) return;
c[u] = ccc;
for (int v : E[u]) {
if (vis[v] ^ vis[u]) continue;
if (!c[v])
Dfs(v, 3 - ccc);
else if (c[u] == c[v]) {
flg = 1;
return;
}
}
}
inline void Cal(int u, int pa) {
vis[u] = 1;
cnt[u] += cnt[pa];
for (int v : E[u]) {
if (vis[v]) continue;
Cal(v, u);
}
}
inline bool check(int x, int y) {
if (col[x] != col[y]) return 0;
if ((dep[x] & 1) ^ (dep[y] & 1)) return 1;
return cnt[x] + cnt[y] - 2 * cnt[lca(x, y)] > 0;
}
int main() {
n = read(), m = read();
for (int i = (1), _ed = (m); i <= _ed; ++i) {
int u = read(), v = read();
E[u].push_back(v), E[v].push_back(u);
}
for (int i = (1), _ed = (n); i <= _ed; ++i)
if (!vis[i]) ++id, dfs1(i, 0);
memset(vis, 0, sizeof vis);
for (int i = (1), _ed = (n); i <= _ed; ++i)
if (!vis[i]) dfs2(i);
for (int i = (1), _ed = (n); i <= _ed; ++i)
if (!dfn[i]) tp = 0, Tarjan(i, 0);
memset(vis, 0, sizeof vis);
for (int i = (1), _ed = (tot); i <= _ed; ++i) {
for (int j = (0), _ed = (V[i].size() - 1); j <= _ed; ++j)
vis[V[i][j]] = i, c[V[i][j]] = 0;
flg = 0;
Dfs(V[i][0], 1);
if (flg)
for (int j = (1), _ed = (V[i].size() - 1); j <= _ed; ++j)
cnt[V[i][j]] = 1;
}
memset(vis, 0, sizeof vis);
for (int i = (1), _ed = (n); i <= _ed; ++i)
if (!vis[i]) Cal(i, 0);
for (int q = (1), _ed = (read()); q <= _ed; ++q) {
int x = read(), y = read();
puts(check(x, y) ? "Yes" : "No");
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 100005;
vector<int> e[N];
int dep[N], f[N], ok[N];
int cnt[N], fa[N][18];
int n, m;
int get(int x) { return x == f[x] ? x : f[x] = get(f[x]); }
void dfs(int x) {
dep[x] = dep[fa[x][0]] + 1;
for (auto i : e[x])
if (!dep[i]) {
fa[i][0] = x;
dfs(i);
if (get(x) == get(i)) ok[x] |= ok[i];
} else if (dep[i] + 1 < dep[x]) {
if ((dep[i] + dep[x] + 1) & 1) ok[x] = 1;
for (int y = get(x); dep[i] + 1 < dep[y]; y = get(y)) f[y] = fa[y][0];
}
}
void dfs2(int x) {
cnt[x] += ok[x];
for (auto i : e[x])
if (dep[i] == dep[x] + 1) {
if (get(x) == get(i)) ok[i] |= ok[x];
cnt[i] = cnt[x];
dfs2(i);
}
}
int LCA(int x, int y) {
if (dep[x] < dep[y]) swap(x, y);
int tmp = dep[x] - dep[y];
for (int i = (int)(0); i <= (int)(16); i++)
if (tmp & (1 << i)) x = fa[x][i];
for (int i = (int)(16); i >= (int)(0); i--)
if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i];
return x == y ? x : fa[x][0];
}
bool ask(int x, int y) {
int L = LCA(x, y);
if (!L) return 0;
return ((dep[y] + dep[x]) & 1) || (cnt[x] + cnt[y] - 2 * cnt[L]);
}
int main() {
scanf("%d%d", &n, &m);
for (int i = (int)(1); i <= (int)(m); i++) {
int x, y;
scanf("%d%d", &x, &y);
e[x].push_back(y);
e[y].push_back(x);
}
for (int i = (int)(1); i <= (int)(n); i++) f[i] = i;
for (int i = (int)(1); i <= (int)(n); i++)
if (!dep[i]) dfs(i);
for (int i = (int)(1); i <= (int)(16); i++)
for (int j = (int)(1); j <= (int)(n); j++)
fa[j][i] = fa[fa[j][i - 1]][i - 1];
for (int i = (int)(1); i <= (int)(n); i++)
if (dep[i] == 1) dfs2(i);
scanf("%d", &m);
while (m--) {
int x, y;
scanf("%d%d", &x, &y);
puts(ask(x, y) ? "Yes" : "No");
}
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
#pragma GCC optimize("O2")
using namespace std;
const int maxn = 1e6 + 10, inf = 1e9 + 10, lg = 22;
int n, m, q, cid[maxn], csize = 0, H[maxn], low[maxn], ok[maxn], par[maxn][lg],
comp[maxn], c1;
bool dp[maxn][lg];
vector<int> G[maxn];
bool mark[maxn];
void dfs(int a, int p) {
par[a][0] = p;
for (int i = 1; i < lg; i++) {
par[a][i] = par[par[a][i - 1]][i - 1];
}
low[a] = inf;
mark[a] = 1;
for (int b : G[a]) {
if (!mark[b]) {
H[b] = H[a] + 1;
comp[b] = comp[a];
dfs(b, a);
low[a] = min(low[a], low[b]);
} else if (b != p) {
low[a] = min(low[a], H[b]);
}
}
}
void dfs_build(int a) {
mark[a] = 1;
for (int b : G[a]) {
if (!mark[b]) {
if (low[b] >= H[a]) {
cid[b] = csize++;
} else {
cid[b] = cid[a];
}
dfs_build(b);
} else if (H[a] > H[b]) {
int dis = H[a] - H[b];
if (dis % 2 == 0) {
ok[cid[a]] = 1;
}
}
}
}
void dfss(int a) {
mark[a] = 1;
dp[a][0] = (ok[cid[a]]);
for (int b : G[a]) {
if (!mark[b]) {
dfss(b);
}
}
}
bool lca(int a, int b) {
bool ans = 0;
if (H[a] > H[b]) swap(a, b);
for (int i = lg - 1; i >= 0; i--) {
if (H[par[b][i]] >= H[a]) {
ans |= dp[b][i];
b = par[b][i];
}
}
if (a == b) {
return ans;
}
for (int i = lg - 1; i >= 0; i--) {
if (par[b][i] != par[a][i]) {
ans |= dp[a][i];
ans |= dp[b][i];
a = par[a][i];
b = par[b][i];
}
}
ans |= dp[a][0];
ans |= dp[b][0];
return ans;
}
int32_t main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < m; i++) {
int a, b;
scanf("%d%d", &a, &b);
a--, b--;
G[a].push_back(b);
G[b].push_back(a);
}
for (int i = 0; i < n; i++) {
if (!mark[i]) {
comp[i] = c1++;
dfs(i, i);
}
}
fill(mark, mark + n, 0);
for (int i = 0; i < n; i++) {
if (!mark[i]) {
cid[i] = csize++;
dfs_build(i);
}
}
fill(mark, mark + n, 0);
for (int i = 0; i < n; i++) {
if (!mark[i]) {
dfss(i);
}
}
for (int j = 0; j < lg - 1; j++) {
for (int i = 0; i < n; i++) {
dp[i][j + 1] = dp[i][j] | dp[par[i][j]][j];
}
}
scanf("%d", &q);
while (q--) {
int a, b;
scanf("%d%d", &a, &b);
a--, b--;
if (H[a] > H[b]) swap(a, b);
if (a == b || comp[a] != comp[b]) {
printf("No\n");
continue;
}
if ((H[b] - H[a]) & 1) {
printf("Yes\n");
continue;
}
bool ans = lca(a, b);
printf(ans ? "Yes\n" : "No\n");
}
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 1000;
vector<int> side[maxn];
int dfn[maxn], low[maxn], idx, f[maxn][23], stk[maxn], top, dep[maxn],
bel[maxn];
bool odd[maxn], in[maxn];
int mark[maxn];
vector<int> edge[maxn];
void tarjan(int u, int fa) {
bel[u] = bel[fa];
dfn[u] = low[u] = ++idx;
f[u][0] = fa;
stk[++top] = u;
dep[u] = dep[fa] + 1;
in[u] = 1;
for (int i = 0; i < side[u].size(); i++) {
int v = side[u][i];
if (v == fa) continue;
if (!dfn[v])
tarjan(v, u), low[u] = min(low[u], low[v]), edge[u].push_back(v),
edge[v].push_back(u);
else if (in[v])
low[u] = min(low[u], dfn[v]), odd[u] |= (abs(dep[u] - dep[v]) + 1) & 1;
}
if (low[u] == dfn[u]) {
int flag = 0;
for (int t = top; t >= 1 && stk[t] != u; t--)
if (odd[stk[t]]) {
flag = 1;
break;
}
while (stk[top] != u) {
if (flag) mark[stk[top]] |= flag;
in[stk[top]] = 0;
top--;
}
top--;
in[u] = 0;
}
}
int lca(int u, int v) {
if (dep[u] < dep[v]) swap(u, v);
for (int j = 22; j >= 0; j--)
if (dep[f[u][j]] >= dep[v]) u = f[u][j];
if (u == v) return u;
for (int j = 22; j >= 0; j--)
if (f[u][j] != f[v][j]) u = f[u][j], v = f[v][j];
return f[u][0];
}
void dfs(int u, int fa) {
for (int i = 0; i < edge[u].size(); i++) {
int v = edge[u][i];
if (v == fa) continue;
mark[v] += mark[u];
dfs(v, u);
}
}
int dis(int u, int v, int lca) { return dep[u] + dep[v] - 2 * dep[lca]; }
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = (1), iend = (m); i <= iend; i++) {
int u, v;
scanf("%d%d", &u, &v);
side[u].push_back(v);
side[v].push_back(u);
}
for (int i = (1), iend = (n); i <= iend; i++)
if (!dfn[i]) {
bel[0] = i;
top = 0;
tarjan(i, 0);
dfs(i, 0);
}
for (int i = (1), iend = (22); i <= iend; i++)
for (int j = (1), jend = (n); j <= jend; j++)
f[j][i] = f[f[j][i - 1]][i - 1];
int q;
scanf("%d", &q);
for (int i = (1), iend = (q); i <= iend; i++) {
int u, v;
scanf("%d%d", &u, &v);
if (bel[u] != bel[v] || u == v) {
printf("No\n");
continue;
}
int l = lca(u, v);
if (dis(u, v, l) & 1) {
printf("Yes\n");
continue;
}
int ans = mark[u] - 2 * mark[l] + mark[v];
if (ans > 0) {
printf("Yes\n");
continue;
} else {
printf("No\n");
continue;
}
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
namespace FGF {
int n, m;
const int N = 5e5 + 5;
struct edg {
int to, nxt, id;
} e[N << 1];
int cnt = 1, head[N], s;
int rt[N], dfn[N], low[N], st[N], bel[N], col[N], num, tp, dcc, siz[N], sum[N],
dep[N], fa[N][20];
bool vis[N], is[N], fl;
vector<int> V[N], E[N];
void add(int u, int v, int id) {
cnt++;
e[cnt].to = v;
e[cnt].nxt = head[u];
head[u] = cnt;
e[cnt].id = id;
}
void tarjan(int u, int f) {
dfn[u] = low[u] = ++num;
dep[u] = dep[e[f ^ 1].to] + 1, fa[u][0] = e[f ^ 1].to,
rt[u] = rt[e[f ^ 1].to];
for (int i = 1; i <= 18; i++) fa[u][i] = fa[fa[u][i - 1]][i - 1];
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].to;
if (vis[i] || i == (f ^ 1)) continue;
st[++tp] = i;
if (!dfn[v]) {
tarjan(v, i);
low[u] = min(low[u], low[v]);
if (low[v] >= dfn[u]) {
++dcc;
int x = -1;
V[dcc].push_back(u);
do {
x = st[tp--];
bel[x] = bel[x ^ 1] = dcc;
vis[x] = vis[x ^ 1] = 1;
V[dcc].push_back(e[x].to);
E[dcc].push_back(x);
} while (x != i);
}
} else
low[u] = min(low[u], dfn[v]);
}
}
void dfs(int u, int f) {
vis[u] = 1;
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].to;
if (vis[v] || i == (f ^ 1)) continue;
sum[v] = sum[u] + is[i], dfs(v, i);
}
}
bool dfs2(int u, int fro) {
if (vis[u]) {
if (col[u] == col[e[fro ^ 1].to])
return 1;
else
return 0;
}
vis[u] = 1;
col[u] = col[e[fro ^ 1].to] ^ 1;
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].to;
if ((i == (fro ^ 1)) || bel[i] != bel[fro]) continue;
if (dfs2(v, i)) return 1;
}
return 0;
}
int getlca(int u, int v) {
if (dep[u] < dep[v]) swap(u, v);
int d = dep[u] - dep[v];
for (int i = 18; i >= 0; i--)
if (d & (1 << i)) u = fa[u][i];
if (u == v) return u;
for (int i = 18; i >= 0; i--)
if (fa[u][i] != fa[v][i]) u = fa[u][i], v = fa[v][i];
return fa[u][0];
}
void work() {
scanf("%d%d", &n, &m);
int u, v;
for (int i = 1; i <= m; i++) {
scanf("%d%d", &u, &v);
add(u, v, i), add(v, u, i);
}
for (int i = 1; i <= n; i++)
if (!dfn[i]) rt[0] = i, tarjan(i, 0);
memset(vis, 0, sizeof(vis));
for (int i = 1; i <= dcc; i++) {
for (int sz = V[i].size(), j = 0; j < sz; j++) vis[V[i][j]] = 0;
if (dfs2(e[E[i][0]].to, E[i][0]))
for (int sz = E[i].size(), j = 0; j < sz; j++)
is[E[i][j]] = is[E[i][j] ^ 1] = 1;
}
memset(vis, 0, sizeof(vis));
for (int i = 1; i <= n; i++)
if (!vis[i]) dfs(i, 0);
int Q;
scanf("%d", &Q);
while (Q--) {
scanf("%d%d", &u, &v);
if (rt[u] != rt[v])
puts("No");
else {
int lca = getlca(u, v);
if ((dep[u] + dep[v] - 2 * dep[lca]) & 1)
puts("Yes");
else if (sum[u] + sum[v] - 2 * sum[lca])
puts("Yes");
else
puts("No");
}
}
}
} // namespace FGF
int main() {
FGF::work();
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 100005, maxm = 100005;
struct edge {
int to, next;
} e[maxm * 2];
int h[maxn], tot;
int n, m, q;
struct Edge {
int u, v;
bool operator<(const Edge &a) const { return u != a.u ? u < a.u : v < a.v; }
};
stack<Edge> S;
vector<Edge> bcc_e[maxn];
map<Edge, int> odd;
inline void add(int u, int v) {
e[++tot] = (edge){v, h[u]};
h[u] = tot;
}
int Time, dfn[maxn], low[maxn], bcc_cnt, bccno[maxn];
vector<int> bcc[maxn];
bool iscut[maxn];
void tarjan(int u, int fa) {
dfn[u] = low[u] = ++Time;
int child = 0;
for (int i = h[u]; i; i = e[i].next) {
int v = e[i].to;
Edge E = (Edge){u, v};
if (!dfn[v]) {
S.push(E);
++child;
tarjan(v, u);
low[u] = min(low[u], low[v]);
if (low[v] >= dfn[u]) {
iscut[u] = true;
bcc_cnt++;
for (;;) {
Edge x = S.top();
S.pop();
bcc_e[bcc_cnt].push_back(x);
if (bccno[x.u] != bcc_cnt)
bcc[bcc_cnt].push_back(x.u), bccno[x.u] = bcc_cnt;
if (bccno[x.v] != bcc_cnt)
bcc[bcc_cnt].push_back(x.v), bccno[x.v] = bcc_cnt;
if (x.u == u && x.v == v) break;
}
}
} else if (dfn[v] < dfn[u] && v != fa) {
S.push(E);
low[u] = min(low[u], dfn[v]);
}
}
if (fa == 0 && child == 1) iscut[u] = false;
}
void find_bcc() {
for (int i = 1; i <= n; ++i)
if (!dfn[i]) tarjan(i, 0);
}
int now, col[maxn];
bool color(int u) {
for (int i = h[u]; i; i = e[i].next) {
int v = e[i].to;
if (bccno[v] != now) continue;
if (col[v] == col[u]) return false;
if (!col[v]) {
col[v] = 3 - col[u];
if (!color(v)) return false;
}
}
return true;
}
void color_bcc() {
fill(bccno + 1, bccno + n + 1, 0);
for (int i = 1; i <= bcc_cnt; ++i) {
for (int v : bcc[i]) bccno[v] = i;
int u = bcc[i][0];
col[u] = 1;
now = i;
if (!color(u))
for (Edge E : bcc_e[i]) odd[E] = true;
for (int v : bcc[i]) col[v] = 0;
}
}
int ufs[maxn], U[maxn], V[maxn];
int find(int x) {
if (ufs[x] == 0) return x;
return ufs[x] = find(ufs[x]);
}
bool merge(int u, int v) {
int x = find(u), y = find(v);
if (x == y) return false;
ufs[x] = y;
return true;
}
void build_tree() {
for (int i = 1, j = 1; i < n; ++i) {
while (j <= m && !merge(U[j], V[j])) ++j;
if (j > m) break;
add(U[j], V[j]);
add(V[j], U[j]);
}
}
int logn = 1;
int deep[maxn], f[maxn][20], unv[maxn][20];
int flag[maxn];
void dfs(int u, int fa) {
flag[u] = now;
f[u][0] = fa;
deep[u] = deep[fa] + 1;
for (int i = h[u], v; i; i = e[i].next) {
v = e[i].to;
if (v == fa) continue;
unv[v][0] = odd.count((Edge){u, v}) | odd.count((Edge){v, u});
dfs(v, u);
}
}
void get_fa() {
for (int &j = logn; (1 << j) <= n; ++j)
for (int i = 1; i <= n; ++i)
f[i][j] = f[f[i][j - 1]][j - 1],
unv[i][j] = unv[i][j - 1] | unv[f[i][j - 1]][j - 1];
}
bool lca(int u, int v) {
if (deep[u] > deep[v]) swap(u, v);
int ans = 0, p = deep[v] - deep[u], len = 0;
for (int i = 0; (1 << i) <= n; ++i)
if (p & (1 << i)) ans |= unv[v][i], v = f[v][i], len += (1 << i);
if (u == v) return ans || (len & 1);
for (int i = logn; i >= 0; --i)
if (f[u][i] != f[v][i]) {
ans |= unv[v][i] | unv[u][i];
u = f[u][i];
v = f[v][i];
len += (1 << i) << 1;
}
ans |= unv[v][0] | unv[u][0];
len += 2;
return ans || (len & 1);
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; ++i) {
scanf("%d%d", &U[i], &V[i]);
add(U[i], V[i]);
add(V[i], U[i]);
}
find_bcc();
color_bcc();
fill(h + 1, h + n + 1, 0);
tot = 0;
build_tree();
now = 0;
for (int i = 1; i <= n; ++i)
if (!flag[i]) ++now, dfs(i, 0);
get_fa();
scanf("%d", &q);
for (int x, y, i = 1; i <= q; ++i) {
scanf("%d%d", &x, &y);
if (flag[x] == flag[y] && lca(x, y))
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int n, m, x, y, q, cnt[100005], tot, vis[100005], dep[100005], dfn[100005],
low[100005], d[100005], num, f[100005][20], s[100005], b[100005];
vector<int> v[100005];
inline int lca(int x, int y) {
if (dep[x] < dep[y]) swap(x, y);
for (int i = 18; i >= 0; i--)
if (dep[f[x][i]] >= dep[y]) x = f[x][i];
if (x == y) return x;
for (int i = 18; i >= 0; i--)
if (f[x][i] != f[y][i]) x = f[x][i], y = f[y][i];
return f[x][0];
}
int st[100005], top;
void dfs(int x, int fa) {
dfn[x] = low[x] = ++num;
st[++top] = x;
vis[x] = 1;
cnt[x] = tot;
dep[x] = dep[fa] + 1;
f[x][0] = fa;
for (int i = 1; i <= 18; i++) f[x][i] = f[f[x][i - 1]][i - 1];
for (int i = 0; i < v[x].size(); i++) {
int h = v[x][i];
if (h == fa) continue;
if (!dfn[h]) {
dfs(h, x);
low[x] = min(low[x], low[h]);
} else if (vis[h])
low[x] = min(low[x], dfn[h]), d[x] |= ((dep[x] + dep[h]) % 2 == 0);
}
if (dfn[x] == low[x]) {
int flag = 0, t = top;
while (st[t] != x && !flag) flag = d[st[t--]];
while (st[top] != x) {
b[st[top]] = flag;
vis[st[top]] = 0;
top--;
}
vis[x] = 0;
top--;
}
}
void df(int x) {
s[x] += b[x];
for (int i = 0; i < v[x].size(); i++)
if (dep[v[x][i]] == dep[x] + 1) s[v[x][i]] += s[x], df(v[x][i]);
}
inline int pd(int x, int y) {
if (x == y || cnt[x] != cnt[y]) return 0;
if ((dep[x] + dep[y]) & 1) return 1;
int l = lca(x, y);
return s[x] + s[y] - 2 * s[l] > 0;
}
signed main() {
cin >> n >> m;
for (int i = 1; i <= m; i++) {
cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
for (int i = 1; i <= n; i++)
if (!dfn[i]) tot++, dfs(i, 0), df(i);
cin >> q;
while (q--) {
cin >> x >> y;
puts(pd(x, y) ? "Yes" : "No");
}
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int st[100010], aim[100010 << 1], nxt[100010 << 1], ln;
void in_edge(int x, int y) {
aim[ln] = y;
nxt[ln] = st[x];
st[x] = ln++;
}
int father[100010];
int fp[100010][17];
int level[100010];
int belong[100010], tag[100010];
int n, m;
int Find(int *father, int x) {
return father[x] = father[x] == x ? x : Find(father, father[x]);
}
void dfs(int fx, int x) {
for (int j = 1; j < 17 && (1 << j) <= level[x]; j++)
fp[x][j] = fp[fp[x][j - 1]][j - 1];
belong[x] = x;
for (int i = st[x]; i != -1; i = nxt[i])
if (aim[i] != fx) {
int v = aim[i];
if (level[v] == -1) {
level[v] = level[x] + 1;
fp[v][0] = x;
dfs(x, v);
if (Find(belong, v) == Find(belong, x)) tag[x] |= tag[v];
} else if (level[v] < level[x]) {
int p = Find(belong, x);
while (level[p] > level[v] + 1) {
belong[p] = fp[p][0];
p = Find(belong, p);
}
if ((level[x] - level[v]) % 2 == 0) tag[x] = 1;
}
}
}
int sum[100010];
void dfs2(int fx, int x) {
if (tag[Find(belong, x)]) ++sum[x];
for (int i = st[x]; i != -1; i = nxt[i])
if (fp[aim[i]][0] == x) {
int v = aim[i];
sum[v] = sum[x];
if (Find(belong, v) == Find(belong, x)) tag[v] |= tag[x];
dfs2(x, v);
}
}
int lca(int x, int y) {
if (level[x] < level[y]) swap(x, y);
for (int i = 16; i >= 0; i--)
if (level[x] - (1 << i) >= level[y]) x = fp[x][i];
if (x == y) return x;
for (int i = 16; i >= 0; i--)
if (fp[x][i] != fp[y][i]) {
x = fp[x][i];
y = fp[y][i];
}
return fp[x][0];
}
int check(int u, int v) {
if (Find(father, u) != Find(father, v)) return 0;
int r = lca(u, v);
if ((level[u] - level[v]) % 2) return 1;
return sum[u] + sum[v] - 2 * sum[r] > 0;
}
int main() {
memset(st, -1, sizeof(st));
ln = 0;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) father[i] = i;
for (int i = 0; i < m; i++) {
int u, v;
scanf("%d%d", &u, &v);
in_edge(u, v);
in_edge(v, u);
father[Find(father, u)] = Find(father, v);
}
memset(level, -1, sizeof(level));
for (int i = 1; i <= n; i++)
if (level[i] == -1) {
level[i] = 0;
dfs(-1, i);
dfs2(-1, i);
}
int qn;
scanf("%d", &qn);
for (int t = 0; t < qn; t++) {
int u, v;
scanf("%d%d", &u, &v);
puts(check(u, v) ? "Yes" : "No");
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
int read() {
int kkk = 0, x = 1;
char c = getchar();
while ((c < '0' || c > '9') && c != '-') c = getchar();
if (c == '-') c = getchar(), x = -1;
while (c >= '0' && c <= '9') kkk = kkk * 10 + (c - '0'), c = getchar();
return kkk * x;
}
int N, n, m, q;
int vis[MAXN], deep[MAXN], jump[MAXN][22], R[MAXN], father[MAXN];
struct node {
int head[MAXN * 2], tot;
int to[MAXN * 2], nextn[MAXN * 2];
void ADD(int u, int v) {
to[++tot] = v, nextn[tot] = head[u];
head[u] = tot;
}
} Ed, T;
void format(int u, int fa) {
vis[u] = 1;
deep[u] = deep[fa] + 1;
int LOG = log2(deep[u]);
jump[u][0] = fa;
for (int i = 1; i <= LOG; ++i) jump[u][i] = jump[jump[u][i - 1]][i - 1];
for (int i = Ed.head[u]; i != 0; i = Ed.nextn[i]) {
int v = Ed.to[i];
if (vis[v]) continue;
format(v, u);
}
}
int LCA(int x, int y) {
if (deep[x] < deep[y]) swap(x, y);
int C = deep[x] - deep[y], LOG = log2(C);
for (int i = 0; i <= LOG; ++i)
if (C & (1 << i)) x = jump[x][i];
if (x == y) return x;
LOG = log2(deep[x]);
for (int i = LOG; i >= 0; --i)
if (jump[x][i] != jump[y][i]) x = jump[x][i], y = jump[y][i];
return jump[x][0];
}
int dfn[MAXN], low[MAXN], tim, z[MAXN], top;
vector<int> P[MAXN * 2];
vector<pair<int, int>> E[MAXN * 2];
void tarjan(int u, int from) {
dfn[u] = low[u] = ++tim;
z[++top] = u;
for (int i = Ed.head[u]; i != 0; i = Ed.nextn[i])
if (i != from) {
int v = Ed.to[i];
if (!dfn[v]) {
tarjan(v, i ^ 1);
low[u] = min(low[u], low[v]);
if (low[v] >= dfn[u]) {
++N;
T.ADD(u, N);
P[N].push_back(u);
int x;
do {
x = z[top--];
T.ADD(N, x);
P[N].push_back(x);
} while (x != v);
}
} else
low[u] = min(low[u], dfn[v]);
}
}
int Deep[MAXN * 2], Jump[MAXN * 2][23], sum[MAXN * 2];
void Format(int u, int fa) {
Deep[u] = Deep[fa] + 1;
int LOG = log2(Deep[u]);
Jump[u][0] = fa;
for (int i = 1; i <= LOG; ++i) Jump[u][i] = Jump[Jump[u][i - 1]][i - 1];
for (int i = T.head[u]; i != 0; i = T.nextn[i]) {
int v = T.to[i];
Format(v, u);
}
}
int TLCA(int x, int y) {
if (Deep[x] < Deep[y]) swap(x, y);
int C = Deep[x] - Deep[y], LOG = log2(C);
for (int i = 0; i <= LOG; ++i)
if (C & (1 << i)) x = Jump[x][i];
if (x == y) return x;
LOG = log2(Deep[x]);
for (int i = LOG; i >= 0; --i)
if (Jump[x][i] != Jump[y][i]) x = Jump[x][i], y = Jump[y][i];
return Jump[x][0];
}
int col[MAXN], flag;
void paint(int u) {
for (int i = Ed.head[u]; i != 0; i = Ed.nextn[i]) {
int v = Ed.to[i];
if (col[v] == -1) {
col[v] = col[u] ^ 1;
paint(v);
} else if (col[u] == col[v])
flag = 1;
}
}
void Push(int u) {
for (int i = T.head[u]; i != 0; i = T.nextn[i]) {
int v = T.to[i];
sum[v] += sum[u];
Push(v);
}
}
int find(int k) {
if (father[k] != k) father[k] = find(father[k]);
return father[k];
}
int main() {
N = n = read(), m = read();
Ed.tot = 1;
for (int i = 1; i <= n; ++i) father[i] = i;
for (int i = 1; i <= m; ++i) {
int u = read(), v = read();
if (find(u) != find(v)) father[find(u)] = find(v);
Ed.ADD(u, v);
Ed.ADD(v, u);
}
for (int i = 1; i <= n; ++i)
if (!deep[i]) format(i, 0);
for (int i = 1; i <= n; ++i)
if (!dfn[i]) tarjan(i, 0), R[i] = 1;
for (int i = 1; i <= n; ++i)
if (R[i]) Format(i, 0);
for (int u = 1; u <= n; ++u) {
for (int i = Ed.head[u]; i != 0; i = Ed.nextn[i]) {
int v = Ed.to[i], tmp = u;
if (u > v) continue;
if (Deep[tmp] < Deep[v]) swap(tmp, v);
E[Jump[tmp][0]].push_back(make_pair(tmp, v));
}
}
for (int p = n + 1; p <= N; ++p) {
for (int i = 0; i < P[p].size(); ++i)
col[P[p][i]] = -1, Ed.head[P[p][i]] = 0;
Ed.tot = flag = 0;
for (int i = 0; i < E[p].size(); ++i) {
Ed.ADD(E[p][i].first, E[p][i].second);
Ed.ADD(E[p][i].second, E[p][i].first);
}
col[P[p][0]] = 0;
paint(P[p][0]);
sum[p] = flag;
}
for (int i = 1; i <= n; ++i)
if (R[i]) Push(i);
q = read();
int QQ = 0;
while (q--) {
int u = read(), v = read();
++QQ;
if (find(u) != find(v)) {
puts("No");
continue;
}
int dis = deep[u] + deep[v] - 2 * deep[LCA(u, v)];
if (dis & 1)
puts("Yes");
else {
dis = sum[u] + sum[v] - sum[TLCA(u, v)] - sum[Jump[TLCA(u, v)][0]];
if (dis)
puts("Yes");
else
puts("No");
}
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
pair<int, int> edges[200105];
int C[200105], D[200105], temp[200105], sum[200105], num[200105], low[200105],
disc[200105], vis[200105], cno, comp[200105], pa[19][200105], artic[200105],
tim;
stack<int> stk;
set<int> s[200105];
vector<pair<int, int> > g[200105];
vector<int> t[200105];
void put() {
s[cno].insert(edges[stk.top()].first);
s[cno].insert(edges[stk.top()].second);
stk.pop();
}
void setTraverse() {
for (int j : s[cno]) {
if (artic[j]) {
t[cno].push_back(comp[j]);
t[comp[j]].push_back(cno);
} else
comp[j] = cno;
}
}
void dfs(int a, int p, int f = 1) {
low[a] = disc[a] = ++tim;
temp[a] = f;
vis[a] = 1;
int child = 0;
for (pair<int, int> i : g[a]) {
if (!vis[i.first]) {
child++;
stk.push(i.second);
dfs(i.first, a, f ^ 1);
low[a] = min(low[a], low[i.first]);
if ((disc[a] == 1 && child > 1) ||
(disc[a] != 1 && low[i.first] >= disc[a])) {
cno++;
artic[a] = 1;
if (comp[a] == 0) {
comp[a] = cno;
s[cno].insert(a);
cno++;
s[cno].insert(a);
}
while (stk.top() != i.second) put();
put();
setTraverse();
}
} else if (i.first != p && disc[i.first] < low[a]) {
low[a] = min(low[a], disc[i.first]);
stk.push(i.second);
}
}
}
void buildTree(int n) {
for (__typeof(n) j = 1; j <= n; j++) {
if (!vis[j]) {
tim = 0;
dfs(j, j);
if (!stk.empty()) {
cno++;
while (!stk.empty()) put();
setTraverse();
}
}
}
}
void dfs2(int a, int p, int c) {
vis[a] = 1;
sum[a] += num[a];
pa[0][a] = p;
C[a] = c;
for (int i : t[a]) {
if (!vis[i]) {
D[i] = D[a] + 1;
sum[i] = sum[a];
dfs2(i, a, c);
}
}
}
int lca(int a, int b) {
if (D[b] > D[a]) swap(a, b);
int diff = D[a] - D[b];
for (int i = 0; i < 19; i++)
if (diff & (1 << i)) a = pa[i][a];
if (a == b) return a;
for (int i = 19 - 1; i >= 0; i--)
if (pa[i][a] != pa[i][b]) {
a = pa[i][a];
b = pa[i][b];
}
return pa[0][a];
}
int main() {
int n, m, q;
scanf("%d %d", &n, &m);
for (__typeof(m) i = 0; i < m; i++) {
int a, b;
scanf("%d %d", &a, &b);
edges[i + 1] = {a, b};
g[a].emplace_back(b, i + 1);
g[b].emplace_back(a, i + 1);
}
scanf("%d", &q);
buildTree(n);
memset(vis, 0, sizeof vis);
for (int i = 1; i <= cno; i++) {
for (int j : s[i]) {
for (pair<int, int> k : g[j]) {
if (s[i].count(k.first) && temp[j] == temp[k.first]) {
num[i] = 1;
break;
}
}
if (num[i]) break;
}
}
for (__typeof(cno) i = 1; i <= cno; i++) {
if (!vis[i]) {
dfs2(i, 0, i);
}
}
for (int i = 1; i < 19; i++) {
for (int j = 1; j <= cno; j++) pa[i][j] = pa[i - 1][pa[i - 1][j]];
}
for (__typeof(q) i = 0; i < q; i++) {
int a, b;
scanf("%d %d", &a, &b);
int k = 0;
if (C[comp[a]] != C[comp[b]] || a == b)
k = 0;
else if (temp[a] ^ temp[b])
k = 1;
else {
a = comp[a];
b = comp[b];
int c = lca(a, b);
if (sum[a] + sum[b] - 2 * sum[c] + num[c])
k = 1;
else
k = 0;
}
if (k)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int dfn[100005], low[100005], N, Time, Stack[100005], zhan[100005],
fa[19][100005], odd[100005], dep[100005], suf[100005], n, m, q, C,
app[100005], root[100005];
vector<int> edge[100005];
void tarjan(int x) {
dfn[x] = low[x] = ++Time;
app[x] = C;
Stack[++N] = x;
zhan[x] = 1;
for (int i = 1; i <= 17; ++i) fa[i][x] = fa[i - 1][fa[i - 1][x]];
for (int i = 0; i < edge[x].size(); ++i) {
if (fa[0][x] == edge[x][i]) continue;
if (!dfn[edge[x][i]]) {
dep[edge[x][i]] = dep[x] + 1;
fa[0][edge[x][i]] = x;
tarjan(edge[x][i]);
low[x] = min(low[x], low[edge[x][i]]);
} else if (zhan[edge[x][i]]) {
low[x] = min(low[x], dfn[edge[x][i]]);
odd[x] |= ((dep[edge[x][i]] & 1) == (dep[x] & 1));
}
}
zhan[x] = 0;
if (dfn[x] == low[x]) {
int tmp = N, flag = 0;
while (Stack[tmp] != x && !flag) flag |= odd[Stack[tmp--]];
while (Stack[N] != x) {
zhan[Stack[N]] = 0;
suf[Stack[N--]] += flag;
}
N--;
}
}
int lca(int x, int y) {
if (dep[x] < dep[y]) swap(x, y);
for (int i = 17; i >= 0; --i)
if (dep[fa[i][x]] >= dep[y]) x = fa[i][x];
if (x == y) return x;
for (int i = 17; i >= 0; --i)
if (fa[i][x] != fa[i][y]) x = fa[i][x], y = fa[i][y];
return fa[0][x];
}
void getsum(int x) {
for (int i = 0; i < edge[x].size(); ++i)
if (fa[0][edge[x][i]] == x) suf[edge[x][i]] += suf[x], getsum(edge[x][i]);
}
int main() {
scanf("%d%d", &n, &m);
int a, b;
for (int i = 1; i <= m; ++i) {
scanf("%d%d", &a, &b);
edge[a].push_back(b);
edge[b].push_back(a);
}
scanf("%d", &q);
for (int i = 1; i <= n; ++i)
if (!dfn[i]) dep[i] = 1, C++, root[i] = 1, tarjan(i);
for (int i = 1; i <= n; ++i)
if (dep[i] == 1) getsum(i);
for (int i = 1; i <= q; ++i) {
scanf("%d%d", &a, &b);
if (app[a] != app[b]) {
puts("No");
continue;
}
int LCA = lca(a, b);
if ((dep[a] + dep[b] - dep[LCA] - dep[LCA]) & 1) {
puts("Yes");
continue;
}
if (suf[a] + suf[b] - (suf[LCA] << 1) > 0)
puts("Yes");
else
puts("No");
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 100005, maxm = 100005;
struct edge {
int to, next;
} e[maxm * 2];
int h[maxn], tot;
int n, m, q;
struct Edge {
int u, v;
bool operator<(const Edge &a) const { return u != a.u ? u < a.u : v < a.v; }
};
stack<Edge> S;
vector<Edge> bcc_e[maxn];
map<Edge, int> odd;
inline void add(int u, int v) {
e[++tot] = (edge){v, h[u]};
h[u] = tot;
}
int Time, dfn[maxn], low[maxn], bcc_cnt, bccno[maxn];
vector<int> bcc[maxn];
bool iscut[maxn];
void tarjan(int u, int fa) {
dfn[u] = low[u] = ++Time;
int child = 0;
for (int i = h[u]; i; i = e[i].next) {
int v = e[i].to;
Edge E = (Edge){u, v};
if (!dfn[v]) {
S.push(E);
++child;
tarjan(v, u);
low[u] = min(low[u], low[v]);
if (low[v] >= dfn[u]) {
iscut[u] = true;
bcc_cnt++;
for (;;) {
Edge x = S.top();
S.pop();
bcc_e[bcc_cnt].push_back(x);
if (bccno[x.u] != bcc_cnt)
bcc[bcc_cnt].push_back(x.u), bccno[x.u] = bcc_cnt;
if (bccno[x.v] != bcc_cnt)
bcc[bcc_cnt].push_back(x.v), bccno[x.v] = bcc_cnt;
if (x.u == u && x.v == v) break;
}
}
} else if (dfn[v] < dfn[u] && v != fa) {
S.push(E);
low[u] = min(low[u], dfn[v]);
}
}
if (fa == 0 && child == 1) iscut[u] = false;
}
void find_bcc() {
for (int i = 1; i <= n; ++i)
if (!dfn[i]) tarjan(i, 0);
}
int now, col[maxn];
bool color(int u) {
for (int i = h[u]; i; i = e[i].next) {
int v = e[i].to;
if (bccno[v] != now) continue;
if (col[v] == col[u]) return false;
if (!col[v]) {
col[v] = 3 - col[u];
if (!color(v)) return false;
}
}
return true;
}
void color_bcc() {
fill(bccno + 1, bccno + n + 1, 0);
for (int i = 1; i <= bcc_cnt; ++i) {
for (int v : bcc[i]) bccno[v] = i;
int u = bcc[i][0];
col[u] = 1;
now = i;
if (!color(u))
for (Edge E : bcc_e[i]) odd[E] = true;
for (int v : bcc[i]) col[v] = 0;
}
}
int ufs[maxn], U[maxn], V[maxn];
int find(int x) {
if (ufs[x] == 0) return x;
return ufs[x] = find(ufs[x]);
}
bool merge(int u, int v) {
int x = find(u), y = find(v);
if (x == y) return false;
ufs[x] = y;
return true;
}
void build_tree() {
for (int i = 1, j = 1; i < n; ++i) {
while (j <= m && !merge(U[j], V[j])) ++j;
if (j > m) break;
add(U[j], V[j]);
add(V[j], U[j]);
}
}
int logn = 1;
int deep[maxn], f[maxn][20], unv[maxn][20];
int flag[maxn];
void dfs(int u, int fa) {
flag[u] = now;
f[u][0] = fa;
deep[u] = deep[fa] + 1;
for (int i = h[u], v; i; i = e[i].next) {
v = e[i].to;
if (v == fa) continue;
unv[v][0] = odd.count((Edge){u, v}) | odd.count((Edge){v, u});
dfs(v, u);
}
}
void get_fa() {
for (int &j = logn; (1 << j) <= n; ++j)
for (int i = 1; i <= n; ++i)
f[i][j] = f[f[i][j - 1]][j - 1],
unv[i][j] = unv[i][j - 1] | unv[f[i][j - 1]][j - 1];
}
bool lca(int u, int v) {
if (deep[u] > deep[v]) swap(u, v);
int ans = 0, p = deep[v] - deep[u], len = 0;
for (int i = 0; (1 << i) <= n; ++i)
if (p & (1 << i)) ans |= unv[v][i], v = f[v][i], len += (1 << i);
if (u == v) return ans || (len & 1);
for (int i = logn; i >= 0; --i)
if (f[u][i] != f[v][i]) {
ans |= unv[v][i] | unv[u][i];
u = f[u][i];
v = f[v][i];
len += (1 << i) << 1;
}
ans |= unv[v][0] | unv[u][0];
len += 2;
return ans || (len & 1);
}
int main() {
cin >> n >> m;
for (int i = 1; i <= m; ++i) {
scanf("%d%d", &U[i], &V[i]);
add(U[i], V[i]);
add(V[i], U[i]);
}
find_bcc();
color_bcc();
fill(h + 1, h + n + 1, 0);
tot = 0;
build_tree();
now = 0;
for (int i = 1; i <= n; ++i)
if (!flag[i]) ++now, dfs(i, 0);
get_fa();
scanf("%d", &q);
for (int x, y, i = 1; i <= q; ++i) {
scanf("%d%d", &x, &y);
if (flag[x] == flag[y] && lca(x, y))
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int MN = 2e5 + 5, LG = 20;
vector<pair<int, int> > Adj[MN];
int mh[MN], Cmp[MN], h[MN], es[MN], ee[MN], cmp, n, m;
vector<int> st, C[MN], g[MN], adj[MN];
bool mk[MN], col[MN], is[MN], D[LG][MN];
int par[LG][MN];
int H[MN], V[MN], sz_v;
void DFS(int s, int pr, int Comp) {
Cmp[s] = Comp;
mk[s] = true;
mh[s] = h[s];
for (auto x : Adj[s]) {
int v = x.first, e = x.second;
if (mk[v]) continue;
int lst = -1;
if (!st.empty()) lst = st.back();
h[v] = h[s] + 1;
DFS(v, e, Comp);
if (mh[v] >= h[s]) {
while (!st.empty() && st.back() != lst) {
C[cmp].push_back(st.back());
st.pop_back();
}
++cmp;
}
mh[s] = min(mh[s], mh[v]);
}
for (auto x : Adj[s]) {
int v = x.first, e = x.second;
if (h[v] < h[s]) {
if (e ^ pr) mh[s] = min(mh[s], h[v]);
st.push_back(e);
}
}
}
bool bipartite(int s) {
mk[s] = true;
for (auto v : g[s])
if (!mk[v]) {
col[v] = !col[s];
if (!bipartite(v)) return false;
} else if (col[v] == col[s])
return false;
return true;
}
void init() {
cin >> n >> m;
for (int i = 0; i < m; ++i) {
cin >> es[i] >> ee[i];
--es[i], --ee[i];
Adj[es[i]].push_back({ee[i], i});
Adj[ee[i]].push_back({es[i], i});
}
int counter = 0;
for (int i = 0; i < n; ++i)
if (!mk[i]) DFS(i, -1, counter++);
for (int i = 0; i < n; ++i) Adj[i].clear();
memset(mk, 0, sizeof mk);
for (int i = 0; i < cmp; ++i) {
sz_v = 0;
for (auto x : C[i])
g[es[x]].push_back(ee[x]), g[ee[x]].push_back(es[x]), V[sz_v++] = es[x],
V[sz_v++] = ee[x];
sort(V, V + sz_v);
sz_v = unique(V, V + sz_v) - V;
is[i + n] = !bipartite(es[C[i].back()]);
for (int j = 0; j < sz_v; ++j) {
int v = V[j];
adj[v].push_back(i + n), adj[i + n].push_back(v);
mk[v] = false, col[v] = false;
g[v].clear();
}
}
n += cmp;
}
void dfs(int s, int pr) {
mk[s] = true;
if (pr == -1)
H[s] = 0;
else {
H[s] = H[pr] + 1;
if (pr >= n - cmp && is[pr]) D[0][s] = true;
}
par[0][s] = pr;
for (int i = 1; i < LG; ++i) {
if (~par[i - 1][s]) {
par[i][s] = par[i - 1][par[i - 1][s]];
D[i][s] = D[i - 1][s] || D[i - 1][par[i - 1][s]];
} else
par[i][s] = -1;
}
for (auto v : adj[s])
if (v ^ pr) dfs(v, s);
}
bool lca(int u, int v) {
bool ret = false;
if (H[u] < H[v]) swap(u, v);
int t = H[u] - H[v];
for (int i = 0; i < LG; ++i)
if (t & (1 << i)) ret = ret || D[i][u], u = par[i][u];
if (u == v) return ret;
for (int i = LG - 1; ~i; --i)
if (par[i][u] ^ par[i][v]) {
ret = ret || D[i][v] || D[i][u];
u = par[i][u], v = par[i][v];
}
return ret || D[0][u] || D[0][v];
}
int main() {
ios_base ::sync_with_stdio(false), cin.tie(0), cout.tie(0);
init();
memset(mk, 0, sizeof mk);
for (int i = 0; i < n; ++i)
if (!mk[i]) dfs(i, -1);
int q;
cin >> q;
while (q--) {
int u, v;
cin >> u >> v;
--u, --v;
if (Cmp[u] ^ Cmp[v])
cout << "No\n";
else if ((h[u] - h[v]) % 2)
cout << "Yes\n";
else
cout << (lca(u, v) ? "Yes\n" : "No\n");
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
struct E {
int ad, id, ne;
} e[100010 * 4];
int n, m, q, u[100010], v[100010], w[100010], h1[100010], h2[100010],
de[100010], fa[100010], f[100010], p0[100010], p1[100010];
bool o[100010];
void add(int* he, int x, int y, int d) {
static int t = 0;
++t, e[t].ne = he[x], he[x] = t, e[t].ad = y, e[t].id = d;
}
int ff(int* f, int x) {
if (f[x] == x)
return x;
else
return f[x] = ff(f, f[x]);
}
void ff1(int x) {
p0[x] = p1[x] = x;
for (int p = h1[x]; p; p = e[p].ne) {
int y = e[p].ad;
if (!de[y]) {
fa[y] = x, de[y] = de[x] + 1, ff1(y), p1[y] = x;
if (ff(p0, x) == ff(p0, y)) o[x] |= o[y];
} else if (de[y] + 1 < de[x]) {
int z = ff(p0, x);
while (de[z] > de[y] + 1) p0[z] = fa[z], z = ff(p0, z);
if ((de[x] - de[y]) % 2 == 0) o[x] = 1;
}
}
for (int p = h2[x]; p; p = e[p].ne)
if (de[e[p].ad]) w[e[p].id] = ff(p1, e[p].ad);
}
void ff2(int x) {
if (o[ff(p0, x)]) f[x]++;
for (int i = h1[x]; i; i = e[i].ne) {
int y = e[i].ad;
if (de[y] == de[x] + 1) {
if (ff(p0, x) == ff(p0, y)) o[y] |= o[x];
f[y] = f[x], ff2(y);
}
}
}
bool chk(int u, int v, int w) {
if (ff(p1, u) != ff(p1, v)) return 0;
if ((de[u] - de[v]) % 2) return 1;
return f[u] + f[v] - f[w] * 2 > 0;
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1, x, y; i <= m; i++)
scanf("%d%d", &x, &y), add(h1, x, y, i), add(h1, y, x, i);
scanf("%d", &q);
for (int i = 1; i <= q; i++)
scanf("%d%d", u + i, v + i), add(h2, u[i], v[i], i), add(h2, v[i], u[i], i);
for (int i = 1; i <= n; i++)
if (!de[i]) de[i] = 1, ff1(i), ff2(i);
for (int i = 1; i <= q; i++) puts(chk(u[i], v[i], w[i]) ? "Yes" : "No");
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
void read(int &x) {
char c = getchar();
x = 0;
while (c < '0' || c > '9') c = getchar();
while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
}
const int maxn = 2e5 + 10;
int n, m;
namespace Tree {
int tote = 1, FIR[maxn], TO[maxn], NEXT[maxn];
int fa[maxn], qf[20][maxn], dep[maxn];
int c, rmq[20][maxn * 2], lg[maxn * 2];
bool vis[maxn], col[maxn];
int cnt[maxn], sum[maxn], fir[maxn];
int f[maxn];
int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); }
void addedge(int u, int v) {
TO[++tote] = v;
NEXT[tote] = FIR[u];
FIR[u] = tote;
}
void rmq_build() {
int i, j;
for (i = 0; 1 << i <= c; i++) lg[1 << i] = i;
for (i = 1; i <= c; i++) lg[i] = max(lg[i], lg[i - 1]);
for (i = 1; i <= lg[c]; i++)
for (j = 1; j <= c - (1 << i) + 1; j++) {
int l = rmq[i - 1][j], r = rmq[i - 1][j + (1 << (i - 1))];
rmq[i][j] = dep[l] < dep[r] ? l : r;
}
}
int lca(int u, int v) {
u = fir[u];
v = fir[v];
if (u > v) swap(u, v);
int sz = lg[v - u];
int l = rmq[sz][u], r = rmq[sz][v - (1 << sz) + 1];
return dep[l] < dep[r] ? l : r;
}
void dfs(int u) {
qf[0][u] = fa[u];
for (int i = 1; i < 20; i++) qf[i][u] = qf[i - 1][qf[i - 1][u]];
vis[u] = 1;
rmq[0][++c] = u;
fir[u] = c;
for (int p = FIR[u]; p; p = NEXT[p]) {
int v = TO[p];
if (!vis[v]) {
fa[v] = u;
dep[v] = dep[u] + 1;
col[v] = !col[u];
dfs(v);
rmq[0][++c] = u;
}
}
}
int q[maxn], H = 0, T = 0;
void bfs(int S) {
q[++T] = S;
while (H - T)
for (int p = FIR[q[++H]]; p; p = NEXT[p])
if (fa[TO[p]] == q[H]) q[++T] = TO[p];
}
void build() {
int i;
for (i = 1; i <= n; i++)
if (!vis[i]) {
rmq[0][++c] = 0;
dep[i] = 1;
dfs(i);
bfs(i);
}
rmq_build();
}
void prework() {
int i, j;
for (i = 1; i <= n; i++) f[i] = i;
for (i = 1; i <= m; i++) {
int u = TO[i << 1], v = TO[i << 1 | 1], c;
if (dep[u] < dep[v]) swap(u, v);
if (fa[u] == v) continue;
if (find(u) == find(v)) continue;
for (c = u, j = 19; j >= 0; j--)
if (dep[qf[j][c]] > dep[v]) c = qf[j][c];
for (u = find(u); dep[u] > dep[v]; u = find(fa[u])) f[find(u)] = find(c);
}
for (i = 1; i <= m; i++) {
int u = TO[i << 1], v = TO[i << 1 | 1];
if (dep[u] < dep[v]) swap(u, v);
if (fa[u] == v) continue;
if (col[u] == col[v]) {
cnt[find(u)] = 1;
}
}
for (i = 1; i <= n; i++) cnt[i] = cnt[find(i)];
for (i = 1; i <= n; i++) sum[q[i]] = sum[fa[q[i]]] + cnt[q[i]];
}
bool query(int u, int v) {
int f = lca(u, v);
if (!f) return 0;
if (col[u] ^ col[v]) return 1;
return sum[u] + sum[v] - sum[f] - sum[f];
}
} // namespace Tree
int main() {
int i, u, v;
read(n);
read(m);
for (i = 1; i <= m; i++) {
read(u);
read(v);
Tree::addedge(u, v);
Tree::addedge(v, u);
}
Tree::build();
Tree::prework();
read(m);
while (m--) {
read(u);
read(v);
puts(Tree::query(u, v) ? "Yes" : "No");
}
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const long long linf = 1e18 + 5;
const int mod = (int)1e9 + 7;
const int logN = 18;
const int inf = 1e9;
const int N = 2e5 + 5;
int q, n, m, x, y, z, root[N], lca[logN + 1][N], depth[N], odd[N], f[N], up[N],
sum[N];
vector<int> v[N];
int findset(int x) { return f[x] = (f[x] == x ? x : findset(f[x])); }
int LCA(int x, int y) {
if (depth[x] < depth[y]) swap(x, y);
int diff = depth[x] - depth[y];
for (int i = 0; i <= logN; i++)
if (diff & (1 << i)) x = lca[i][x];
if (x == y) return x;
for (int i = logN; i >= 0; i--)
if (lca[i][x] != lca[i][y]) x = lca[i][x], y = lca[i][y];
return lca[0][x] * (lca[0][x] == lca[0][y]);
}
void dfs(int node) {
for (__typeof(v[node].begin()) it = v[node].begin(); it != v[node].end();
it++) {
int to = *it;
if (!depth[to]) {
root[to] = lca[0][to] = node;
depth[to] = depth[node] + 1;
dfs(to);
if (findset(to) == findset(node)) odd[node] |= odd[to];
} else if (depth[node] - 1 > depth[to]) {
int w = findset(node);
while (depth[w] - 1 > depth[to]) {
f[w] = root[w];
w = findset(w);
}
if ((depth[node] - depth[to]) % 2 == 0) odd[node] = 1;
}
}
}
void dfs2(int node) {
if (odd[findset(node)]) ++sum[node];
for (__typeof(v[node].begin()) it = v[node].begin(); it != v[node].end();
it++) {
int to = *it;
if (depth[to] - 1 == depth[node]) {
sum[to] = sum[node];
if (findset(node) == findset(to)) odd[to] |= odd[node];
dfs2(to);
}
}
}
void dfs3(int node) {
for (__typeof(v[node].begin()) it = v[node].begin(); it != v[node].end();
it++) {
int to = *it;
if (depth[to] - 1 == depth[node]) {
odd[to] += odd[node];
dfs3(to);
}
}
}
int main() {
scanf("%d %d", &n, &m);
for (int i = 1; i <= m; i++) {
scanf("%d %d", &x, &y);
v[x].push_back(y);
v[y].push_back(x);
}
for (int i = 1; i <= n; i++) f[i] = i;
for (int i = 1; i <= n; i++) {
if (depth[i]) continue;
depth[i] = 1;
dfs(i);
dfs2(i);
}
for (int j = 1; j <= logN; j++)
for (int i = 1; i <= n; i++) lca[j][i] = lca[j - 1][lca[j - 1][i]];
scanf("%d", &q);
for (int i = 1; i <= q; i++) {
scanf("%d %d", &x, &y);
int t = LCA(x, y);
if (!t ||
(depth[x] + depth[y]) % 2 == 0 && sum[x] + sum[y] - 2 * sum[t] == 0)
printf("No\n");
else
printf("Yes\n");
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
int n, m, q, curcol, col[100010], depth[100010], val[100010], sum[100010],
par[20][100010], parr[100010];
bool vis[100010];
vector<int> g[100010];
int FIND(int pos) {
if (parr[pos] != pos) parr[pos] = FIND(parr[pos]);
return parr[pos];
}
void dfs(int pos, int no, int d) {
vis[pos] = true;
col[pos] = curcol;
par[0][pos] = no;
depth[pos] = d;
for (int i = 0; i < g[pos].size(); i++) {
if (g[pos][i] == no || (vis[g[pos][i]] && depth[g[pos][i]] > depth[pos]))
continue;
if (vis[g[pos][i]]) {
if ((depth[pos] - depth[g[pos][i]]) % 2 == 0) val[pos] = 1;
for (int j = FIND(pos); depth[j] > depth[g[pos][i]] + 1; j = FIND(j))
parr[j] = par[0][j];
continue;
}
dfs(g[pos][i], pos, d + 1);
if (FIND(pos) == FIND(g[pos][i])) val[pos] |= val[g[pos][i]];
}
}
void dfs2(int pos) {
sum[pos] += val[pos];
for (int i = 0; i < g[pos].size(); i++) {
if (depth[g[pos][i]] != depth[pos] + 1) continue;
if (FIND(pos) == FIND(g[pos][i])) val[g[pos][i]] |= val[pos];
sum[g[pos][i]] = sum[pos];
dfs2(g[pos][i]);
}
}
int lca(int pos, int pos2) {
if (depth[pos] < depth[pos2]) swap(pos, pos2);
for (int i = 18; i >= 0; i--)
if (depth[pos] - (int)pow(2, i) >= depth[pos2]) pos = par[i][pos];
for (int i = 18; i >= 0; i--) {
if (par[i][pos] != par[i][pos2]) {
pos = par[i][pos];
pos2 = par[i][pos2];
}
}
if (pos == pos2) return pos;
return par[0][pos];
}
int main() {
cin >> n >> m;
int x, y;
for (int i = 0; i < m; i++) {
scanf("%d%d", &x, &y);
g[x].push_back(y);
g[y].push_back(x);
}
for (int i = 1; i <= n; i++) parr[i] = i;
for (int i = 1; i <= n; i++) {
if (vis[i]) continue;
curcol = i;
dfs(i, 0, 0);
dfs2(i);
}
for (int i = 0; i < 18; i++) {
for (int j = 1; j <= n; j++) {
if (par[i][j] == 0) continue;
par[i + 1][j] = par[i][par[i][j]];
}
}
cin >> q;
for (int qn = 0; qn < q; qn++) {
scanf("%d%d", &x, &y);
if (col[x] != col[y]) {
puts("No");
continue;
}
int z = lca(x, y);
if ((depth[x] - depth[z] + depth[y] - depth[z]) % 2 == 1 ||
sum[x] + sum[y] - sum[z] * 2 > 0)
puts("Yes");
else
puts("No");
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int N, M, Q, u, v, id;
vector<set<int>> G;
vector<int> D, L, P, C1, C2;
stack<pair<int, int>> S, O;
bool dfs(int u, int p, int d, int c) {
D[u] = L[u] = d;
P[u] = c;
bool ans = 0;
for (int v : G[u])
if (v != p) {
if (D[v] == -1) {
S.emplace(u, v);
bool odd = dfs(v, u, d + 1, c ^ 1);
if (p == -1 or L[v] >= d)
while (1) {
auto e = S.top();
S.pop();
if (odd) O.push(e);
if (e == make_pair(u, v)) break;
}
else
ans |= odd;
L[u] = min(L[u], L[v]);
} else if (D[v] < d) {
S.emplace(u, v);
L[u] = min(L[u], D[v]);
if (P[u] == P[v]) ans = 1;
}
}
return ans;
}
void dfs(int u, vector<int> &C) {
D[u] = 1, C[u] = id;
for (int v : G[u])
if (D[v] == -1) dfs(v, C);
}
int main() {
cin >> N >> M;
G.resize(N);
for (int _ = 0; _ < (int)M; _++) {
cin >> u >> v;
u--, v--;
G[u].insert(v);
G[v].insert(u);
}
C1.resize(N);
D.assign(N, -1);
id = 0;
for (int u = 0; u < (int)N; u++)
if (D[u] == -1) dfs(u, C1), id++;
D.assign(N, -1);
L.resize(N);
P.resize(N);
for (int u = 0; u < (int)N; u++)
if (D[u] == -1) dfs(u, -1, 0, 0);
while (!O.empty()) {
tie(u, v) = O.top();
O.pop();
G[u].erase(v);
G[v].erase(u);
}
C2.resize(N);
D.assign(N, -1);
id = 0;
for (int u = 0; u < (int)N; u++)
if (D[u] == -1) dfs(u, C2), id++;
cin >> Q;
while (Q--) {
cin >> u >> v;
u--, v--;
if (C1[u] != C1[v])
cout << "No\n";
else if (C2[u] != C2[v])
cout << "Yes\n";
else if (P[u] != P[v])
cout << "Yes\n";
else
cout << "No\n";
}
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
template <class T>
inline void checkmin(T &a, const T &b) {
if (b < a) a = b;
}
template <class T>
inline void checkmax(T &a, const T &b) {
if (b > a) a = b;
}
const int maxn = 1e5 + 10;
vector<pair<int, int> > g[maxn];
bool odd[maxn], in[maxn];
stack<int> stk;
int col[maxn], n, m, q, dfn[maxn], low[maxn];
int num = 0, cur_cmp = 0, dep[maxn], cmp[maxn], tag[maxn];
int parent[20][maxn];
vector<int> v, vid;
bool colour_graph(int x) {
bool flag = false;
for (int i = 0; i < g[x].size(); i++) {
int to = g[x][i].first, idx = g[x][i].second;
if (!in[to]) continue;
vid.push_back(idx);
flag |= col[to] == col[x];
if (col[to] == -1) col[to] = col[x] ^ 1, flag |= colour_graph(to);
}
return flag;
}
void tarjan(int x) {
dfn[x] = low[x] = ++num;
stk.push(x);
cmp[x] = cur_cmp;
for (int i = 0; i < g[x].size(); i++) {
int to = g[x][i].first, ind = g[x][i].second;
if (!dfn[to]) {
v.clear();
vid.clear();
dep[to] = dep[x] + 1;
tarjan(to);
low[x] = min(low[x], low[to]);
if (low[to] >= dfn[x]) {
v.clear();
vid.clear();
while (stk.top() != to) {
v.push_back(stk.top());
stk.pop();
}
v.push_back(stk.top());
stk.pop();
v.push_back(x);
vid.push_back(ind);
for (int j = 0; j < v.size(); j++) col[v[j]] = -1, in[v[j]] = true;
col[x] = 0;
if (colour_graph(x)) {
for (int j = 0; j < vid.size(); j++) odd[vid[j]] = true;
}
for (int j = 0; j < v.size(); j++) in[v[j]] = false;
}
} else if (dfn[x] > dfn[to])
low[x] = min(low[x], dfn[to]);
}
}
void dfs(int x, int fa) {
parent[0][x] = fa;
for (int i = 0; i < g[x].size(); i++) {
int to = g[x][i].first, id = g[x][i].second;
if (to == fa) continue;
if (dep[to] == dep[x] + 1) {
tag[to] = tag[x] + odd[id];
dfs(to, x);
}
}
}
void get_lca() {
for (int k = 0; k + 1 < 20; k++) {
for (int i = 0; i < n; i++) {
if (parent[k][i] == -1)
parent[k + 1][i] = -1;
else
parent[k + 1][i] = parent[k][parent[k][i]];
}
}
}
int lca(int u, int v) {
if (dep[u] > dep[v]) swap(u, v);
for (int k = 0; k < 20; k++) {
if ((dep[u] - dep[v]) >> k & 1) {
v = parent[k][v];
}
}
if (u == v) return u;
for (int k = 19; k >= 0; k--) {
if (parent[k][u] != parent[k][v]) {
u = parent[k][u];
v = parent[k][v];
}
}
return parent[0][u];
}
bool query(int u, int v) {
if (cmp[u] != cmp[v]) return false;
int dist = dep[u] + dep[v] - dep[lca(u, v)] * 2;
if (dist & 1) return true;
int sum = tag[u] + tag[v] - tag[lca(u, v)] * 2;
return sum > 0;
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < m; i++) {
int u, v;
scanf("%d%d", &u, &v);
u--;
v--;
g[u].push_back(make_pair(v, i));
g[v].push_back(make_pair(u, i));
}
for (int i = 0; i < n; i++)
if (!dfn[i]) tarjan(i), ++cur_cmp, dfs(i, -1);
get_lca();
scanf("%d", &q);
for (int i = 0; i < q; i++) {
int u, v;
scanf("%d%d", &u, &v);
u--;
v--;
printf(query(u, v) ? "Yes\n" : "No\n");
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 100010;
vector<int> G[N];
int n, m, Q, dep[N], fa[N], ffa[N];
int hson[N], top[N], sz[N], sum[N];
int dfn[N], dfc = 0;
bool vis[N], ans[N];
int qu[N], qv[N];
int idx[N];
int find(int x) { return ffa[x] == x ? x : ffa[x] = find(ffa[x]); }
void dfs1(int u) {
vis[u] = 1;
sz[u] = 1;
for (int v : G[u]) {
if (vis[v]) continue;
dep[v] = dep[u] + 1;
fa[v] = u;
dfs1(v);
sz[u] += sz[v];
if (sz[v] > sz[hson[u]]) hson[u] = v;
}
}
void dfs2(int u, int tp) {
dfn[u] = ++dfc;
top[u] = tp;
if (hson[u]) dfs2(hson[u], tp);
for (int v : G[u])
if (fa[v] == u && v != hson[u]) dfs2(v, v);
}
int LCA(int x, int y) {
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]]) swap(x, y);
x = fa[top[x]];
}
return dep[x] < dep[y] ? x : y;
}
bool query(int x, int y) {
if (find(x) != find(y)) return 0;
if ((dep[x] & 1) != (dep[y] & 1)) return 1;
return sum[x] + sum[y] - 2 * sum[LCA(x, y)];
}
void build(int u) {
vis[u] = 1;
for (int v : G[u]) {
if (fa[v] == u || fa[u] == v) continue;
if ((dep[u] & 1) != (dep[v] & 1)) continue;
sum[u]++;
sum[v]++;
sum[LCA(u, v)] -= 2;
}
for (int v : G[u])
if (!vis[v] && fa[v] == u) build(v);
}
void reduction1(int u) {
for (int v : G[u]) {
if (fa[v] != u) continue;
reduction1(v);
sum[u] += sum[v];
}
}
void reduction2(int u) {
for (int v : G[u]) {
if (fa[v] != u) continue;
sum[v] += sum[u];
reduction2(v);
}
}
void clear() {
memset(vis, 0, sizeof(vis));
memset(top, 0, sizeof(top));
memset(hson, 0, sizeof(hson));
memset(sz, 0, sizeof(sz));
memset(dep, 0, sizeof(dep));
memset(sum, 0, sizeof(sum));
memset(fa, 0, sizeof(fa));
dfc = 0;
memset(dfn, 0, sizeof(dfn));
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) ffa[i] = i;
for (int i = 1, u, v; i <= m; i++) {
scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
if (find(u) != find(v)) ffa[find(u)] = find(v);
}
scanf("%d", &Q);
for (int i = 1; i <= Q; i++) scanf("%d%d", qu + i, qv + i);
for (int i = 1; i <= n; i++) idx[i] = i;
for (int tms = 7; tms; tms--) {
random_shuffle(idx + 1, idx + 1 + n);
for (int i = 1; i <= n; i++) random_shuffle(G[i].begin(), G[i].end());
clear();
for (int i = 1; i <= n; i++)
if (!vis[idx[i]]) dfs1(idx[i]), dfs2(idx[i], idx[i]);
memset(vis, 0, sizeof(vis));
for (int i = 1; i <= n; i++)
if (!vis[idx[i]]) {
build(idx[i]);
reduction1(idx[i]);
reduction2(idx[i]);
}
for (int i = 1; i <= Q; i++)
if (!ans[i]) ans[i] = query(qu[i], qv[i]);
}
for (int i = 1; i <= Q; i++) puts(ans[i] ? "Yes" : "No");
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 100010;
const int MAXM = 100010;
int n, m;
struct edge {
int ne, to, val;
edge(int N = 0, int T = 0) : ne(N), to(T) {}
} e[MAXM << 1];
int fir[MAXN], num = 1;
inline void join(int a, int b) {
e[++num] = edge(fir[a], b);
fir[a] = num;
}
int dfn[MAXN], low[MAXN], cnt = 0, stk[MAXN], top = 0, tot = 0, pos[MAXM << 1];
vector<int> dv[MAXN], de[MAXN];
int col[MAXN];
bool vis[MAXN];
int dep[MAXN], anc[MAXN][20], lg[MAXN];
void tarjan(int u, int la) {
dfn[u] = low[u] = ++cnt;
for (int i = fir[u]; i; i = e[i].ne) {
if (i == (la ^ 1) || pos[i]) continue;
int v = e[i].to;
stk[++top] = i;
if (!dfn[v]) {
tarjan(v, i);
low[u] = min(low[u], low[v]);
if (low[v] >= dfn[u]) {
int x = 0;
dv[++tot].push_back(u);
do {
x = stk[top--];
pos[x] = pos[x ^ 1] = tot;
dv[tot].push_back(e[x].to);
de[tot].push_back(x);
} while (x != i);
}
} else
low[u] = min(low[u], dfn[v]);
}
}
bool color(int u, int la, int c) {
if (~col[u]) return col[u] == col[e[la ^ 1].to];
col[u] = c;
for (int i = fir[u]; i; i = e[i].ne) {
if (pos[i] != pos[la] || i == (la ^ 1)) continue;
int v = e[i].to;
if (color(v, i, c ^ 1)) return 1;
}
return 0;
}
int sum[MAXN];
void dfs(int u, int fa) {
dep[u] = dep[anc[u][0] = fa] + 1;
for (int i = 1; i <= lg[dep[u]]; i++) anc[u][i] = anc[anc[u][i - 1]][i - 1];
vis[u] = 1;
for (int i = fir[u]; i; i = e[i].ne) {
int v = e[i].to;
if (vis[v]) continue;
sum[v] = sum[u] + e[i].val;
dfs(v, u);
}
}
inline int LCA(int x, int y) {
if (dep[x] < dep[y]) swap(x, y);
for (int i = lg[dep[x]]; ~i; i--)
if (dep[anc[x][i]] >= dep[y]) x = anc[x][i];
if (x == y) return x;
for (int i = lg[dep[x]]; ~i; i--)
if (anc[x][i] != anc[y][i]) x = anc[x][i], y = anc[y][i];
return anc[x][0];
}
int pa[MAXN];
int find(int x) { return pa[x] == x ? x : pa[x] = find(pa[x]); }
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) pa[i] = i;
for (int i = 1; i <= m; i++) {
int x, y;
scanf("%d%d", &x, &y);
join(x, y);
join(y, x);
pa[find(x)] = find(y);
}
for (int i = 2; i <= n; i++) lg[i] = lg[i >> 1] + 1;
for (int i = 1; i <= n; i++)
if (!dfn[i]) tarjan(i, 0);
for (int i = 1; i <= tot; i++) {
for (int j = 0; j < (int)dv[i].size(); j++) col[dv[i][j]] = -1;
pos[0] = i;
if (color(dv[i][0], 0, 0)) {
for (int j = 0; j < (int)de[i].size(); j++) e[de[i][j]].val = 1;
}
}
for (int i = 1; i <= n; i++)
if (!vis[i]) dfs(i, 0);
int q;
scanf("%d", &q);
while (q--) {
int x = -1, y = -1;
scanf("%d%d", &x, &y);
if (find(x) != find(y)) {
puts("No");
continue;
}
int z = LCA(x, y);
if ((dep[x] + dep[y]) & 1)
puts("Yes");
else if (sum[x] + sum[y] - sum[z] * 2 > 0)
puts("Yes");
else
puts("No");
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
template <bool Rank = true>
struct DsuSec {
map<int, int> r, sz;
vector<pair<int, int> > edges;
void clear() {
r.clear();
sz.clear();
edges.clear();
}
int root(int u) const {
const auto it = r.find(u);
if (it == r.end()) return u;
return it->second;
}
int size(int u) const {
const auto it = sz.find(u);
if (it == sz.end()) return 1;
return it->second;
}
int size() const { return edges.size(); }
int find(int u) {
if (u == root(u)) return u;
return r[u] = find(root(u));
}
int join(int u, int v) {
edges.push_back({u, v});
u = find(u);
v = find(v);
if (u == v) return 0;
if (Rank && size(u) > size(v)) std::swap(u, v);
r[u] = v;
sz[v] = size(u) + size(v);
return 1;
}
void join_bipartite(int u, int v) {
join(2 * u, 2 * v + 1);
join(2 * v, 2 * u + 1);
}
bool is_bipartite() {
if (r.empty()) return true;
int u = r.begin()->first;
return find(u) != find(u ^ 1);
}
void swap(DsuSec& rhs) {
r.swap(rhs.r);
sz.swap(rhs.sz);
edges.swap(rhs.edges);
}
void merge(DsuSec& rhs) {
if (rhs.size() > size()) this->swap(rhs);
for (pair<int, int> e : rhs.edges) join(e.first, e.second);
}
};
const int N = 5e5 + 10;
DsuSec<false> dsu;
DsuSec<true> sec[N];
int n, m, nz;
vector<pair<int, int> > queries;
bool in_tree[N], vis[N];
vector<int> adj[N], t[N];
int p[N], L[N], color[N];
void add_edge(int u, int v) {
adj[u].push_back(v);
adj[v].push_back(u);
}
void dfs(int u, int pr = -1) {
p[u] = pr;
for (int v : adj[u]) {
if (v == pr) continue;
L[v] = L[u] + 1;
dfs(v, u);
}
}
void dfst(int u, int pr = -1) {
vis[u] = 1;
for (int v : t[u]) {
if (v == pr) continue;
color[v] = color[u] ^ 1;
dfst(v, u);
}
}
bool is_block(int u) { return u >= n; }
int join_block(int u, int p) {
u = dsu.find(u);
p = dsu.find(p);
assert(u != p);
int res = !sec[u].is_bipartite();
dsu.join(u, p);
sec[p].merge(sec[u]);
return res;
}
const int LOGN = 20;
int tempo;
bool odd[N];
int P[N][LOGN], odds[N][LOGN];
int sta[N], nd[N];
void yandex_dfs(int u, int p = -1) {
sta[u] = ++tempo;
P[u][0] = p;
odds[u][0] = odd[u];
for (int i = 1; i < LOGN; i++)
if (P[u][i - 1] != -1)
P[u][i] = P[P[u][i - 1]][i - 1],
odds[u][i] = odds[P[u][i - 1]][i - 1] | odds[u][i - 1];
for (int v : t[u])
if (!sta[v]) yandex_dfs(v, u);
nd[u] = tempo;
}
bool anc(int p, int x) { return sta[p] <= sta[x] && sta[x] <= nd[p]; }
int LCA(int u, int v) {
if (anc(u, v)) return u;
for (int i = LOGN - 1; i >= 0; i--) {
if (P[u][i] != -1 && !anc(P[u][i], v)) u = P[u][i];
}
if (P[u][0] != -1 && anc(P[u][0], v)) return P[u][0];
return -1;
}
int lift(int u, int p) {
if (u == p) return odd[u];
int res = 0;
for (int i = LOGN - 1; i >= 0; i--) {
if (P[u][i] != -1 && !anc(P[u][i], p)) {
res |= odds[u][i];
u = P[u][i];
}
}
return res | odds[u][1];
}
void yandex() {
for (int i = 0; i < nz; i++) t[i].clear();
for (int i = 0; i < nz; i++) odd[i] = !sec[dsu.find(i)].is_bipartite();
for (int i = 0; i < nz; i++)
for (int v : adj[i]) t[dsu.find(i)].push_back(dsu.find(v));
memset(P, -1, sizeof P);
for (int i = 0; i < nz; i++) {
if (!sta[i]) yandex_dfs(i);
}
int q;
cin >> q;
while (q--) {
int a, b;
cin >> a >> b;
--a, --b;
int w = LCA(a, b);
if (w == -1)
cout << "No"
<< "\n";
else if (lift(a, w) || lift(b, w) || color[a] != color[b])
cout << "Yes"
<< "\n";
else
cout << "No"
<< "\n";
}
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
nz = n;
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
--x, --y;
if (dsu.join(x, y)) {
in_tree[i] = true;
add_edge(x, nz);
add_edge(nz, y);
t[x].push_back(y);
t[y].push_back(x);
sec[nz].join_bipartite(x, y);
nz++;
}
queries.push_back({x, y});
}
memset(p, -1, sizeof p);
for (int i = 0; i < nz; i++)
if (p[i] == -1) dfs(i);
memset(vis, 0, sizeof vis);
for (int i = 0; i < nz; i++)
if (!vis[i]) dfst(i);
dsu.clear();
for (int i = 0; i < m; i++) {
if (in_tree[i]) {
continue;
}
int u, v;
tie(u, v) = queries[i];
if (u == v) {
continue;
}
int parity = color[u] ^ color[v];
vector<int> blocks;
u = dsu.find(u);
v = dsu.find(v);
while (u != v) {
if (L[u] < L[v]) swap(u, v);
if (is_block(u)) blocks.push_back(u);
u = dsu.find(p[u]);
}
if (is_block(u)) blocks.push_back(u);
assert(!blocks.empty());
for (int i = 1; i < (int)blocks.size(); i++)
if (L[blocks[i]] < L[blocks[0]]) swap(blocks[i], blocks[0]);
int acc = !sec[blocks[0]].is_bipartite();
for (int i = 1; i < (int)blocks.size(); i++)
acc |= join_block(blocks[i], blocks[0]);
sec[blocks[0]].join_bipartite(queries[i].first, queries[i].second);
if (acc) {
} else {
string res = "00";
res[parity] = '1';
}
}
yandex();
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int n, m, q, len, en = 0, num = 0, top = 0, to[100100 << 1], r[100100 << 1],
l[100100], st[100100], odd[100100], dfn[100100], low[100100],
vis[100100], f[100100][18], d[100100], cnt[100100],
con[100100];
void add(int x, int y) { to[++en] = y, r[en] = l[x], l[x] = en; }
void tarjan(int x, int fa) {
st[++top] = x, dfn[x] = low[x] = ++num;
f[x][0] = fa, d[x] = d[fa] + 1, vis[x] = 1;
for (int i = l[x]; i; i = r[i]) {
int y = to[i];
if (y == fa) continue;
if (!dfn[y])
tarjan(y, x), low[x] = min(low[x], low[y]);
else if (vis[y]) {
low[x] = min(low[x], dfn[y]);
if ((d[x] & 1) == (d[y] & 1)) odd[x] = 1;
}
}
if (dfn[x] == low[x]) {
int F = 0, TOP = top;
while (st[TOP] != x) {
if (odd[st[TOP]]) F = 1;
TOP--;
}
if (F || odd[x])
for (int i = TOP + 1; i <= top; i++) cnt[st[i]]++;
while (st[top] != x) vis[st[top]] = 0, con[st[top]] = x, top--;
con[x] = x, top--, vis[x] = 0;
}
}
int lca(int x, int y) {
if (d[x] < d[y]) swap(x, y);
int D = d[x] - d[y];
for (int i = len; i >= 0; i--)
if (D & (1 << i)) x = f[x][i];
if (x == y) return x;
for (int i = len; i >= 0; i--)
if (f[x][i] != f[y][i]) x = f[x][i], y = f[y][i];
return f[x][0];
}
void dfs(int x) {
for (int i = l[x]; i; i = r[i])
if (f[to[i]][0] == x) cnt[to[i]] += cnt[x], dfs(to[i]);
}
int main() {
scanf("%d %d", &n, &m);
int x, y;
len = (int)log2(n);
for (int i = 1; i <= m; i++) scanf("%d %d", &x, &y), add(x, y), add(y, x);
for (int i = 1; i <= n; i++) f[i][0] = i;
for (int i = 1; i <= n; i++)
if (!dfn[i]) tarjan(i, 0);
for (int j = 1; j <= len; j++)
for (int i = 1; i <= n; i++) f[i][j] = f[f[i][j - 1]][j - 1];
for (int i = 1; i <= n; i++)
if (!f[i][0]) dfs(i);
scanf("%d", &q);
while (q--) {
scanf("%d %d", &x, &y);
int z = lca(x, y);
if (!z) {
printf("No\n");
continue;
}
if (((d[x] + d[y] - 2 * d[z]) & 1) || (cnt[x] + cnt[y] > 2 * cnt[z]))
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const long long inf = 0x7f7f7f7f7f7f;
long long mod;
const long double eps = 1e-8;
inline long long add(long long x) { return x >= mod ? x - mod : x; }
inline long long sub(long long x) { return x < 0 ? x + mod : x; }
inline void Add(long long &x, long long y) {
if ((x += y) >= mod) x -= mod;
}
inline void Sub(long long &x, long long y) {
if ((x -= y) < 0) x += mod;
}
long long read() {
long long x = 0, f = 1;
char c = getchar();
while (!isdigit(c)) {
if (c == '-') f = -1;
c = getchar();
}
while (isdigit(c)) {
x = (x << 3) + (x << 1) + (c ^ 48);
c = getchar();
}
return x * f;
}
const long long N = 1e5 + 10;
long long n, m, q;
long long fa[N];
long long bz[18][N];
long long dep[N], s[N], a[N];
std::vector<long long> e[N];
inline long long find_r(long long x) {
return x == fa[x] ? x : fa[x] = find_r(fa[x]);
}
long long head[N], to[N + N], nxt[N + N], tot;
void add(long long a, long long b) {
to[++tot] = b;
nxt[tot] = head[a];
head[a] = tot;
}
void dfs1(long long u) {
dep[u] = dep[bz[0][u]] + 1;
for (long long i = head[u]; i; i = nxt[i]) {
long long v = to[i];
if (!dep[v]) {
bz[0][v] = u;
dfs1(v);
if (find_r(u) == find_r(v)) a[u] |= a[v];
} else if (dep[v] < dep[u] - 1) {
if ((dep[u] + dep[v] + 1) & 1) a[u] = 1;
for (long long x = find_r(u); dep[x] > dep[v] + 1; x = find_r(x))
fa[x] = bz[0][x];
}
}
}
void dfs2(long long u) {
s[u] += a[u];
for (long long i = head[u]; i; i = nxt[i]) {
long long v = to[i];
if (dep[v] == dep[u] + 1) {
if (find_r(u) == find_r(v)) a[v] |= a[u];
s[v] = s[u];
dfs2(v);
}
}
}
long long LCA(long long x, long long y) {
if (dep[x] < dep[y]) swap(x, y);
for (long long i = (16), iE = (0); i >= iE; i--)
if (dep[bz[i][x]] >= dep[y]) x = bz[i][x];
if (x == y) return x;
for (long long i = (16), iE = (0); i >= iE; i--)
if (bz[i][x] != bz[i][y]) x = bz[i][x], y = bz[i][y];
return bz[0][x];
}
signed main() {
n = read(), m = read();
for (long long i = (1), iE = (m); i <= iE; i++) {
long long u = read(), v = read();
add(u, v), add(v, u);
e[u].push_back(v), e[v].push_back(u);
}
for (long long i = (1), iE = (n); i <= iE; i++) fa[i] = i;
for (long long i = (1), iE = (n); i <= iE; i++)
if (!dep[i]) dfs1(i), dfs2(i);
for (long long j = (1), jE = (16); j <= jE; j++)
for (long long i = (1), iE = (n); i <= iE; i++)
bz[j][i] = bz[j - 1][bz[j - 1][i]];
q = read();
while (q--) {
long long u = read(), v = read();
long long l_a = LCA(u, v);
if (!l_a)
puts("No");
else if (((dep[u] + dep[v]) & 1) || s[u] + s[v] - 2 * s[l_a])
puts("Yes");
else
puts("No");
}
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 500005;
int tot = 1;
int ver[N << 1], nxt[N << 1], head[N << 1];
int dfn[N], low[N], f[N][19], rt[N];
int vis[N], st[N], top, v[N], sum[N], is[N];
vector<int> e[N], dcc[N];
int cnt, num, dep[N], bel[N], c[N], n, m, q;
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
return x * f;
}
void add(int x, int y) {
ver[++tot] = y;
nxt[tot] = head[x];
head[x] = tot;
}
void tarjan(int x, int fr) {
dfn[x] = low[x] = ++num;
dep[x] = dep[ver[fr ^ 1]] + 1;
f[x][0] = ver[fr ^ 1];
rt[x] = rt[ver[fr ^ 1]];
for (int i = 1; i <= 18; i++) f[x][i] = f[f[x][i - 1]][i - 1];
for (int i = head[x]; i; i = nxt[i]) {
int y = ver[i];
if (vis[i] || i == (fr ^ 1)) continue;
st[++top] = i;
if (!dfn[y]) {
tarjan(y, i);
low[x] = min(low[x], low[y]);
if (dfn[x] <= low[y]) {
++cnt;
int z;
dcc[cnt].push_back(x);
do {
z = st[top--];
bel[z] = bel[z ^ 1] = cnt;
vis[z] = vis[z ^ 1] = 1;
dcc[cnt].push_back(ver[z]);
e[cnt].push_back(z);
} while (z != i);
}
} else
low[x] = min(low[x], dfn[y]);
}
}
void dfs(int x, int fa) {
vis[x] = 1;
for (int i = head[x]; i; i = nxt[i]) {
int y = ver[i];
if (vis[y] || (i == (fa ^ 1))) continue;
sum[y] = sum[x] + is[i];
dfs(y, i);
}
}
bool dfs2(int x, int fa) {
if (vis[x]) {
if (c[x] == c[ver[fa ^ 1]])
return 1;
else
return 0;
}
vis[x] = 1;
c[x] = c[ver[fa ^ 1]] ^ 1;
for (int i = head[x]; i; i = nxt[i]) {
int y = ver[i];
if (i == (fa ^ 1) || bel[i] != bel[fa]) continue;
if (dfs2(y, i)) return 1;
}
return 0;
}
int LCA(int x, int y) {
if (dep[x] > dep[y]) swap(x, y);
for (int i = 18; i >= 0; i--) {
if (dep[f[y][i]] >= dep[x]) y = f[y][i];
}
if (x == y) return x;
for (int i = 18; i >= 0; i--) {
if (f[x][i] != f[y][i]) x = f[x][i], y = f[y][i];
}
return f[x][0];
}
int main() {
n = read();
m = read();
for (int i = 1; i <= m; i++) {
int u, v;
u = read();
v = read();
add(u, v);
add(v, u);
}
for (int i = 1; i <= n; i++) {
if (!dfn[i]) tarjan(i, 0), rt[0] = i;
}
memset(vis, 0, sizeof(vis));
for (int i = 1; i <= cnt; i++) {
for (int j = 0; j < dcc[i].size(); j++) {
vis[dcc[i][j]] = 0;
vis[dcc[i][j] ^ 1] = 0;
}
if (dfs2(ver[e[i][0]], e[i][0])) {
for (int j = 0; j < e[i].size(); j++) {
is[e[i][j]] = is[e[i][j] ^ 1] = 1;
}
}
}
memset(vis, 0, sizeof(vis));
for (int i = 1; i <= n; i++) {
if (!vis[i]) dfs(i, 0);
}
q = read();
while (q--) {
int u, v;
u = read();
v = read();
if (rt[u] != rt[v])
puts("No");
else {
int l = LCA(u, v);
if ((dep[u] + dep[v] - 2 * dep[l]) & 1)
puts("Yes");
else if (sum[u] + sum[v] - 2 * sum[l])
puts("Yes");
else
puts("No");
}
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 100111;
const int MAXM = 100111;
int N, M;
int F[MAXN];
int Find(int a) { return F[a] == a ? a : F[a] = Find(F[a]); }
struct Vert {
int FE;
int Dep;
int Cnt;
int Dps;
int Dfn, Low;
bool Vis;
} V[MAXN];
struct Edge {
int x, y, next, neg;
bool u, odd;
int Bel;
} E[MAXM << 1];
int Ecnt;
void addE(int a, int b) {
++Ecnt;
E[Ecnt].x = a;
E[Ecnt].y = b;
E[Ecnt].next = V[a].FE;
V[a].FE = Ecnt;
E[Ecnt].neg = Ecnt + 1;
++Ecnt;
E[Ecnt].y = a;
E[Ecnt].x = b;
E[Ecnt].next = V[b].FE;
V[b].FE = Ecnt;
E[Ecnt].neg = Ecnt - 1;
}
int DFN;
void DFS(int at) {
V[at].Dps = DFN;
V[at].Vis = true;
for (int k = V[at].FE, to; k > 0; k = E[k].next) {
to = E[k].y;
if (V[to].Vis) continue;
E[k].u = true;
E[E[k].neg].u = true;
V[to].Dep = V[at].Dep + 1;
DFS(to);
}
}
int St[MAXM << 1], Top;
int Bcnt;
bool Odd[MAXM << 1];
void PBC(int at, int e = 0) {
++DFN;
V[at].Low = V[at].Dfn = DFN;
for (int k = V[at].FE, to; k > 0; k = E[k].next) {
if (k == E[e].neg) continue;
to = E[k].y;
if (V[to].Dfn) {
if (V[to].Dfn < V[at].Dfn) St[++Top] = k;
V[at].Low = min(V[at].Low, V[to].Dfn);
} else {
St[++Top] = k;
PBC(to, k);
V[at].Low = min(V[at].Low, V[to].Low);
}
}
if (V[at].Low >= V[E[e].x].Dfn) {
++Bcnt;
while (Top > 1 && St[Top] != e) {
E[St[Top]].Bel = Bcnt;
--Top;
}
E[St[Top]].Bel = Bcnt;
--Top;
}
}
void Push(int at) {
V[at].Vis = true;
for (int k = V[at].FE, to; k > 0; k = E[k].next) {
to = E[k].y;
if (V[to].Vis) continue;
V[to].Cnt = V[at].Cnt + E[k].odd;
Push(to);
if (!E[k].odd) F[to] = at;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin >> N >> M;
for (int i = 1, a, b; i <= M; ++i) {
cin >> a >> b;
addE(a, b);
}
for (int i = 1; i <= N; ++i)
if (!V[i].Vis) {
++DFN;
DFS(i);
}
for (int i = 1, a, b; i <= Ecnt; ++i) {
if (E[i].u) continue;
a = E[i].x;
b = E[i].y;
if (V[a].Dep < V[b].Dep) swap(a, b);
if ((V[a].Dep - V[b].Dep) & 1) continue;
E[i].odd = true;
}
for (int i = 1; i <= N; ++i)
if (!V[i].Dfn) {
Top = 0;
PBC(i);
}
for (int i = 1; i <= Ecnt; ++i)
if (E[i].Bel && E[i].odd) Odd[E[i].Bel] = true;
for (int i = 1; i <= Ecnt; ++i)
if (Odd[E[i].Bel]) E[i].odd = true;
for (int i = 1; i <= Ecnt; ++i)
if (E[E[i].neg].odd) E[i].odd = true;
for (int i = 1; i <= N; ++i) V[i].Vis = false;
for (int i = 1; i <= N; ++i) F[i] = i;
for (int i = 1; i <= N; ++i)
if (!V[i].Vis) Push(i);
int Qcnt;
cin >> Qcnt;
for (int i = 1, a, b; i <= Qcnt; ++i) {
cin >> a >> b;
if (V[a].Dps != V[b].Dps)
puts("No");
else {
if (V[a].Dep < V[b].Dep) swap(a, b);
if ((V[a].Dep - V[b].Dep) & 1)
puts("Yes");
else if (Find(a) != Find(b))
puts("Yes");
else
puts("No");
}
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
namespace IO {
char buf[1000000], *p1, *p2;
inline char getc() {
if (p1 == p2) p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin);
return p1 == p2 ? EOF : *(p1++);
}
template <typename tp>
inline void r(tp &n) {
n = 0;
char c = getc();
while (!isdigit(c)) c = getc();
while (isdigit(c)) n = n * 10 + c - 48, c = getc();
}
template <typename tp>
inline void w(tp n) {
if (n / 10) w(n / 10);
putchar(n % 10 + 48);
}
}; // namespace IO
using namespace IO;
const int N = 1e5 + 5, M = 1e5 + 5;
using namespace std;
int n, m, q, num, tot, top;
int dep[N], dis[N], dfn[N], low[N], col[N], stk[N], odd[N], cur[N], f[N][21];
int head[N], nt[M << 1], to[M << 1];
void Add(int x, int y) {
++num;
nt[num] = head[x];
head[x] = num;
to[num] = y;
++num;
nt[num] = head[y];
head[y] = num;
to[num] = x;
}
int ins[N];
void Tar(int p, int fa) {
col[p] = col[fa];
dep[p] = dep[fa] + 1;
dfn[p] = low[p] = ++tot;
stk[++top] = p;
ins[p] = 1;
for (int i = head[p]; i; i = nt[i])
if (to[i] ^ fa) {
int v = to[i];
if (!dfn[v]) {
f[v][0] = p;
for (int i = 1; i <= 20; ++i) f[v][i] = f[f[v][i - 1]][i - 1];
Tar(v, p);
low[p] = min(low[p], low[v]);
} else if (ins[v])
low[p] = min(low[p], dfn[v]), cur[p] |= (!(dep[p] + dep[v] & 1));
}
if (dfn[p] == low[p]) {
int flg = 0, t = top;
while (stk[t] != p && !flg) flg = cur[stk[t]], --t;
while (stk[top] != p) {
odd[stk[top]] |= flg;
ins[stk[top]] = 0;
--top;
}
--top, ins[p] = 0;
}
}
void DFS(int p) {
dis[p] += odd[p];
for (int i = head[p]; i; i = nt[i])
if (dep[to[i]] == dep[p] + 1) dis[to[i]] = dis[p], DFS(to[i]);
}
int LCA(int x, int y) {
if (dep[x] < dep[y]) x ^= y ^= x ^= y;
for (int i = 20; i >= 0; --i)
if (dep[f[x][i]] >= dep[y]) x = f[x][i];
if (x == y) return x;
for (int i = 20; i >= 0; --i)
if (f[x][i] != f[y][i]) x = f[x][i], y = f[y][i];
return f[x][0];
}
int DIS(int x, int y) { return dis[x] + dis[y] - 2 * dis[LCA(x, y)]; }
bool check(int x, int y) {
if (col[x] != col[y] || x == y) return false;
if (dep[x] + dep[y] & 1) return true;
return DIS(x, y) > 0;
}
int main() {
r(n), r(m);
for (int i = 1, x, y; i <= m; ++i) r(x), r(y), Add(x, y);
for (int i = 1; i <= n; ++i)
if (!dfn[i]) col[i] = i, Tar(i, i), DFS(i);
r(q);
for (int i = 1, x, y; i <= q; ++i)
r(x), r(y), puts(check(x, y) ? "Yes" : "No");
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int MXN = 100005;
const int LOG = 17;
struct DisjointSet {
int fa[MXN];
void init(int n) {
for (int i = (0); i <= (n); i++) fa[i] = i;
}
int f(int x) { return x == fa[x] ? x : fa[x] = f(fa[x]); }
void uni(int x, int y) { fa[f(y)] = f(x); }
} djs, con;
int N, M;
int fa[MXN], dep[MXN], inc[MXN], cnt[MXN], vst[MXN];
vector<int> E[MXN], child[MXN];
vector<pair<int, int> > backedge;
int up[LOG][MXN];
void DFS(int u, int f, int d) {
vst[u] = 1;
fa[u] = f;
up[0][u] = f;
dep[u] = d;
for (auto v : E[u]) {
if (vst[v]) {
if (f != v) backedge.push_back({u, v});
continue;
}
DFS(v, u, d + 1);
child[u].push_back(v);
}
}
void DFS2(int u) {
vst[u] = 1;
if (inc[djs.f(u)] and djs.f(u) != u) cnt[u]++;
for (auto v : child[u]) {
assert(!vst[v]);
cnt[v] = cnt[u];
DFS2(v);
}
}
int lca(int u, int v) {
if (dep[u] > dep[v]) swap(u, v);
int k = dep[v] - dep[u];
for (int i = 0; i < (LOG); i++)
if (k & (1 << i)) {
v = up[i][v];
}
if (u == v) return u;
assert(dep[u] == dep[v]);
for (int i = LOG - 1; i >= 0; i--) {
if (up[i][u] != up[i][v]) {
u = up[i][u];
v = up[i][v];
}
}
return fa[u];
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
;
cin >> N >> M;
con.init(N);
for (int i = 0; i < (M); i++) {
int u, v;
cin >> u >> v;
E[u].push_back(v);
E[v].push_back(u);
con.uni(u, v);
}
for (int i = (1); i <= (N); i++)
if (!vst[i]) {
DFS(i, i, 0);
}
for (int i = (1); i <= (LOG - 1); i++)
for (int j = (1); j <= (N); j++) {
up[i][j] = up[i - 1][up[i - 1][j]];
}
djs.init(N);
for (auto it : backedge) {
int u = it.first, v = it.second;
int l = lca(u, v);
if ((dep[u] + dep[v]) % 2 == 0) inc[l] = 1;
u = djs.f(u);
v = djs.f(v);
while (u != v) {
if (dep[u] > dep[v]) swap(u, v);
djs.uni(fa[v], v);
v = djs.f(v);
}
assert(dep[djs.f(u)] <= dep[djs.f(l)]);
assert(dep[djs.f(v)] <= dep[djs.f(l)]);
}
for (int i = (1); i <= (N); i++)
if (inc[i] and djs.f(i) != i) {
inc[djs.f(i)] = 1;
inc[i] = 0;
}
for (int i = (1); i <= (N); i++) vst[i] = 0;
for (int i = (1); i <= (N); i++)
if (!vst[i]) {
DFS2(i);
}
int Q;
cin >> Q;
for (int _ = 0; _ < (Q); _++) {
int u, v;
cin >> u >> v;
int ans = 0;
int f = lca(u, v);
if (cnt[u] - cnt[f] or cnt[v] - cnt[f]) ans = 1;
if ((dep[u] + dep[v]) % 2 == 1) ans = 1;
if (con.f(u) != con.f(v)) ans = 0;
if (u == v) ans = 0;
if (ans)
cout << "Yes\n";
else
cout << "No\n";
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
namespace IO {
char ibuf[(1 << 21) + 1], *iS, *iT;
char Get() {
return (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, (1 << 21) + 1, stdin),
(iS == iT ? EOF : *iS++))
: *iS++);
}
int read() {
int x = 0, c = Get();
while (!isdigit(c)) c = Get();
while (isdigit(c)) x = x * 10 + c - 48, c = Get();
return x;
}
} // namespace IO
using IO::read;
const int N = 100007;
std::vector<int> e[N];
int t, cnt, top, dep[N], fa[N][17], root[N], sum[N], dfn[N], low[N], stk[N],
ins[N], bel[N], is[N];
void dfs(int u) {
for (int i = 1; i <= 16; ++i) fa[u][i] = fa[fa[u][i - 1]][i - 1];
for (int v : e[u])
if (!dep[v]) fa[v][0] = u, dep[v] = dep[u] + 1, root[v] = root[u], dfs(v);
}
int lca(int u, int v) {
if (dep[u] < dep[v]) std::swap(u, v);
for (int i = 16; ~i; --i)
if (dep[fa[u][i]] >= dep[v]) u = fa[u][i];
for (int i = 16; ~i; --i)
if (fa[u][i] ^ fa[v][i]) u = fa[u][i], v = fa[v][i];
return u ^ v ? fa[u][0] : u;
}
void dfs2(int u) {
for (int v : e[u])
if (fa[v][0] == u) sum[v] += sum[u], dfs2(v);
}
void tarjan(int u, int fa) {
dfn[u] = low[u] = ++t, stk[++top] = u, ins[u] = 1;
for (int v : e[u])
if (v ^ fa)
!dfn[v] ? (tarjan(v, u), low[u] = std::min(low[u], low[v]))
: low[u] = std::min(low[u], dfn[v]);
if (dfn[u] == low[u])
for (++cnt; stk[top + 1] ^ u;) ins[stk[top]] = 0, bel[stk[top--]] = cnt;
}
int main() {
int n = read(), m = read();
for (int i = 1, u, v; i <= m; ++i)
u = read(), v = read(), e[u].push_back(v), e[v].push_back(u);
for (int u = 1; u <= n; ++u)
if (!dep[u]) dep[u] = 1, dfs(root[u] = u);
for (int u = 1; u <= n; ++u)
if (dep[u] == 1) tarjan(u, 0);
for (int u = 1; u <= n; ++u)
if (!is[bel[u]])
for (int v : e[u])
if (!((dep[u] + dep[v]) & 1) && bel[u] == bel[v]) {
is[bel[u]] = 1;
break;
}
for (int u = 1; u <= n; ++u)
if (fa[u][0] && bel[u] == bel[fa[u][0]] && is[bel[u]]) ++sum[u];
for (int u = 1; u <= n; ++u)
if (dep[u] == 1) dfs2(u);
for (int q = read(), u, v; q; --q)
u = read(), v = read(),
puts(root[u] ^ root[v] || u == v ||
(!((dep[u] + dep[v]) & 1) &&
sum[u] + sum[v] == 2 * sum[lca(u, v)])
? "No"
: "Yes");
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
pair<int, int> edges[200105];
int C[200105], D[200105], temp[200105], sum[200105], num[200105], low[200105],
disc[200105], vis[200105], cno, comp[200105], pa[19][200105], artic[200105],
tim;
stack<int> stk;
unordered_set<int> s[200105];
vector<pair<int, int> > g[200105];
vector<int> t[200105];
void put() {
s[cno].insert(edges[stk.top()].first);
s[cno].insert(edges[stk.top()].second);
stk.pop();
}
void setTraverse() {
for (int j : s[cno]) {
if (artic[j]) {
t[cno].push_back(comp[j]);
t[comp[j]].push_back(cno);
} else
comp[j] = cno;
}
}
void dfs(int a, int p, int f = 1) {
low[a] = disc[a] = ++tim;
temp[a] = f;
vis[a] = 1;
int child = 0;
for (pair<int, int> i : g[a]) {
if (!vis[i.first]) {
child++;
stk.push(i.second);
dfs(i.first, a, f ^ 1);
low[a] = min(low[a], low[i.first]);
if ((disc[a] == 1 && child > 1) ||
(disc[a] != 1 && low[i.first] >= disc[a])) {
cno++;
artic[a] = 1;
if (comp[a] == 0) {
comp[a] = cno;
s[cno].insert(a);
cno++;
s[cno].insert(a);
}
while (stk.top() != i.second) put();
put();
setTraverse();
}
} else if (i.first != p && disc[i.first] < low[a]) {
low[a] = min(low[a], disc[i.first]);
stk.push(i.second);
}
}
}
void buildTree(int n) {
for (__typeof(n) j = 1; j <= n; j++) {
if (!vis[j]) {
tim = 0;
dfs(j, j);
if (!stk.empty()) {
cno++;
while (!stk.empty()) put();
setTraverse();
}
}
}
}
void dfs2(int a, int p, int c) {
vis[a] = 1;
sum[a] += num[a];
pa[0][a] = p;
C[a] = c;
for (int i : t[a]) {
if (!vis[i]) {
D[i] = D[a] + 1;
sum[i] = sum[a];
dfs2(i, a, c);
}
}
}
int lca(int a, int b) {
if (D[b] > D[a]) swap(a, b);
int diff = D[a] - D[b];
for (int i = 0; i < 19; i++)
if (diff & (1 << i)) a = pa[i][a];
if (a == b) return a;
for (int i = 19 - 1; i >= 0; i--)
if (pa[i][a] != pa[i][b]) {
a = pa[i][a];
b = pa[i][b];
}
return pa[0][a];
}
int main() {
int n, m, q;
scanf("%d %d", &n, &m);
for (__typeof(m) i = 0; i < m; i++) {
int a, b;
scanf("%d %d", &a, &b);
edges[i + 1] = {a, b};
g[a].emplace_back(b, i + 1);
g[b].emplace_back(a, i + 1);
}
scanf("%d", &q);
buildTree(n);
memset(vis, 0, sizeof vis);
for (int i = 1; i <= cno; i++) {
for (int j : s[i]) {
for (pair<int, int> k : g[j]) {
if (s[i].count(k.first) && temp[j] == temp[k.first]) {
num[i] = 1;
break;
}
}
if (num[i]) break;
}
}
for (__typeof(cno) i = 1; i <= cno; i++) {
if (!vis[i]) {
dfs2(i, 0, i);
}
}
for (int i = 1; i < 19; i++) {
for (int j = 1; j <= cno; j++) pa[i][j] = pa[i - 1][pa[i - 1][j]];
}
for (__typeof(q) i = 0; i < q; i++) {
int a, b;
scanf("%d %d", &a, &b);
int k = 0;
if (C[comp[a]] != C[comp[b]] || a == b)
k = 0;
else if (temp[a] ^ temp[b])
k = 1;
else {
a = comp[a];
b = comp[b];
int c = lca(a, b);
if (sum[a] + sum[b] - 2 * sum[c] + num[c])
k = 1;
else
k = 0;
}
if (k)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0, p = 1;
char c = getchar();
for (; !isdigit(c); c = getchar())
if (c == '-') p = -1;
for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
return x *= p;
}
template <typename T>
inline bool chkmin(T &a, T b) {
return a > b ? a = b, 1 : 0;
}
template <typename T>
inline bool chkmax(T &a, T b) {
return a < b ? a = b, 1 : 0;
}
inline void File() {}
const int N = 1e5 + 10;
int n, m, low[N], clk, dfn[N], dep[N];
int fa[20][N], cnt, vis[N], ans[N], u, v, tp;
int e = 1, beg[N], nex[N << 1], to[N << 1], id[N];
pair<int, int> sta[N << 1];
inline void add(int x, int y) {
to[++e] = y, nex[e] = beg[x], beg[x] = e;
to[++e] = x, nex[e] = beg[y], beg[y] = e;
}
inline void dfs(int u, int f) {
id[u] = cnt, dep[u] = dep[fa[0][u] = f] + 1;
for (int i = 1; i <= 18; ++i) fa[i][u] = fa[i - 1][fa[i - 1][u]];
for (int i = beg[u], v = to[i]; i; v = to[i = nex[i]])
if (!id[v]) dfs(v, u);
}
inline void Tarjan(int u, int f) {
dfn[u] = low[u] = ++clk;
for (int i = beg[u], v = to[i]; i; v = to[i = nex[i]])
if (v != f) {
if (!dfn[v]) {
sta[++tp] = make_pair(u, v);
Tarjan(v, u);
chkmin(low[u], low[v]);
if (low[v] >= dfn[u]) {
int flag = 0, tmp = tp;
for (;;) {
int x = sta[tp].first, y = sta[tp].second;
--tp;
if ((dep[x] & 1) == (dep[y] & 1)) {
flag = 1;
break;
}
if (x == u && y == v) break;
}
if (!flag) continue;
tp = tmp;
for (;;) {
int x = sta[tp].first, y = sta[tp].second;
--tp, ans[x] = ans[y] = 1;
if (x == u && y == v) break;
}
ans[u] = 0;
}
} else if (dfn[v] < dfn[u]) {
sta[++tp] = make_pair(u, v);
chkmin(low[u], dfn[v]);
}
}
}
inline void update(int u, int f) {
vis[u] = 1, ans[u] += ans[f];
for (int i = beg[u], v = to[i]; i; v = to[i = nex[i]])
if (!vis[v]) update(v, u);
}
inline int lca(int x, int y) {
if (dep[x] < dep[y]) swap(x, y);
for (int i = 18; i >= 0; --i)
if (dep[fa[i][x]] >= dep[y]) x = fa[i][x];
if (x == y) return x;
for (int i = 18; i >= 0; --i)
if (fa[i][x] ^ fa[i][y]) x = fa[i][x], y = fa[i][y];
return fa[0][x];
}
inline bool check(int u, int v) {
if (id[u] ^ id[v]) return false;
if (abs(dep[u] - dep[v]) & 1) return true;
if ((ans[u] + ans[v] - ans[lca(u, v)] * 2) > 0) return true;
return false;
}
int main() {
n = read(), m = read();
for (int i = 1; i <= m; ++i) u = read(), v = read(), add(v, u);
for (int i = 1; i <= n; ++i)
if (!dfn[i]) ++cnt, dfs(i, 0), Tarjan(i, 0), update(i, 0);
for (int q = read(); q--;)
u = read(), v = read(), puts(check(u, v) ? "Yes" : "No");
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
struct Edge {
int v;
Edge *link;
} edge[400010 * 2], *adj[200010], *tree[200010 * 2];
int totE, con, root, currT, top, top1;
int totCut, totBi;
int dep[200010], deps[200010], num[200010], dfn[200010], lowlink[200010],
cut[200010], bi[200010], color[200010];
int temp[200010], st1[200010], st2[200010], st3[200010], st4[200010],
f[200010 * 2];
int used[200010 * 2], odd[200010 * 2];
int fa[200010][20];
int LOG[200010];
void addEdge(int u, int v) {
Edge *p = &edge[totE++];
p->v = v;
p->link = adj[u];
adj[u] = p;
}
void addTree(int u, int v) {
Edge *p = &edge[totE++];
p->v = v;
p->link = tree[u];
tree[u] = p;
}
void dfs(int u, int pre) {
dep[u] = dep[pre] + 1;
dfn[u] = lowlink[u] = ++currT;
num[u] = con;
Edge *p = adj[u];
int cnt = 0;
bool flag = false;
while (p) {
if (!dfn[p->v]) {
++cnt;
dfs(p->v, u);
lowlink[u] = min(lowlink[u], lowlink[p->v]);
if (lowlink[p->v] >= dfn[u]) flag = true;
} else if (p->v != pre)
lowlink[u] = min(lowlink[u], dfn[p->v]);
p = p->link;
}
if (flag && (u != root || cnt > 1)) cut[u] = ++totCut;
}
bool dfs2(int u) {
Edge *p = adj[u];
while (p) {
if (bi[p->v] == totBi) {
if (color[p->v] == -1) {
color[p->v] = !color[u];
if (!dfs2(p->v)) return false;
} else if (color[p->v] == color[u])
return false;
}
p = p->link;
}
return true;
}
void solve() {
++totBi;
int cnt = 0;
for (int i = 0; i < top1; ++i) {
int u = st3[i], v = st4[i];
bi[u] = totBi;
bi[v] = totBi;
color[u] = color[v] = -1;
if (cut[u]) temp[cnt++] = cut[u];
if (cut[v]) temp[cnt++] = cut[v];
}
sort(temp, temp + cnt);
for (int i = 0; i < cnt; ++i)
if (i == 0 || temp[i] != temp[i - 1]) {
addTree(temp[i], totCut + totBi);
addTree(totCut + totBi, temp[i]);
}
color[st3[0]] = 0;
odd[totBi] = !dfs2(st3[0]);
}
void dfs1(int u, int pre) {
dfn[u] = lowlink[u] = ++currT;
Edge *p = adj[u];
while (p) {
if (!dfn[p->v]) {
st1[top] = u;
st2[top++] = p->v;
dfs1(p->v, u);
lowlink[u] = min(lowlink[u], lowlink[p->v]);
if (lowlink[p->v] >= dfn[u]) {
top1 = 0;
while (true) {
--top;
st3[top1] = st1[top];
st4[top1++] = st2[top];
if (st1[top] == u && st2[top] == p->v) break;
}
solve();
}
} else {
if (dfn[u] > dfn[p->v] && p->v != pre) {
st1[top] = u;
st2[top++] = p->v;
}
if (p->v != pre) lowlink[u] = min(lowlink[u], dfn[p->v]);
}
p = p->link;
}
}
void dfs3(int u, int pre) {
deps[u] = deps[pre] + 1;
used[u] = true;
fa[u][0] = pre;
if (u <= totCut)
f[u] = 0;
else
f[u] = odd[u - totCut];
f[u] += f[pre];
Edge *p = tree[u];
while (p) {
if (!used[p->v]) dfs3(p->v, u);
p = p->link;
}
}
int LCA(int u, int v) {
if (deps[u] < deps[v]) swap(u, v);
int diff = deps[u] - deps[v];
for (int i = 0; diff; ++i, diff >>= 1)
if (diff & 1) u = fa[u][i];
if (u == v) return u;
int t = 19;
for (int i = t; i >= 0; --i) {
if (fa[u][i] != fa[v][i]) {
u = fa[u][i];
v = fa[v][i];
}
}
return fa[u][0];
}
int main() {
LOG[1] = 0;
for (int i = 2; i < 200010; ++i) LOG[i] = (LOG[i] >> 1) + 1;
int n, m;
scanf("%d%d", &n, &m);
for (int i = 0; i < m; ++i) {
int u, v;
scanf("%d%d", &u, &v);
addEdge(u, v);
addEdge(v, u);
}
dep[0] = -1;
for (int i = 1; i <= n; ++i)
if (!dfn[i]) {
root = i;
dfs(i, 0);
++con;
}
currT = 0;
memset(dfn, 0, sizeof(dfn));
for (int i = 1; i <= n; ++i)
if (!dfn[i]) dfs1(i, 0);
for (int i = 1; i <= totCut + totBi; ++i)
if (!used[i]) dfs3(i, 0);
for (int l = 1; (1 << l) <= totCut + totBi; ++l)
for (int i = 1; i <= totCut + totBi; ++i)
if (fa[i][l - 1] != 0) fa[i][l] = fa[fa[i][l - 1]][l - 1];
int q;
scanf("%d", &q);
for (int i = 0; i < q; ++i) {
int u, v;
scanf("%d%d", &u, &v);
if (u == v || num[u] != num[v])
puts("No");
else {
int x, y;
if (cut[u])
x = cut[u];
else
x = totCut + bi[u];
if (cut[v])
y = cut[v];
else
y = totCut + bi[v];
int lca = LCA(x, y);
int sum = f[x] + f[y] - 2 * f[fa[lca][0]];
if (sum > 0)
puts("Yes");
else if ((dep[u] + dep[v]) % 2 == 0)
puts("No");
else
puts("Yes");
}
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
namespace zyt {
template <typename T>
inline void read(T &x) {
char c;
bool f = false;
x = 0;
do c = getchar();
while (c != '-' && !isdigit(c));
if (c == '-') f = true, c = getchar();
do x = x * 10 + c - '0', c = getchar();
while (isdigit(c));
if (f) x = -x;
}
template <typename T>
inline void write(T x) {
static char buf[20];
char *pos = buf;
if (x < 0) putchar('-'), x = -x;
do *pos++ = x % 10 + '0';
while (x /= 10);
while (pos > buf) putchar(*--pos);
}
inline void write(const char *const s) { printf("%s", s); }
const int N = 1e5 + 10, M = 1e5 + 10, B = 18;
int n, m, ecnt, head[N];
struct edge {
int to, next;
} e[M << 1];
bool mark[M << 1];
int dfn[N], dfncnt, euler[N << 1], dep[N];
inline void add(const int a, const int b) {
e[ecnt] = (edge){b, head[a]}, head[a] = ecnt++;
}
namespace BCC {
int bcc, top, belong[M << 1], stack[M << 1], low[N];
bool vis[M << 1];
vector<int> own[N], ownpt[N];
void Tarjan(const int u, const int from) {
dfn[u] = low[u] = ++dfncnt;
euler[dfncnt] = u;
dep[u] = (~from ? dep[e[from ^ 1].to] + 1 : 1);
for (int i = head[u]; ~i; i = e[i].next) {
int v = e[i].to;
if (vis[i] || (~from && i == (from ^ 1))) continue;
stack[top++] = i;
if (dfn[v])
low[u] = min(low[u], dfn[v]);
else {
Tarjan(v, i), low[u] = min(low[u], low[v]);
euler[++dfncnt] = u;
if (dfn[u] <= low[v]) {
int t;
++bcc;
ownpt[bcc].push_back(u);
do {
t = stack[--top];
belong[t] = belong[t ^ 1] = bcc;
vis[t] = vis[t ^ 1] = true;
own[bcc].push_back(t);
ownpt[bcc].push_back(e[i].to);
} while (t != i);
}
}
}
}
void solve() {
for (int i = 1; i <= n; i++)
if (!dfn[i]) Tarjan(i, -1);
}
} // namespace BCC
namespace ST {
int lg2[N << 1], st[N << 1][B];
inline int _min(const int a, const int b) { return dfn[a] < dfn[b] ? a : b; }
void build() {
int tmp = 0;
for (int i = 1; i <= dfncnt; i++) {
lg2[i] = tmp;
if (i == (1 << (tmp + 1))) ++tmp;
}
for (int i = dfncnt; i > 0; i--) {
st[i][0] = euler[i];
for (int j = 1; j < B; j++)
if (i + (1 << j) - 1 <= dfncnt)
st[i][j] = _min(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
else
break;
}
}
inline int query(const int a, const int b) {
int len = b - a + 1;
return _min(st[a][lg2[len]], st[b - (1 << lg2[len]) + 1][lg2[len]]);
}
} // namespace ST
inline int lca(const int a, const int b) {
return ST::query(min(dfn[a], dfn[b]), max(dfn[a], dfn[b]));
}
int p[N];
int f(const int x) { return x == p[x] ? x : p[x] = f(p[x]); }
bool vis[N], col[N];
int w[N];
bool dfs(const int u, const int from) {
using BCC::belong;
if (vis[u]) {
if (!vis[e[from ^ 1].to]) fprintf(stderr, "%dgg", from);
if (col[u] == col[e[from ^ 1].to])
return true;
else
return false;
}
vis[u] = true, col[u] = (col[e[from ^ 1].to] ^ 1);
for (int i = head[u]; ~i; i = e[i].next) {
int v = e[i].to;
if (belong[i] != belong[from] || i == (from ^ 1)) continue;
if (dfs(v, i)) return true;
}
return false;
}
void dfs2(const int u, const int from) {
vis[u] = true;
for (int i = head[u]; ~i; i = e[i].next) {
int v = e[i].to;
if (vis[v] || ~from && i == (from ^ 1)) continue;
w[v] = w[u] + mark[i];
dfs2(v, i);
}
}
int work() {
using BCC::bcc;
using BCC::own;
using BCC::ownpt;
using BCC::solve;
read(n), read(m);
memset(head, -1, sizeof(int[n + 1]));
for (int i = 1; i <= n; i++) p[i] = i;
for (int i = 0; i < m; i++) {
int a, b;
read(a), read(b);
add(a, b), add(b, a);
p[f(a)] = f(b);
}
solve();
ST::build();
for (int i = 1; i <= bcc; i++) {
for (int j = 0; j < ownpt[i].size(); j++) vis[ownpt[i][j]] = false;
if (dfs(e[own[i][0]].to, own[i][0]))
for (int j = 0; j < own[i].size(); j++)
mark[own[i][j]] = mark[own[i][j] ^ 1] = true;
}
memset(vis, 0, sizeof(bool[n + 1]));
for (int i = 1; i <= n; i++)
if (!vis[i]) dfs2(i, -1);
int q;
read(q);
while (q--) {
int u, v;
read(u), read(v);
if (f(u) != f(v)) {
write("No\n");
continue;
}
int l = lca(u, v);
if (((dep[u] + dep[v] - (dep[l] << 1)) & 1) || w[u] + w[v] - (w[l] << 1))
write("Yes\n");
else
write("No\n");
}
return 0;
}
} // namespace zyt
int main() { return zyt::work(); }
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
long long n, m, k;
long long read() {
long long s = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
s = s * 10 + ch - '0';
ch = getchar();
}
return s * f;
}
inline void print(long long *f, long long len) {
for (long long i = 0; i < len; i++) printf("%lld ", f[i]);
puts("");
}
const long long maxn = 2e5 + 10;
long long head[maxn], to[maxn], nxt[maxn], tot;
long long dep[maxn], anc[maxn][17];
void add(long long x, long long y) {
to[++tot] = y;
nxt[tot] = head[x];
head[x] = tot;
}
long long fa[maxn], w[maxn], a[maxn];
long long find(long long x) { return fa[x] == x ? fa[x] : fa[x] = find(fa[x]); }
void dfs1(long long u) {
dep[u] = dep[anc[u][0]] + 1;
for (long long i = head[u]; i; i = nxt[i]) {
long long v = to[i];
if (!dep[v]) {
anc[v][0] = u;
dfs1(v);
if (find(u) == find(v)) w[u] |= w[v];
} else if (dep[v] < dep[u] - 1) {
if ((dep[u] + dep[v] + 1) & 1) w[u] = 1;
for (long long x = find(u); dep[x] > dep[v] + 1; x = find(x))
fa[x] = anc[x][0];
}
}
}
void dfs2(long long x) {
a[x] += w[x];
for (long long i = head[x]; i; i = nxt[i]) {
long long v = to[i];
if (dep[v] == dep[x] + 1) {
if (find(x) == find(v)) w[v] |= w[x];
a[v] = a[x];
dfs2(v);
}
}
}
long long lca(long long x, long long y) {
if (dep[x] < dep[y]) swap(x, y);
for (long long i = 16, iend = 0; i >= iend; --i)
if (dep[anc[x][i]] >= dep[y]) x = anc[x][i];
if (x == y) return x;
for (long long i = 16, iend = 0; i >= iend; --i)
if (anc[x][i] != anc[y][i]) x = anc[x][i], y = anc[y][i];
return anc[x][0];
}
signed main() {
n = read(), m = read();
for (long long i = 1, iend = m; i <= iend; ++i) {
long long x = read(), y = read();
add(x, y);
add(y, x);
}
for (long long i = 1, iend = n; i <= iend; ++i) fa[i] = i;
for (long long i = 1, iend = n; i <= iend; ++i) {
if (!dep[i]) dfs1(i), dfs2(i);
}
for (long long j = 1, jend = 16; j <= jend; ++j)
for (long long i = 1, iend = n; i <= iend; ++i)
anc[i][j] = anc[anc[i][j - 1]][j - 1];
long long Q = read();
while (Q--) {
long long u = read(), v = read();
long long qwq = lca(u, v);
if (!qwq)
puts("No");
else if (((dep[u] + dep[v]) & 1) || a[u] + a[v] - 2 * a[qwq])
puts("Yes");
else
puts("No");
}
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int read() {
char ch = '!';
int res = 0, f = 0;
while (!isdigit(ch)) {
ch = getchar();
if (ch == '-') f = 1;
}
while (isdigit(ch)) res = res * 10 + ch - '0', ch = getchar();
return f ? -res : res;
}
const int N = 2e5 + 100;
int n, m, Q, cnt, fa[N], col[N];
int pa[N], dep[N], sum[N], ans[N], dfn[N], low[N], ts, stk[N], top;
vector<int> G[N], nxt[N];
vector<pair<int, int> > q[N];
int getfa(int x) { return x == fa[x] ? x : fa[x] = getfa(fa[x]); }
void dfs(int u, int c) {
col[u] = c;
for (auto v : nxt[u])
if (!col[v]) dfs(v, 3 - c);
}
void dfs3(int u, int lst) {
pa[u] = lst, dep[u] = dep[lst] + 1;
for (auto v : G[u])
if (v != lst) dfs3(v, u);
}
void dfs1(int u, int lst) {
sum[u] += sum[lst], dfn[u] = ++ts;
for (auto v : G[u])
if (v != lst) dfs1(v, u);
}
void dfs2(int u, int lst) {
fa[u] = u;
for (auto p : q[u])
ans[p.second] =
sum[u] + sum[p.first] - sum[getfa(p.first)] - sum[pa[getfa(p.first)]];
for (auto v : G[u])
if (v != lst) dfs2(v, u);
fa[u] = lst;
}
void tarjan(int u) {
dfn[u] = low[u] = ++ts, stk[++top] = u;
for (auto v : nxt[u])
if (!dfn[v]) {
tarjan(v), low[u] = min(low[u], low[v]);
if (low[v] == dfn[u]) {
++cnt, G[cnt].push_back(u), G[u].push_back(cnt);
do G[cnt].push_back(stk[top]), G[stk[top]].push_back(cnt);
while (stk[top--] != v);
}
} else
low[u] = min(low[u], dfn[v]);
}
int main() {
n = cnt = read(), m = read();
for (int i = 1; i <= n; ++i) fa[i] = i;
for (int i = 1; i <= m; ++i) {
int a = read(), b = read();
nxt[a].push_back(b);
nxt[b].push_back(a);
fa[getfa(a)] = getfa(b);
}
for (int i = 1; i <= n; ++i)
if (!col[i]) dfs(i, 1);
for (int i = 1; i <= n; ++i)
if (!dfn[i]) tarjan(i);
for (int i = 1; i <= cnt; ++i)
if (!dep[i]) dfs3(i, 0);
for (int x = 1; x <= n; ++x)
for (auto y : nxt[x])
if (col[x] == col[y])
dep[pa[x]] > dep[pa[y]] ? sum[pa[x]] = 1 : sum[pa[y]] = 1;
memset(dfn, 0, sizeof(dfn));
ts = 0;
for (int i = 1; i <= cnt; ++i)
if (dep[i] == 1) dfs1(i, 0);
Q = read();
for (int i = 1; i <= Q; ++i) {
int x = read(), y = read();
if (getfa(x) != getfa(y)) continue;
if (col[x] != col[y]) {
ans[i] = 1;
continue;
}
if (dfn[y] < dfn[x]) swap(x, y);
q[y].push_back(make_pair(x, i));
}
for (int i = 1; i <= cnt; ++i)
if (dep[i] == 1) dfs2(i, 0);
for (int i = 1; i <= Q; ++i) puts(ans[i] ? "Yes" : "No");
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
template <typename T>
void read(T &x) {
x = 0;
int f(1);
char c = getchar();
for (; !isdigit(c); c = getchar())
if (c == '-') f = -1;
for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
x *= f;
}
template <typename T, typename... Args>
inline void read(T &x, Args &...args) {
read(x);
read(args...);
}
const int maxn = 100005;
int n, m, hd[maxn], cnt, fg[maxn];
struct edge {
int ed, nxt;
} e[2 * maxn];
int tg[2 * maxn], val[2 * maxn], col[2 * maxn];
int dep[2 * maxn], fa[2 * maxn][19], lg[2 * maxn];
void star(int u, int v) {
e[++cnt] = (edge){v, hd[u]};
hd[u] = cnt;
e[++cnt] = (edge){u, hd[v]};
hd[v] = cnt;
}
int tot, top, num, odd, nc;
vector<int> G[maxn * 2];
int dfn[2 * maxn], low[2 * maxn], sta[2 * maxn];
void Tarjan(int x, int Cl) {
tg[x] = Cl;
sta[++top] = x;
dfn[x] = low[x] = ++tot;
col[x] = nc;
for (int i = hd[x]; i; i = e[i].nxt) {
int y = e[i].ed;
int tmp = odd;
if (!fg[i / 2]) fg[i / 2] = 1, odd += (tg[x] == tg[y]);
if (!dfn[y]) {
int cur = tmp;
Tarjan(y, Cl ^ 1);
tmp = (odd - tmp > 0);
low[x] = min(low[x], low[y]);
if (low[y] == dfn[x]) {
++num;
val[num] = tmp;
while (sta[top + 1] != y)
G[num].emplace_back(sta[top]), G[sta[top--]].emplace_back(num);
G[num].emplace_back(x);
G[x].emplace_back(num);
odd = cur;
}
} else
low[x] = min(low[x], dfn[y]);
}
}
void Dfs1(int x, int f) {
fa[x][0] = f;
dep[x] = dep[f] + 1;
for (int i(1); i < lg[dep[x]]; ++i) fa[x][i] = fa[fa[x][i - 1]][i - 1];
val[x] += val[f];
for (auto y : G[x])
if (y != f) Dfs1(y, x);
}
int LCA(int x, int y) {
while (dep[x] != dep[y]) {
if (dep[x] < dep[y]) swap(x, y);
x = fa[x][lg[dep[x] - dep[y]] - 1];
}
if (x == y) return x;
for (int i = lg[dep[x]] - 1; i >= 0; --i)
if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i];
return fa[x][0];
}
int Asks(int x, int y) {
if (x == y || col[x] != col[y]) return 0;
int l = LCA(x, y);
return (tg[x] ^ tg[y]) || ((val[x] + val[y] - val[l] - val[fa[l][0]]) > 0);
}
int main() {
cnt = 1;
memset(tg, -1, sizeof tg);
read(n, m);
num = n;
for (int i(1); i <= m; ++i) {
int u, v;
read(u, v);
star(u, v);
}
for (int i(1); i <= 2 * n; ++i) lg[i] = lg[i - 1] + (1 << lg[i - 1] == i);
dep[0] = -1;
for (int i(1); i <= n; ++i)
if (!dfn[i]) {
++nc;
tot = 0;
Tarjan(i, 0);
--top;
Dfs1(i, 0);
}
int Q;
read(Q);
while (Q--) {
int x, y;
read(x, y);
if (Asks(x, y))
puts("Yes");
else
puts("No");
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int inf = ~0U >> 1;
const long long INF = ~0LLU >> 1;
int rd() { return RAND_MAX == 32767 ? ((rand() << 15) ^ rand()) : rand(); }
const int maxn = 100010;
const int maxq = 100010;
int p[maxn], q[maxn], fa[maxn], dep[maxn], odd[maxn], sum[maxn];
int lca[maxq];
vector<int> G[maxn];
vector<pair<int, int> > E, G_[maxn];
int Find(int *p, int x) { return p[x] == x ? x : p[x] = Find(p, p[x]); }
void Dfs(int t) {
for (__typeof(G[t].begin()) e = G[t].begin(); e != G[t].end(); e++)
if (dep[*e] == -1) {
dep[*e] = dep[t] + 1;
fa[*e] = t;
Dfs(*e);
q[*e] = t;
if (Find(p, *e) == Find(p, t)) odd[t] |= odd[*e];
} else if (dep[t] > dep[*e] + 1) {
if ((dep[t] - dep[*e]) % 2 == 0) odd[t] = 1;
for (int k = Find(p, t); dep[k] > dep[*e] + 1; k = Find(p, k))
p[k] = fa[k];
}
for (__typeof(G_[t].begin()) e = G_[t].begin(); e != G_[t].end(); e++)
if (dep[(*e).first] != -1) lca[(*e).second] = Find(q, (*e).first);
}
void Calc(int t) {
sum[t] += odd[t];
for (__typeof(G[t].begin()) e = G[t].begin(); e != G[t].end(); e++)
if (dep[*e] == dep[t] + 1) {
if (Find(p, *e) == Find(p, t)) odd[*e] |= odd[t];
sum[*e] = sum[t];
Calc(*e);
}
}
int main() {
int n, m, T;
cin >> n >> m;
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
u--;
v--;
G[u].push_back(v);
G[v].push_back(u);
}
cin >> T;
for (int i = 0; i < T; i++) {
int u, v;
cin >> u >> v;
u--;
v--;
G_[u].push_back(make_pair(v, i));
G_[v].push_back(make_pair(u, i));
E.push_back(make_pair(u, v));
}
memset(dep, -1, sizeof(dep));
memset(odd, 0, sizeof(odd));
for (int i = 0; i < n; i++) p[i] = q[i] = i;
for (int i = 0; i < n; i++)
if (dep[i] == -1) {
fa[i] = -1;
dep[i] = 0;
Dfs(i);
sum[i] = 0;
Calc(i);
}
for (int i = 0; i < T; i++) {
int u = E[i].first, v = E[i].second, w = lca[i];
if (u != v && Find(q, u) == Find(q, v) &&
(abs(dep[u] - dep[v]) % 2 == 1 || sum[u] + sum[v] - sum[w] * 2 > 0))
cout << "Yes" << endl;
else
cout << "No" << endl;
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
struct DJS {
int p[101010];
void init(int n) {
for (int i = 0; i <= n; i++) p[i] = i;
}
int find_p(int x) { return x == p[x] ? x : p[x] = find_p(p[x]); }
void uni(int x, int y) { p[find_p(x)] = find_p(y); }
} djs, cc;
int n, m;
vector<int> g[101010], s[101010];
void init() {
scanf("%d%d", &n, &m);
djs.init(n);
cc.init(n);
while (m--) {
int ui, vi;
scanf("%d%d", &ui, &vi);
g[ui].push_back(vi);
g[vi].push_back(ui);
cc.uni(ui, vi);
}
}
bool got[101010], got2[101010];
int p[20][101010], dep[101010];
inline int lca(int ui, int vi) {
if (dep[ui] > dep[vi]) swap(ui, vi);
int dlt = dep[vi] - dep[ui];
for (int i = 0; i < 20; i++)
if ((dlt >> i) & 1) vi = p[i][vi];
if (ui == vi) return ui;
for (int i = 20 - 1; i >= 0; i--)
if (p[i][ui] != p[i][vi]) {
ui = p[i][ui];
vi = p[i][vi];
}
return p[0][ui];
}
vector<pair<int, int> > e;
void go(int now, int prt, int dp) {
got[now] = true;
dep[now] = dp;
p[0][now] = prt;
for (int son : g[now]) {
if (son == prt) continue;
if (got[son])
e.push_back({now, son});
else {
s[now].push_back(son);
go(son, now, dp + 1);
}
}
}
bool od[101010];
int sum[101010];
void go2(int now) {
got2[now] = true;
if (od[djs.find_p(now)] && now != djs.find_p(now)) sum[now]++;
for (int son : s[now]) {
sum[son] = sum[now];
go2(son);
}
}
void solve() {
for (int i = 1; i <= n; i++)
if (!got[i]) go(i, i, 0);
for (int i = 1; i < 20; i++)
for (int j = 1; j <= n; j++) p[i][j] = p[i - 1][p[i - 1][j]];
for (pair<int, int> tp : e) {
int ui = tp.first, vi = tp.second;
int lc = lca(ui, vi);
if ((dep[vi] - dep[ui]) % 2 == 0) od[lc] = true;
ui = djs.find_p(ui);
vi = djs.find_p(vi);
while (ui != vi) {
if (dep[ui] > dep[vi]) swap(ui, vi);
djs.uni(vi, p[0][vi]);
vi = djs.find_p(vi);
}
}
for (int i = 1; i <= n; i++)
if (od[i] && i != djs.find_p(i)) {
od[djs.find_p(i)] = true;
od[i] = false;
}
for (int i = 1; i <= n; i++)
if (!got2[i]) go2(i);
int q;
scanf("%d", &q);
while (q--) {
int ui, vi;
scanf("%d%d", &ui, &vi);
int lc = lca(ui, vi);
bool pos = false;
if (sum[ui] != sum[lc] || sum[vi] != sum[lc] || (dep[ui] + dep[vi]) % 2)
pos = true;
if (cc.find_p(ui) != cc.find_p(vi) || ui == vi) pos = false;
puts(pos ? "Yes" : "No");
}
}
int main() {
init();
solve();
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7, M = 2e6 + 7;
struct graph {
int cnt, v[M], nxt[M], w[M], hd[N];
void add(int x, int y, int z) {
v[++cnt] = y;
w[cnt] = z;
nxt[cnt] = hd[x];
hd[x] = cnt;
}
} G, H;
int n, m, q, a[M][2], dep[N], lca[N], p0[N], p1[N], fa[N], tot[N];
bool o[N];
int find(int* f, int x) { return (f[x] == x) ? x : f[x] = find(f, f[x]); }
void dfs(int u) {
for (int i = G.hd[u]; i; i = G.nxt[i])
if (!dep[G.v[i]]) {
fa[G.v[i]] = u;
dep[G.v[i]] = dep[u] + 1;
dfs(G.v[i]);
p1[G.v[i]] = u;
if (find(p0, u) == find(p0, G.v[i])) o[u] |= o[G.v[i]];
} else if (dep[G.v[i]] + 1 < dep[u]) {
int z = find(p0, u);
while (dep[z] > dep[G.v[i]] + 1) p0[z] = fa[z], z = find(p0, z);
if ((dep[u] - dep[G.v[i]]) % 2 == 0) o[u] = 1;
}
for (int i = H.hd[u]; i; i = H.nxt[i]) lca[H.w[i]] = find(p1, H.v[i]);
}
void work(int u) {
if (o[find(p0, u)]) tot[u]++;
for (int i = G.hd[u]; i; i = G.nxt[i])
if (dep[G.v[i]] == dep[u] + 1) {
if (find(p0, u) == find(p0, G.v[i])) o[G.v[i]] |= o[u];
tot[G.v[i]] = tot[u];
work(G.v[i]);
}
}
bool check(int x, int y, int w) {
if (find(p1, x) != find(p1, y)) return 0;
if ((dep[x] - dep[y]) % 2) return 1;
return tot[x] + tot[y] - tot[w] * 2 > 0;
}
int main() {
scanf("%d%d", &n, &m);
int x, y;
for (int i = 1; i <= m; i++)
scanf("%d%d", &x, &y), G.add(x, y, i), G.add(y, x, i);
scanf("%d", &q);
for (int i = 1; i <= q; i++)
scanf("%d%d", &a[i][0], &a[i][1]), H.add(a[i][0], a[i][1], i),
H.add(a[i][1], a[i][0], i);
for (int i = 1; i <= n; i++) p0[i] = p1[i] = i;
for (int i = 1; i <= n; i++)
if (!dep[i]) dep[i] = 1, dfs(i), work(i);
for (int i = 1; i <= q; i++)
if (check(a[i][0], a[i][1], lca[i]))
puts("Yes");
else
puts("No");
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
const int N = 1e5 + 5;
int n, m, q, tot, ff[N], t[4 * N], dfn[N];
int fa[N], sz[N], son[N], top[N], dep[N];
int to[N + N], nx[N + N], hd[N], sze;
inline void add(int u, int v) { to[++sze] = v, nx[sze] = hd[u], hd[u] = sze; }
inline int find(int x) { return x == ff[x] ? x : ff[x] = find(ff[x]); }
inline void merge(int x, int y) {
x = find(x), y = find(y);
if (x != y) ff[x] = y;
}
inline void pushdown(int p, int l, int r, int mid) {
if (t[p] == r - l + 1) t[p << 1] = mid - l + 1, t[p << 1 | 1] = r - mid;
}
inline void uptqj(int p, int l, int r, int x, int y) {
if (l >= x && r <= y) {
t[p] = r - l + 1;
return;
}
int mid = l + r >> 1;
pushdown(p, l, r, mid);
if (x <= mid) uptqj(p << 1, l, mid, x, y);
if (y > mid) uptqj(p << 1 | 1, mid + 1, r, x, y);
t[p] = t[p << 1] + t[p << 1 | 1];
}
inline int query(int p, int l, int r, int x, int y) {
if (l >= x && r <= y) return t[p];
int mid = l + r >> 1, res = 0;
pushdown(p, l, r, mid);
if (x <= mid) res += query(p << 1, l, mid, x, y);
if (y > mid) res += query(p << 1 | 1, mid + 1, r, x, y);
return res;
}
inline int LCA(int x, int y) {
for (; top[x] != top[y]; x = fa[top[x]])
if (dep[top[x]] < dep[top[y]]) std::swap(x, y);
return dep[x] < dep[y] ? x : y;
}
inline void uptl(int x, int y) {
for (; top[x] != top[y]; x = fa[top[x]]) {
if (dep[top[x]] < dep[top[y]]) std::swap(x, y);
uptqj(1, 1, n, dfn[top[x]], dfn[x]);
}
if (dep[x] > dep[y]) std::swap(x, y);
uptqj(1, 1, n, dfn[x] + 1, dfn[y]);
}
inline int ql(int x, int y) {
int res = 0;
for (; top[x] != top[y]; x = fa[top[x]]) {
if (dep[top[x]] < dep[top[y]]) std::swap(x, y);
res += query(1, 1, n, dfn[top[x]], dfn[x]);
}
if (dep[x] > dep[y]) std::swap(x, y);
return res + query(1, 1, n, dfn[x], dfn[y]);
}
inline void dfs1(int u, int p) {
sz[u] = 1, dep[u] = dep[p] + 1, fa[u] = p;
int i, v;
for (i = hd[u]; i; i = nx[i])
if (!dep[v = to[i]]) {
dfs1(v, u), sz[u] += sz[v];
if (sz[v] > sz[son[u]]) son[u] = v;
}
}
inline void dfs2(int u, int t) {
top[u] = t, dfn[u] = ++tot;
int i, v;
if (!son[u]) return;
dfs2(son[u], t);
for (i = hd[u]; i; i = nx[i])
if (dep[v = to[i]] >= dep[u] && v != son[u] && fa[v] == u) dfs2(v, v);
}
inline void dfs3(int u) {
for (int i = hd[u], v; i; i = nx[i])
if ((v = to[i]) != fa[u]) {
if (fa[v] == u)
dfs3(v);
else if (dep[v] > dep[u])
continue;
else if ((dep[u] - dep[v]) % 2 < 1)
uptl(u, v);
}
}
inline void dfs4(int u) {
for (int i = hd[u], v; i; i = nx[i])
if ((v = to[i]) != fa[u]) {
if (fa[v] == u)
dfs4(v);
else if (dep[v] > dep[u])
continue;
else if ((dep[u] - dep[v]) & 1) {
if (ql(u, v) > query(1, 1, n, dfn[v], dfn[v])) uptl(u, v);
}
}
}
int main() {
scanf("%d%d", &n, &m);
int i, j, l, x, y;
for (i = 1; i <= n; i++) ff[i] = i;
while (m--) scanf("%d%d", &i, &j), add(i, j), add(j, i), merge(i, j);
for (i = 1; i <= n; i++)
if (!dep[i]) dfs1(i, 0), dfs2(i, i), dfs3(i), dfs4(i);
for (scanf("%d", &q); q; q--) {
scanf("%d%d", &i, &j), l = LCA(i, j);
if (find(i) != find(j))
puts("No");
else {
if ((dep[i] + dep[j] - 2 * dep[l]) & 1)
puts("Yes");
else if (ql(i, j) > query(1, 1, n, dfn[l], dfn[l]))
puts("Yes");
else
puts("No");
}
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int n, m, q, fa[N], f[N][17], dep[N], s[N], a[N];
vector<int> e[N];
int get(int u) { return u == fa[u] ? u : fa[u] = get(fa[u]); }
void dfs1(int u) {
dep[u] = dep[f[u][0]] + 1;
for (auto v : e[u]) {
if (!dep[v]) {
f[v][0] = u;
dfs1(v);
if (get(u) == get(v)) a[u] |= a[v];
} else if (dep[v] + 1 < dep[u]) {
if ((dep[u] + dep[v] + 1) & 1) a[u] = 1;
for (int x = get(u); dep[x] > dep[v] + 1; x = get(x)) fa[x] = f[x][0];
}
}
}
void dfs2(int u) {
s[u] += a[u];
for (auto v : e[u]) {
if (dep[v] == dep[u] + 1) {
if (get(u) == get(v)) a[v] |= a[u];
s[v] = s[u];
dfs2(v);
}
}
}
int lca(int x, int y) {
if (dep[x] < dep[y]) swap(x, y);
for (int i = 16; i >= 0; i--)
if (dep[f[x][i]] >= dep[y]) x = f[x][i];
if (x == y) return x;
for (int i = 16; i >= 0; i--)
if (f[x][i] != f[y][i]) x = f[x][i], y = f[y][i];
return f[x][0];
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++) {
int u, v;
scanf("%d%d", &u, &v);
e[u].push_back(v);
e[v].push_back(u);
}
for (int i = 1; i <= n; i++) fa[i] = i;
for (int i = 1; i <= n; i++)
if (!dep[i]) dfs1(i), dfs2(i);
for (int j = 1; j <= 16; j++)
for (int i = 1; i <= n; i++) f[i][j] = f[f[i][j - 1]][j - 1];
scanf("%d", &q);
while (q--) {
int u, v;
scanf("%d%d", &u, &v);
int l = lca(u, v);
if (!l)
puts("No");
else if (((dep[u] + dep[v]) & 1) || s[u] + s[v] - 2 * s[l])
puts("Yes");
else
puts("No");
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int n, m;
vector<int> u[100005];
vector<int> v[100005];
int cid[100005], _CID, col[100005], d[100005], par[100005], SPEC[100005],
spec[100005];
int P[18][100005], Q[18][100005];
set<int> mergeto;
int l[100005];
int gl(int a) { return l[a] = a == l[a] ? a : gl(l[a]); }
void dfs(int i, int p, int _d = 1) {
col[i] = col[p] ^ 1;
cid[i] = _CID;
d[i] = _d++;
par[i] = p;
for (int j = (0); j < (int(u[i].size())); j++)
if (cid[u[i][j]] == 0) {
v[i].push_back(u[i][j]);
dfs(u[i][j], i, _d);
}
for (int j = (0); j < (int(u[i].size())); j++)
if (d[u[i][j]] < d[i]) {
int to = d[u[i][j]] + 1;
for (int x = i; d[x] > to; x = gl(x)) {
l[gl(x)] = gl(par[x]);
}
}
}
int lca(int i, int j) {
if (d[i] < d[j]) swap(i, j);
int r = 0;
for (int k = 17; k >= 0; k--)
if (d[P[k][i]] >= d[j]) {
r += Q[k][i];
i = P[k][i];
}
if (i == j) return r;
for (int k = 17; k >= 0; k--)
if (P[k][i] != P[k][j]) {
r += Q[k][i] + Q[k][j];
i = P[k][i];
j = P[k][j];
}
return r + Q[0][i] + Q[0][j];
}
int main() {
scanf("%d %d", &n, &m);
for (int i = (0); i < (m); i++) {
int a, b;
scanf("%d%d", &a, &b);
u[a].push_back(b);
u[b].push_back(a);
}
for (int i = (1); i < (n + 1); i++) l[i] = i;
for (int i = (1); i < (n + 1); i++)
if (cid[i] == 0) _CID++, dfs(i, 0);
for (int i = (1); i < (n + 1); i++)
for (int j = (0); j < (int(u[i].size())); j++) {
if (col[i] == col[u[i][j]]) {
int lo = d[i] < d[u[i][j]] ? u[i][j] : i;
SPEC[gl(lo)] = true;
}
}
for (int i = (1); i < (n + 1); i++)
if (SPEC[gl(i)]) spec[i] = true;
for (int i = (1); i < (n + 1); i++) P[0][i] = par[i], Q[0][i] = spec[i];
for (int i = (1); i < (18); i++)
for (int j = (1); j < (n + 1); j++) {
Q[i][j] = Q[i - 1][j] + Q[i - 1][P[i - 1][j]];
P[i][j] = P[i - 1][P[i - 1][j]];
}
int q;
scanf("%d", &q);
for (int _ = (0); _ < (q); _++) {
int s, t;
scanf("%d %d", &s, &t);
if (cid[s] != cid[t] || s == t) {
printf("No\n");
continue;
}
if (col[s] != col[t]) {
printf("Yes\n");
continue;
}
printf(lca(s, t) ? "Yes\n" : "No\n");
}
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
struct Edge {
int u, v;
} sta[200010];
int n, m, q, k, t, p, top, vis[200010], tag[200010];
int head[200010], to[200010], nexts[200010];
int dfn[200010], low[200010], res[200010], cnt[200010], dep[200010],
fa[200010][18];
void add(int u, int v) {
to[++k] = v;
nexts[k] = head[u];
head[u] = k;
}
void work(int x, int y) {
int a, b, pre = top, s = 0;
do {
a = sta[top].u, b = sta[top].v;
--top;
if ((dep[a] & 1) == (dep[b] & 1)) {
s = 1;
break;
}
} while (!(a == x && b == y));
if (!s) return;
top = pre;
do {
a = sta[top].u, b = sta[top].v;
--top;
cnt[a] = cnt[b] = 1;
} while (!(a == x && b == y));
cnt[x] = 0;
}
void Tarjan(int x, int f) {
dfn[x] = low[x] = ++t;
for (int i = head[x]; i; i = nexts[i]) {
int v = to[i];
if (v != f && dfn[v] < dfn[x]) {
sta[++top] = (Edge){x, v};
if (!dfn[v]) {
Tarjan(v, x);
low[x] = min(low[x], low[v]);
if (dfn[x] <= low[v]) work(x, v);
} else
low[x] = min(low[x], dfn[v]);
}
}
}
void dfs1(int x, int f) {
dep[x] = dep[f] + 1;
fa[x][0] = f;
res[x] = p;
vis[x] = 1;
for (int i = 1; (1 << i) <= dep[x]; i++) fa[x][i] = fa[fa[x][i - 1]][i - 1];
for (int i = head[x]; i; i = nexts[i])
if (!vis[to[i]]) dfs1(to[i], x);
}
void dfs2(int x, int f) {
cnt[x] += cnt[f];
tag[x] = 1;
for (int i = head[x]; i; i = nexts[i])
if (!tag[to[i]]) dfs2(to[i], x);
}
int LCA(int x, int y) {
if (dep[x] < dep[y]) swap(x, y);
int T = dep[x] - dep[y];
for (int i = 0; (1 << i) <= T; i++)
if (T & (1 << i)) x = fa[x][i];
if (x == y) return x;
for (int i = 17; i >= 0; i--)
if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i];
return fa[x][0];
}
bool Solve(int x, int y) {
if (res[x] != res[y]) return false;
if ((dep[x] & 1) ^ (dep[y] & 1)) return true;
return cnt[x] + cnt[y] - cnt[LCA(x, y)] * 2 > 0;
}
int main() {
int x, y;
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++) scanf("%d%d", &x, &y), add(x, y), add(y, x);
for (int i = 1; i <= n; i++)
if (!dfn[i]) {
++p;
dfs1(i, 0);
Tarjan(i, 0);
dfs2(i, 0);
}
scanf("%d", &q);
for (int i = 1; i <= q; i++) {
scanf("%d%d", &x, &y);
if (Solve(x, y))
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
struct DSU {
int fa[maxn];
inline void init(int n) {
for (int i = 1; i <= n; i++) fa[i] = i;
}
int findset(int x) { return fa[x] == x ? x : fa[x] = findset(fa[x]); }
} dsu;
vector<int> G[maxn];
int dep[maxn], anc[maxn][20], odd[maxn];
void dfs(int now, int fa, int depth) {
anc[now][0] = fa;
dep[now] = depth;
for (const int& to : G[now])
if (!dep[to]) {
dfs(to, now, depth + 1);
if (dsu.findset(now) == dsu.findset(to)) odd[now] |= odd[to];
} else if (dep[to] + 1 < dep[now]) {
if (!((dep[to] + dep[now]) & 1)) odd[now] = 1;
for (int u = dsu.findset(now); dep[to] + 1 < dep[u]; u = dsu.findset(u))
dsu.fa[u] = anc[u][0];
}
}
int cnt[maxn];
void dfs2(int now) {
cnt[now] += odd[now];
for (const int& to : G[now])
if (dep[to] == dep[now] + 1) {
if (dsu.findset(now) == dsu.findset(to)) odd[to] |= odd[now];
cnt[to] = cnt[now];
dfs2(to);
}
}
inline void preprocess(int n) {
dsu.init(n);
for (int i = 1; i <= n; i++)
if (!dep[i]) dfs(i, 0, 1);
for (int i = 1; i < 20; i++)
for (int now = 1; now <= n; now++)
anc[now][i] = anc[anc[now][i - 1]][i - 1];
for (int i = 1; i <= n; i++)
if (dep[i] == 1) dfs2(i);
}
inline int LCA(int a, int b) {
if (dep[a] > dep[b]) swap(a, b);
int ddep = dep[b] - dep[a];
for (int i = 0; ddep; i++, ddep >>= 1)
if (ddep & 1) b = anc[b][i];
if (a == b) return a;
for (int i = 19; i >= 0; i--)
if (anc[a][i] != anc[b][i]) a = anc[a][i], b = anc[b][i];
assert(anc[a][0] == anc[b][0]);
return anc[a][0];
}
inline bool query(int a, int b) {
int lca = LCA(a, b);
if (lca == 0) return 0;
if (((dep[a] + dep[b]) & 1) || (cnt[a] + cnt[b] - 2 * cnt[lca] > 0))
return 1;
else
return 0;
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 0, a, b; i < m; i++) {
scanf("%d%d", &a, &b);
G[a].push_back(b);
G[b].push_back(a);
}
preprocess(n);
int q;
scanf("%d", &q);
for (int i = 0, a, b; i < q; i++) {
scanf("%d%d", &a, &b);
printf("%s\n", query(a, b) ? "Yes" : "No");
}
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 100000;
vector<int> s[N + 5];
int fa[N + 5], pa[N + 5][20], dis[N + 5], odd[N + 5], sum[N + 5];
int Find(int k) {
if (fa[k] == k) return k;
return fa[k] = Find(fa[k]);
}
void dfs1(int u, int add) {
dis[u] = add;
for (int i = 0; i < s[u].size(); i++) {
int v = s[u][i];
if (dis[v] == -1) {
pa[v][0] = u;
dfs1(v, add + 1);
if (Find(u) == Find(v)) {
odd[u] |= odd[v];
}
} else if (dis[v] + 1 < dis[u]) {
if ((dis[u] - dis[v]) % 2 == 0) {
odd[u] = 1;
}
int x = Find(u);
while (dis[v] + 1 < dis[x]) {
fa[x] = pa[x][0];
x = Find(x);
}
}
}
}
int cnt[N + 5];
void dfs2(int u) {
cnt[u] += odd[u];
for (int i = 0; i < s[u].size(); i++) {
int v = s[u][i];
if (dis[v] == dis[u] + 1) {
if (Find(u) == Find(v)) odd[v] |= odd[u];
cnt[v] = cnt[u];
dfs2(v);
}
}
}
int lca(int a, int b) {
if (dis[a] > dis[b]) swap(a, b);
int d = dis[b] - dis[a];
for (int i = 18; i >= 0; i--) {
if (d & (1 << i)) {
b = pa[b][i];
}
}
for (int i = 18; i >= 0; i--) {
if (pa[a][i] != pa[b][i]) {
a = pa[a][i];
b = pa[b][i];
}
}
if (a != b) a = pa[a][0];
return a;
}
int check(int x, int y) {
int pt = lca(x, y);
if (pt == 0) return 0;
if ((dis[x] + dis[y]) % 2) return 1;
if (cnt[x] + cnt[y] - 2 * cnt[pt] > 0) return 1;
return 0;
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 0; i < m; i++) {
int x, y;
scanf("%d%d", &x, &y);
s[x].push_back(y);
s[y].push_back(x);
}
for (int i = 1; i <= n; i++) {
fa[i] = i;
}
memset(dis, -1, sizeof(dis));
for (int i = 0; i < n; i++) {
if (dis[i] == -1) {
dfs1(i, 0);
dfs2(i);
}
}
for (int i = 1; i <= 17; i++) {
for (int j = 1; j <= n; j++) {
pa[j][i] = pa[pa[j][i - 1]][i - 1];
}
}
int t;
scanf("%d", &t);
while (t--) {
int x, y;
scanf("%d%d", &x, &y);
if (check(x, y))
puts("Yes");
else
puts("No");
}
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int MAX = 200010;
vector<int> L[MAX];
struct data {
int a, b, ans, done;
} Q[MAX];
int ara[MAX];
namespace BCT {
vector<int> ed[MAX];
bool cut[MAX];
int tot, Time, low[MAX], st[MAX];
vector<int> bcc[MAX];
stack<int> S;
void popBCC(int s, int x) {
cut[s] = 1;
bcc[++tot].push_back(s);
while (bcc[tot].back() ^ x) {
bcc[tot].push_back(S.top());
S.pop();
}
}
void dfs(int s, int p = -1) {
S.push(s);
int ch = 0;
st[s] = low[s] = ++Time;
for (int x : ed[s]) {
if (!st[x]) {
ch++;
dfs(x, s);
low[s] = min(low[s], low[x]);
if (p != -1 && low[x] >= st[s])
popBCC(s, x);
else if (p == -1)
if (ch > 1) popBCC(s, x);
} else if (p != x)
low[s] = min(low[s], st[x]);
}
if (p == -1 && ch > 1) cut[s] = 1;
}
void processBCC(int n) {
for (int i = 1; i <= n; i++) bcc[i].clear();
memset(st, 0, sizeof(st));
memset(cut, 0, sizeof(cut));
Time = tot = 0;
for (int i = 1; i <= n; i++) {
if (!st[i]) {
dfs(i, -1);
if (!S.empty()) ++tot;
while (!S.empty()) {
bcc[tot].push_back(S.top());
S.pop();
}
}
}
}
int nn;
vector<int> tree[MAX];
int compNum[MAX];
bool oc;
int dis[MAX];
void ogo(int s, int p, int id) {
for (int x : BCT::ed[s]) {
if (compNum[x] != id) continue;
if (x == p or x == s) continue;
if (dis[x]) {
if (dis[x] % 2 == dis[s] % 2) oc = true;
} else {
dis[x] = dis[s] + 1;
ogo(x, s, id);
}
}
}
void buildTree(int n) {
processBCC(n);
for (int i = 1; i <= tot; i++) {
for (int v : bcc[i]) {
compNum[v] = i;
dis[v] = 0;
}
oc = false;
dis[bcc[i][0]] = 1;
ogo(bcc[i][0], bcc[i][0], i);
ara[i] = oc;
for (int q : bcc[i]) {
for (int r : L[q]) {
if (Q[r].done) continue;
int a = Q[r].a;
int b = Q[r].b;
if (compNum[a] == compNum[b]) {
Q[r].done = true;
if (ara[i])
Q[r].ans = true;
else if ((dis[a] % 2) != (dis[b] % 2)) {
Q[r].ans = true;
} else
Q[r].ans = false;
}
}
}
for (int v : bcc[i]) {
compNum[v] = i;
dis[v] = 0;
}
}
nn = tot;
for (int i = 1; i <= n; i++)
if (cut[i]) compNum[i] = ++nn;
for (int i = 1; i <= tot; i++) {
for (int v : bcc[i]) {
if (cut[v]) {
tree[i].push_back(compNum[v]);
tree[compNum[v]].push_back(i);
} else {
compNum[v] = i;
}
}
}
}
}; // namespace BCT
bool isArt(int nd) {
if (nd > BCT::tot) return true;
return false;
}
namespace HLD {
int ptr, par[MAX];
int sz[MAX], h[MAX], pos[MAX], head[MAX], base[MAX];
struct SegmentTree {
struct node {
int mn;
} tree[4 * MAX];
node Merge(node a, node b) {
node ret;
ret.mn = a.mn + b.mn;
return ret;
}
void build(int n, int st, int ed) {
if (st == ed) {
tree[n].mn = ara[base[st]];
return;
}
int mid = (st + ed) / 2;
build(2 * n, st, mid);
build(2 * n + 1, mid + 1, ed);
tree[n] = Merge(tree[2 * n], tree[2 * n + 1]);
}
void update(int n, int st, int ed, int id, int v) {
if (id > ed || id < st) return;
if (st == ed && ed == id) {
tree[n].mn = v;
return;
}
int mid = (st + ed) / 2;
update(2 * n, st, mid, id, v);
update(2 * n + 1, mid + 1, ed, id, v);
tree[n] = Merge(tree[2 * n], tree[2 * n + 1]);
}
node query(int n, int st, int ed, int i, int j) {
if (st >= i && ed <= j) return tree[n];
int mid = (st + ed) / 2;
if (mid < i)
return query(2 * n + 1, mid + 1, ed, i, j);
else if (mid >= j)
return query(2 * n, st, mid, i, j);
else
return Merge(query(2 * n, st, mid, i, j),
query(2 * n + 1, mid + 1, ed, i, j));
}
} st;
void dfs(int u, int far) {
sz[u] = 1, h[u] = far;
for (int v : BCT::tree[u])
BCT::tree[v].erase(find(BCT::tree[v].begin(), BCT::tree[v].end(), u));
for (int &v : BCT::tree[u]) {
par[v] = u;
dfs(v, far + 1);
sz[u] += sz[v];
if (sz[v] > sz[BCT::tree[u][0]]) swap(v, BCT::tree[u][0]);
}
}
void hld(int u) {
pos[u] = ++ptr;
base[ptr] = u;
for (int v : BCT::tree[u]) {
head[v] = (v == BCT::tree[u][0] ? head[u] : v);
hld(v);
}
}
inline int query(int n, int u, int v) {
int res = 0;
while (head[u] != head[v]) {
if (h[head[u]] > h[head[v]]) swap(u, v);
res += st.query(1, 1, n, pos[head[v]], pos[v]).mn;
v = par[head[v]];
}
if (h[u] > h[v]) swap(u, v);
res += st.query(1, 1, n, pos[u], pos[v]).mn;
return res;
}
void build(int n, int root, int x) {
ptr = 0;
for (int i = 1; i <= n; i++) {
if (!head[i]) {
head[i] = i;
h[i] = 0;
dfs(i, 0);
hld(i);
}
}
st.build(1, 1, n);
}
}; // namespace HLD
int D[MAX];
int mark[MAX];
int cur = 0;
void call(int s, int d) {
mark[s] = cur;
D[s] = d;
for (int x : BCT::ed[s]) {
if (mark[x] == 0) call(x, d + 1);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m, q, a, b;
cin >> n >> m;
int x = -1;
for (int i = 1; i <= m; i++) {
cin >> a >> b;
if (x == -1) x = a;
BCT::ed[a].push_back(b);
BCT::ed[b].push_back(a);
}
for (int i = 1; i <= n; i++) {
if (D[i] == 0) {
cur++;
call(i, 1);
}
}
cin >> q;
for (int i = 1; i <= q; i++) {
cin >> Q[i].a >> Q[i].b;
Q[i].done = Q[i].ans = 0;
if (Q[i].b == Q[i].a) {
Q[i].done = true;
Q[i].ans = false;
continue;
}
L[Q[i].a].push_back(i);
L[Q[i].b].push_back(i);
if (mark[Q[i].a] != mark[Q[i].b]) {
Q[i].done = true;
Q[i].ans = false;
}
}
BCT::buildTree(n);
HLD::build(BCT::nn, 1, n);
for (int i = 1; i <= q; i++) {
if (!Q[i].done) {
if (D[Q[i].a] % 2 != D[Q[i].b] % 2) {
Q[i].ans = 1;
} else {
int x = BCT::compNum[Q[i].a];
int y = BCT::compNum[Q[i].b];
Q[i].ans = HLD::query(BCT::nn, x, y);
}
}
if (Q[i].ans)
cout << "Yes\n";
else
cout << "No\n";
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 1e5, L = 18;
int n, m, q;
vector<int> e[N + 1];
int rt[N + 1], fa[N + 1][L + 1], dep[N + 1];
bool vis[N + 1];
int cnt, bel[N + 1];
bool oddRing[N + 1];
int val[N + 1], sum[N + 1];
int read() {
int ret = 0;
char c = getchar();
while (!isdigit(c)) {
c = getchar();
}
while (isdigit(c)) {
ret = ret * 10 + c - '0';
c = getchar();
}
return ret;
}
void dfs(int u, int f, int r) {
rt[u] = r, fa[u][0] = f, dep[u] = dep[f] + 1;
vis[u] = true;
for (int i = 1; i <= L; ++i) {
fa[u][i] = fa[fa[u][i - 1]][i - 1];
}
for (int v : e[u]) {
if (vis[v]) {
continue;
}
dfs(v, u, r);
}
return;
}
void tarjan(int u, int f) {
static int tot, dfn[N + 1], low[N + 1];
static int top, stk[N + 1];
static bool ins[N + 1];
dfn[u] = low[u] = ++tot, stk[++top] = u, ins[u] = true;
for (int v : e[u]) {
if (v == f) {
continue;
}
if (!dfn[v]) {
tarjan(v, u);
low[u] = min(low[u], low[v]);
} else {
low[u] = min(low[u], dfn[v]);
}
}
if (low[u] == dfn[u]) {
++cnt;
do {
bel[stk[top]] = cnt, ins[stk[top]] = false;
} while (stk[top--] != u);
}
return;
}
int lca(int u, int v) {
if (dep[u] > dep[v]) {
swap(u, v);
}
for (int i = L; ~i; --i) {
if (dep[v] - (1 << i) >= dep[u]) {
v = fa[v][i];
}
}
if (u == v) {
return u;
}
for (int i = L; ~i; --i) {
if (fa[u][i] != fa[v][i]) {
u = fa[u][i], v = fa[v][i];
}
}
return fa[u][0];
}
void calcSum(int u) {
sum[u] += val[u];
for (int v : e[u]) {
if (fa[v][0] != u) {
continue;
}
sum[v] = sum[u];
calcSum(v);
}
return;
}
int main() {
n = read(), m = read();
for (int i = 1, u, v; i <= m; ++i) {
u = read(), v = read();
e[u].push_back(v), e[v].push_back(u);
}
for (int i = 1; i <= n; ++i) {
if (!vis[i]) {
dfs(i, 0, i);
}
}
for (int i = 1; i <= n; ++i) {
if (rt[i] == i) {
tarjan(i, 0);
}
}
for (int u = 1; u <= n; ++u) {
if (oddRing[bel[u]]) {
continue;
}
for (int v : e[u]) {
if ((bel[u] == bel[v]) && !(dep[u] - dep[v] & 1)) {
oddRing[bel[u]] = true;
break;
}
}
}
for (int i = 1; i <= n; ++i) {
if ((rt[i] != i) && (bel[i] == bel[fa[i][0]]) && oddRing[bel[i]]) {
val[i] = 1;
}
}
for (int i = 1; i <= n; ++i) {
if (rt[i] == i) {
calcSum(i);
}
}
q = read();
while (q--) {
int u = read(), v = read();
bool flag = true;
if ((u == v) || (rt[u] != rt[v])) {
flag = false;
} else {
if (!((dep[u] ^ dep[v]) & 1) && (sum[u] + sum[v] == sum[lca(u, v)] * 2)) {
flag = false;
}
}
puts(flag ? "Yes" : "No");
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
namespace IO {
const int __S = (1 << 20) + 5;
char __buf[__S], *__H, *__T;
inline char getc() {
if (__H == __T) __T = (__H = __buf) + fread(__buf, 1, __S, stdin);
if (__H == __T) return -1;
return *__H++;
}
template <class __I>
inline void read(__I &__x) {
__x = 0;
int __fg = 1;
char __c = getc();
while (!isdigit(__c) && __c != '-') __c = getc();
if (__c == '-') __fg = -1, __c = getc();
while (isdigit(__c)) __x = __x * 10 + __c - '0', __c = getc();
__x *= __fg;
}
inline void readd(double &__x) {
__x = 0;
double __fg = 1.0;
char __c = getc();
while (!isdigit(__c) && __c != '-') __c = getc();
if (__c == '-') __fg = -1.0, __c = getc();
while (isdigit(__c)) __x = __x * 10.0 + __c - '0', __c = getc();
if (__c != '.') {
__x = __x * __fg;
return;
} else
while (!isdigit(__c)) __c = getc();
double __t = 1e-1;
while (isdigit(__c))
__x = __x + 1.0 * (__c - '0') * __t, __t = __t * 0.1, __c = getc();
__x = __x * __fg;
}
inline void reads(char *__s, int __x) {
char __c = getc();
int __tot = __x - 1;
while (__c < '!' || __c > '~') __c = getc();
while (__c >= '!' && __c <= '~') __s[++__tot] = __c, __c = getc();
__s[++__tot] = '\0';
}
char __obuf[__S], *__oS = __obuf, *__oT = __oS + __S - 1, __c, __qu[55];
int __qr;
inline void flush() {
fwrite(__obuf, 1, __oS - __obuf, stdout);
__oS = __obuf;
}
inline void putc(char __x) {
*__oS++ = __x;
if (__oS == __oT) flush();
}
template <class __I>
inline void print(__I __x) {
if (!__x) putc('0');
if (__x < 0) putc('-'), __x = -__x;
while (__x) __qu[++__qr] = __x % 10 + '0', __x /= 10;
while (__qr) putc(__qu[__qr--]);
}
inline void prints(const char *__s, const int __x) {
int __len = strlen(__s + __x);
for (int __i = __x; __i < __len + __x; __i++) putc(__s[__i]);
}
inline void printd(double __x, int __d) {
long long __t = (long long)floor(__x);
print(__t);
putc('.');
__x -= (double)__t;
while (__d--) {
double __y = __x * 10.0;
__x *= 10.0;
int __c = (int)floor(__y);
putc(__c + '0');
__x -= floor(__y);
}
}
inline void el() { putc('\n'); }
inline void sp() { putc(' '); }
} // namespace IO
using namespace IO;
int n, m, q, x, y, cnt, he[100005], ne[100005 * 20], to[100005 * 20],
p[100005 * 20][2], dep[100005], lca[100005], f0[100005], f1[100005],
fa[100005], dis[100005], tg[100005];
vector<pair<int, int> > e[100005];
void add(int x, int y) {
to[cnt] = y;
ne[cnt] = he[x];
he[x] = cnt++;
}
int find(int *f, int x) { return x == f[x] ? x : f[x] = find(f, f[x]); }
void dfs1(int x) {
for (int i = he[x], y; ~i; i = ne[i])
if (!dep[y = to[i]]) {
fa[y] = x;
dep[y] = dep[x] + 1;
dfs1(y);
f1[y] = x;
if (find(f0, x) == find(f0, y)) tg[x] |= tg[y];
} else if (dep[y] + 1 < dep[x]) {
int z = find(f0, x);
while (dep[z] > dep[y] + 1) f0[z] = fa[z], z = find(f0, z);
if ((dep[x] - dep[y]) % 2 == 0) tg[x] = 1;
}
for (int i = 0; i < e[x].size(); i++)
lca[e[x][i].second] = find(f1, e[x][i].first);
}
void dfs2(int x) {
if (tg[find(f0, x)]) dis[x]++;
for (int i = he[x], y; ~i; i = ne[i])
if (dep[y = to[i]] == dep[x] + 1) {
if (find(f0, x) == find(f0, y)) tg[y] |= tg[x];
dis[y] = dis[x];
dfs2(y);
}
}
bool gao(int x, int y, int z) {
if (find(f1, x) != find(f1, y)) return 0;
return (dep[x] - dep[y]) % 2 || dis[x] + dis[y] - 2 * dis[z] > 0;
}
int main() {
read(n);
read(m);
memset(he, -1, sizeof(he));
for (int i = 1; i <= m; i++) read(x), read(y), add(x, y), add(y, x);
read(q);
for (int i = 1; i <= q; i++) read(p[i][0]), read(p[i][1]);
for (int i = 1; i <= q; i++)
e[p[i][0]].push_back(make_pair(p[i][1], i)),
e[p[i][1]].push_back(make_pair(p[i][0], i));
for (int i = 1; i <= n; i++) f1[i] = f0[i] = i;
for (int i = 1; i <= n; i++)
if (!dep[i]) dep[i] = 1, dfs1(i), dfs2(i);
for (int i = 1; i <= q; i++)
prints(gao(p[i][0], p[i][1], lca[i]) ? "Yes\n" : "No\n", 0);
flush();
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const double pi = 3.14159265358979323846;
struct UnionFind {
vector<int> p, rank;
void assign(int N) {
rank.assign(N, 0);
p.resize(N);
for (int i = 0; i < N; ++i) p[i] = i;
}
UnionFind(int N) {
rank.assign(N, 0);
p.resize(N);
for (int i = 0; i < N; ++i) p[i] = i;
}
int findSet(int i) { return (p[i] == i ? i : (p[i] = findSet(p[i]))); }
bool isSameSet(int i, int j) { return findSet(i) == findSet(j); }
void unionSet(int i, int j) {
if (!isSameSet(i, j)) {
int x = findSet(i), y = findSet(j);
if (rank[x] > rank[y])
p[y] = x;
else {
p[x] = y;
if (rank[x] == rank[y]) rank[y]++;
}
}
}
};
int n, m;
vector<int> dfs_low, dfs_level;
vector<vector<int>> graph;
stack<pair<int, int>> S;
set<pair<int, int>> dirty;
bool tarjanBCC(int u, int level, int parent) {
dfs_low[u] = dfs_level[u] = level;
bool dirt = false;
for (int v : graph[u]) {
if (parent == v) continue;
if (dfs_level[v] == -1) {
S.emplace(u, v);
bool aux = tarjanBCC(v, level + 1, u);
dfs_low[u] = min(dfs_low[u], dfs_low[v]);
if (parent == -1 || (parent != -1 && dfs_low[v] >= dfs_level[u])) {
while (true) {
pair<int, int> P = S.top();
S.pop();
if (aux) dirty.insert(P);
if (P == pair<int, int>(u, v)) break;
}
} else
dirt = aux || dirt;
} else if (dfs_level[v] < dfs_level[u]) {
S.emplace(u, v);
dfs_low[u] = min(dfs_low[u], dfs_level[v]);
if ((dfs_level[u] - dfs_level[v]) % 2 == 0) dirt = true;
}
}
return dirt;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
UnionFind Connected(n);
graph.resize(n);
for (int i = 0; i < m; ++i) {
int a, b;
cin >> a >> b;
graph[--a].push_back(--b);
graph[b].push_back(a);
Connected.unionSet(a, b);
}
dfs_low.assign(n, -1);
dfs_level.assign(n, -1);
for (int u = 0; u < n; ++u)
if (dfs_level[u] == -1) tarjanBCC(u, 0, -1);
vector<int> color(n, -1);
UnionFind U(n);
for (int node = 0; node < n; ++node)
if (color[node] == -1) {
color[node] = 0;
queue<int> q;
q.push(node);
while (!q.empty()) {
int u = q.front();
q.pop();
for (int v : graph[u])
if (color[v] == -1 && !dirty.count(pair<int, int>(u, v)) &&
!dirty.count(pair<int, int>(v, u))) {
color[v] = (color[u] == 0 ? 1 : 0);
U.unionSet(u, v);
q.push(v);
}
}
}
int Q;
cin >> Q;
while (Q--) {
int a, b;
cin >> a >> b;
--a;
--b;
if (a == b || !Connected.isSameSet(a, b))
cout << "No\n";
else if (!U.isSameSet(a, b))
cout << "Yes\n";
else
cout << (color[a] != color[b] ? "Yes\n" : "No\n");
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
struct edge {
int to, next;
} e[200010];
int n, m, q, cnt, father[100010], fa[100010][30], a[100010], dis[100010],
deep[100010], head[100010];
inline void add(int u, int v) {
cnt++;
e[cnt].to = v;
e[cnt].next = head[u];
head[u] = cnt;
}
inline int find(int x) {
return father[x] == x ? x : father[x] = find(father[x]);
}
inline void dfs(int u) {
deep[u] = deep[fa[u][0]] + 1;
for (int i = head[u]; i; i = e[i].next) {
int v = e[i].to;
if (!deep[v]) {
fa[v][0] = u;
dfs(v);
if (find(u) == find(v)) a[u] |= a[v];
} else if (deep[v] < deep[u] - 1) {
if ((deep[u] + deep[v] + 1) & 1) a[u] = 1;
for (int x = find(u); deep[x] > deep[v] + 1; x = find(x))
father[x] = fa[x][0];
}
}
}
inline void dfs2(int u) {
dis[u] += a[u];
for (int i = head[u]; i; i = e[i].next) {
int v = e[i].to;
if (deep[v] != deep[u] + 1) continue;
if (find(u) == find(v)) a[v] |= a[u];
dis[v] = dis[u];
dfs2(v);
}
}
inline int LCA(int x, int y) {
if (deep[x] > deep[y]) swap(x, y);
for (int i = 20; i >= 0; i--)
if (deep[fa[y][i]] >= deep[x]) y = fa[y][i];
if (x == y) return x;
for (int i = 20; i >= 0; i--)
if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i];
return fa[x][0];
}
inline bool check(int u, int v) {
if (!LCA(u, v)) return 0;
if ((deep[u] + deep[v]) & 1) return 1;
if (dis[u] + dis[v] - 2 * dis[LCA(u, v)]) return 1;
return 0;
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) father[i] = i;
for (int i = 1, x, y; i <= m; i++) {
scanf("%d%d", &x, &y);
add(x, y);
add(y, x);
}
for (int i = 1; i <= n; i++)
if (!deep[i]) dfs(i), dfs2(i);
for (int i = 1; i <= 20; i++)
for (int j = 1; j <= n; j++) fa[j][i] = fa[fa[j][i - 1]][i - 1];
scanf("%d", &q);
while (q--) {
int x, y;
scanf("%d%d", &x, &y);
if (check(x, y))
puts("Yes");
else
puts("No");
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 100010;
vector<int> G[N];
int n, m, Q, dep[N], fa[N], ffa[N];
int hson[N], top[N], sz[N], sum[N];
int dfn[N], dfc = 0;
bool vis[N], ans[N];
int qu[N], qv[N];
int idx[N];
int find(int x) { return ffa[x] == x ? x : ffa[x] = find(ffa[x]); }
void dfs1(int u) {
vis[u] = 1;
sz[u] = 1;
for (int v : G[u]) {
if (vis[v]) continue;
dep[v] = dep[u] + 1;
fa[v] = u;
dfs1(v);
sz[u] += sz[v];
if (sz[v] > sz[hson[u]]) hson[u] = v;
}
}
void dfs2(int u, int tp) {
dfn[u] = ++dfc;
top[u] = tp;
if (hson[u]) dfs2(hson[u], tp);
for (int v : G[u])
if (fa[v] == u && v != hson[u]) dfs2(v, v);
}
int LCA(int x, int y) {
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]]) swap(x, y);
x = fa[top[x]];
}
return dep[x] < dep[y] ? x : y;
}
bool query(int x, int y) {
if (find(x) != find(y)) return 0;
if ((dep[x] & 1) != (dep[y] & 1)) return 1;
return sum[x] + sum[y] - 2 * sum[LCA(x, y)];
}
void build(int u) {
vis[u] = 1;
for (int v : G[u]) {
if (fa[v] == u || fa[u] == v) continue;
if ((dep[u] & 1) != (dep[v] & 1)) continue;
sum[u]++;
sum[v]++;
sum[LCA(u, v)] -= 2;
}
for (int v : G[u])
if (!vis[v] && fa[v] == u) build(v);
}
void reduction1(int u) {
for (int v : G[u]) {
if (fa[v] != u) continue;
reduction1(v);
sum[u] += sum[v];
}
}
void reduction2(int u) {
for (int v : G[u]) {
if (fa[v] != u) continue;
sum[v] += sum[u];
reduction2(v);
}
}
void clear() {
memset(vis, 0, sizeof(vis));
memset(top, 0, sizeof(top));
memset(hson, 0, sizeof(hson));
memset(sz, 0, sizeof(sz));
memset(dep, 0, sizeof(dep));
memset(sum, 0, sizeof(sum));
memset(fa, 0, sizeof(fa));
dfc = 0;
memset(dfn, 0, sizeof(dfn));
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) ffa[i] = i;
for (int i = 1, u, v; i <= m; i++) {
scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
if (find(u) != find(v)) ffa[find(u)] = find(v);
}
scanf("%d", &Q);
for (int i = 1; i <= Q; i++) scanf("%d%d", qu + i, qv + i);
for (int i = 1; i <= n; i++) idx[i] = i;
for (int tms = 7; tms; tms--) {
random_shuffle(idx + 1, idx + 1 + n);
for (int i = 1; i <= n; i++) random_shuffle(G[i].begin(), G[i].end());
clear();
for (int i = 1; i <= n; i++)
if (!vis[idx[i]]) dfs1(idx[i]), dfs2(idx[i], idx[i]);
memset(vis, 0, sizeof(vis));
for (int i = 1; i <= n; i++)
if (!vis[idx[i]]) {
build(idx[i]);
reduction1(idx[i]);
reduction2(idx[i]);
}
for (int i = 1; i <= Q; i++)
if (!ans[i]) ans[i] = query(qu[i], qv[i]);
}
for (int i = 1; i <= Q; i++) puts(ans[i] ? "Yes" : "No");
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int n, m, q;
int to[200010], nxt[200010], fst[100010], tot = 1;
void add(int u, int v) {
to[++tot] = v;
nxt[tot] = fst[u];
fst[u] = tot;
}
int d[100010], bl[100010], cnt;
int stk[200010], no[100010], top, bccno;
int anc[100010][17], dfn[100010], clk;
int dfs(int u) {
bl[u] = cnt;
d[u] = d[anc[u][0]] + 1;
int low = dfn[u] = ++clk, lowv;
for (int e = fst[u]; e; e = nxt[e]) {
int v = to[e];
if (!dfn[v]) {
stk[++top] = e;
anc[v][0] = u, lowv = dfs(v);
low = min(low, lowv);
if (lowv >= dfn[u]) {
++bccno;
do no[stk[top] >> 1] = bccno;
while (stk[top--] != e);
}
} else if (dfn[v] < dfn[u] && v != anc[u][0]) {
low = min(low, dfn[v]);
stk[++top] = e;
}
}
return low;
}
int z[17] = {1};
bool val[100010][17], ok[100010];
bool work(int u, int v) {
int l = 0, i;
bool ok = 0;
if (d[u] < d[v]) swap(u, v);
i = 16;
while (!ok && d[u] != d[v]) {
while (d[anc[u][i]] < d[v]) --i;
ok = val[u][i], u = anc[u][i];
l += z[i];
}
i = 16;
while (!ok && u != v) {
while (i && anc[u][i] == anc[v][i]) --i;
l += z[i + 1], ok = val[u][i] | val[v][i];
u = anc[u][i], v = anc[v][i];
}
return ok || l & 1;
}
int main() {
for (int i = 1; i <= 16; ++i) z[i] = z[i - 1] << 1;
scanf("%d%d", &n, &m);
for (int i = 1, u, v; i <= m; ++i) {
scanf("%d%d", &u, &v);
add(u, v), add(v, u);
}
for (int i = 1; i <= n; ++i)
if (!dfn[i]) ++cnt, dfs(i);
for (int e = 2, u, v; e <= tot; e += 2) {
u = to[e], v = to[e | 1];
if (d[u] < d[v]) swap(u, v);
if (anc[u][0] != v && !((d[u] - d[v]) & 1)) ok[no[e >> 1]] = 1;
}
for (int e = 2, u, v; e <= tot; e += 2) {
u = to[e], v = to[e | 1];
if (d[u] < d[v]) swap(u, v);
if (anc[u][0] == v) val[u][0] = ok[no[e >> 1]];
}
for (int i = 1; i <= 16; ++i)
for (int u = 1; u <= n; ++u) {
anc[u][i] = anc[anc[u][i - 1]][i - 1];
val[u][i] = val[u][i - 1] | val[anc[u][i - 1]][i - 1];
}
scanf("%d", &q);
for (int i = 1, u, v; i <= q; ++i) {
scanf("%d%d", &u, &v);
puts(bl[u] == bl[v] && work(u, v) ? "Yes" : "No");
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int n, m;
int k;
bool debug = false;
int f[100005], cid[100005], cc, deep[100005], par[100005], lca[18][100005],
D[100005];
bool mark[100005], col[100005], spec[100005], S[100005];
int ff(int x) { return x == f[x] ? x : f[x] = ff(f[x]); }
vector<int> mp[100005];
void dfs(int i, int p, int d) {
cid[i] = cc;
col[i] = col[p] ^ 1;
deep[i] = d;
par[i] = p;
for (auto child : mp[i]) {
if (!cid[child]) {
dfs(child, i, d + 1);
} else {
int tmp = deep[child] + 1;
for (int j = i; deep[j] > tmp; j = ff(j)) {
f[ff(j)] = ff(par[j]);
}
}
}
}
void dd(int i, int p) {
mark[i] = 1;
D[i] += spec[i];
lca[0][i] = p;
for (int j = 1; j < 18; j++) {
lca[j][i] = lca[j - 1][lca[j - 1][i]];
}
for (auto child : mp[i])
if (!mark[child]) {
D[child] = D[i];
dd(child, i);
}
}
int find(int u, int v) {
if (deep[u] < deep[v]) swap(u, v);
int dif = deep[u] - deep[v];
for (int k = 0; k < 18; k++) {
if (dif >> k & 1) u = lca[k][u];
}
if (u == v) return v;
for (int k = 17; k >= 0; k--)
if (lca[k][u] != lca[k][v]) {
u = lca[k][u];
v = lca[k][v];
}
return lca[0][u];
}
int main() {
int q, u, v;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) f[i] = i;
for (int i = 0; i < m; i++) {
scanf("%d%d", &u, &v);
mp[u].push_back(v);
mp[v].push_back(u);
}
for (int i = 1; i <= n; i++)
if (!cid[i]) {
cc++;
dfs(i, 0, 1);
}
for (int i = 1; i <= n; i++)
for (auto child : mp[i]) {
if (col[child] == col[i]) {
S[ff(deep[child] > deep[i] ? child : i)] = 1;
}
}
for (int i = 1; i <= n; i++)
if (S[ff(i)]) {
spec[i] = 1;
}
for (int i = 1; i <= n; i++)
if (!mark[i]) {
dd(i, 0);
}
scanf("%d", &q);
while (q--) {
scanf("%d%d", &u, &v);
if (u == v || cid[u] != cid[v])
puts("No");
else if (col[u] != col[v])
puts("Yes");
else {
int pa = find(u, v);
int ans = D[u] + D[v] - 2 * D[pa];
puts(ans ? "Yes" : "No");
}
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 10;
struct Edge {
int next, v;
Edge() {}
Edge(int next, int v) : next(next), v(v) {}
} e[maxn << 1];
struct Node {
int val, pos;
Node() {}
Node(int val, int pos) : val(val), pos(pos) {}
};
int n, m;
int head[maxn], cnt;
int dfn[maxn], low[maxn], timer;
int dep[maxn], dfs_seq[maxn];
int vis[maxn], num;
int anc[maxn][20];
int dis[maxn][20];
int st[maxn << 1], top;
int bcc_cnt;
set<int> bcc[maxn];
bool odd[maxn], up[maxn], used[maxn << 1];
inline int read() {
int x = 0;
char ch = getchar();
while (ch < '0' || ch > '9') ch = getchar();
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x;
}
inline void AddEdge(int u, int v) {
e[++cnt] = Edge(head[u], v);
head[u] = cnt;
e[++cnt] = Edge(head[v], u);
head[v] = cnt;
}
void dfs(int u, int fa = 0) {
vis[dfs_seq[++timer] = u] = num;
dfn[u] = low[u] = timer;
for (int i = head[u]; i; i = e[i].next) {
int v = e[i].v;
if (!dfn[v]) {
dep[v] = dep[u] + 1;
anc[v][0] = u;
st[++top] = i;
dfs(v, u);
low[u] = min(low[u], low[v]);
if (low[v] >= dfn[u]) {
++bcc_cnt;
int pos;
do {
pos = st[top--];
bcc[bcc_cnt].insert(e[pos].v);
bcc[bcc_cnt].insert(e[pos ^ 1].v);
if (used[pos] || used[pos ^ 1]) odd[bcc_cnt] = 1;
} while (pos != i);
}
} else if (dfn[v] < dfn[u] && v != fa) {
st[++top] = i;
low[u] = min(low[u], dfn[v]);
if (!((dep[u] - dep[v]) & 1)) used[i] = used[i ^ 1] = 1;
}
}
}
inline Node LCA(int u, int v) {
if (dep[u] < dep[v]) swap(u, v);
int res = 0, h = dep[u] - dep[v], d = 0;
while (h) {
if (h & 1) res += dis[u][d], u = anc[u][d];
d++, h >>= 1;
}
if (u == v) return Node(res, u);
for (int i = 18; i >= 0; i--)
if (anc[u][i] != anc[v][i]) {
res += dis[u][i] + dis[v][i];
u = anc[u][i], v = anc[v][i];
}
return Node(res + dis[u][0] + dis[v][0], anc[u][0]);
}
int main() {
if (fopen("sunset.in", "r") != NULL) {
freopen("sunset.in", "r", stdin);
freopen("sunset.out", "w", stdout);
}
cnt = 1;
n = read(), m = read();
for (int i = 1; i <= m; i++) AddEdge(read(), read());
for (int i = 1; i <= n; i++)
if (!vis[i]) {
num++;
dep[i] = 1;
dfs(i);
}
for (int i = 1; i <= bcc_cnt; i++)
if (odd[i])
for (set<int>::iterator SB = bcc[i].begin(); SB != bcc[i].end(); SB++) {
int u = *SB;
up[u] = bcc[i].find(anc[u][0]) != bcc[i].end();
}
for (int i = 1; i <= n; i++) {
int u = dfs_seq[i];
dis[u][0] = up[u];
for (int j = 0; anc[anc[u][j]][j]; j++) {
anc[u][j + 1] = anc[anc[u][j]][j];
dis[u][j + 1] = dis[anc[u][j]][j] + dis[u][j];
}
}
int q = read(), x, y;
while (q--) {
x = read(), y = read();
if (vis[x] != vis[y] || x == y) {
puts("No");
continue;
}
Node cur = LCA(x, y);
int lca = cur.pos;
if ((dep[x] + dep[y] - 2 * dep[lca]) & 1) {
puts("Yes");
continue;
}
if (cur.val)
puts("Yes");
else
puts("No");
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 5;
int nex[maxn], to[maxn], head[maxn], s[maxn];
int e = 1;
void add(int x, int y) {
nex[++e] = head[x];
head[x] = e;
to[e] = y;
}
int dfn[maxn], low[maxn], st[maxn], dep[maxn];
int p[maxn][27];
bool root[maxn];
int dfs_clock;
int top, color;
bool pd[maxn];
int c[maxn];
int odd[maxn];
void dfs(int x, int fa) {
c[x] = color;
dfn[x] = low[x] = ++dfs_clock;
dep[x] = dep[fa] + 1;
p[x][0] = fa;
st[++top] = x;
pd[x] = 1;
for (int i = 1; (1 << i) <= dep[x]; i++) {
p[x][i] = p[p[x][i - 1]][i - 1];
}
for (int i = head[x]; i; i = nex[i]) {
int u = to[i];
if (u == fa) continue;
if (!dfn[u]) {
dfs(u, x);
low[x] = min(low[x], low[u]);
} else if (pd[u]) {
low[x] = min(low[x], low[u]);
if (dep[x] % 2 == dep[u] % 2) {
odd[x] = 1;
}
}
}
pd[x] = 0;
if (low[x] == dfn[x]) {
bool flag = 0;
for (int i = top; i >= 1; i--) {
if (st[i] == x) break;
if (odd[st[i]]) {
flag = 1;
break;
}
}
if (flag) {
while (top > 0 && st[top] != x) {
s[st[top]]++;
pd[st[top]] = 0;
top--;
}
top--;
} else {
while (top > 0 && st[top] != x) {
pd[st[top]] = 0;
top--;
}
top--;
}
}
}
int lca(int x, int y) {
if (dep[x] < dep[y]) swap(x, y);
for (int i = 25; i >= 0; i--) {
if (dep[p[x][i]] >= dep[y]) x = p[x][i];
}
if (x == y) return x;
for (int i = 25; i >= 0; i--) {
if (p[x][i] != p[y][i]) {
x = p[x][i];
y = p[y][i];
}
}
return p[x][0];
}
void dfs1(int x) {
for (int i = head[x]; i; i = nex[i]) {
int u = to[i];
if (p[u][0] == x) {
s[u] += s[x];
dfs1(u);
}
}
}
int main() {
int i, j, k, m, n;
scanf("%d%d", &n, &m);
for (i = 1; i <= m; i++) {
int x, y;
scanf("%d%d", &x, &y);
add(x, y);
add(y, x);
}
for (int i = 1; i <= n; i++) {
if (!c[i]) {
color++;
dfs(i, 0);
root[i] = 1;
}
}
for (i = 1; i <= n; i++) {
if (root[i]) dfs1(i);
}
int q;
scanf("%d", &q);
while (q--) {
int x, y;
scanf("%d%d", &x, &y);
if (c[x] != c[y]) {
printf("No\n");
continue;
}
int anc = lca(x, y);
if ((dep[x] + dep[y] - 2 * dep[anc]) % 2 || s[x] + s[y] - 2 * s[anc] > 0) {
printf("Yes\n");
} else
printf("No\n");
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 200005;
char buf[1 << 20], *p1, *p2;
inline int _R() {
int d = 0;
bool ty = 1;
char t;
while (t = (p1 == p2 &&
(p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
? 0
: *p1++),
(t < '0' || t > '9') && t != '-')
;
t == '-' ? (ty = 0) : (d = t - '0');
while (t = (p1 == p2 &&
(p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
? 0
: *p1++),
t >= '0' && t <= '9')
d = (d << 3) + (d << 1) + t - '0';
return ty ? d : -d;
}
int n, m, q;
int Tote, Eu[N], Ev[N], Last[N], Next[N], End[N];
bool mark[N];
void ADDE(int id) {
int x = _R(), y = _R();
Eu[id] = x, Ev[id] = y;
End[++Tote] = y, Next[Tote] = Last[x], Last[x] = Tote;
End[++Tote] = x, Next[Tote] = Last[y], Last[y] = Tote;
}
int vtt, top, scc;
int id[N], s[N], dfn[N], low[N];
bool has[N];
void circle(int x) {
scc++;
int u;
do u = s[top--], id[u] = scc;
while (u != x);
}
void tarjan(int x, int fa) {
dfn[x] = low[x] = ++vtt;
s[++top] = x;
for (int u, i = Last[x]; i; i = Next[i])
if (u = End[i], u != fa) {
if (!dfn[u]) {
tarjan(u, x);
low[x] = min(low[x], low[u]);
if (low[u] > dfn[x]) circle(u);
} else
low[x] = min(low[x], dfn[u]);
}
}
int dep[N], ufa[N], Fa[18][N];
bool Ans[18][N];
vector<int> G[N];
int gf(int x) { return x == ufa[x] ? x : ufa[x] = gf(ufa[x]); }
void LINK(int id) {
int x = Eu[id], y = Ev[id];
mark[id] = 1;
ufa[ufa[x]] = ufa[y];
G[x].push_back(y);
G[y].push_back(x);
}
void dfs(int x, int fa) {
dep[x] = dep[fa] + 1;
for (auto u : G[x])
if (u != fa) dfs(u, x);
}
void dfs2(int x, int fa) {
Fa[0][x] = fa;
for (int i = 1; i < 18; i++)
Fa[i][x] = Fa[i - 1][Fa[i - 1][x]],
Ans[i][x] = Ans[i - 1][x] | Ans[i - 1][Fa[i - 1][x]];
for (auto u : G[x])
if (u != fa) dfs2(u, x);
}
bool GA(int x, int y) {
if (x == y) return 0;
if (dep[y] > dep[x]) swap(x, y);
int i, ans = (dep[x] + dep[y]) & 1, t = dep[x] - dep[y];
for (i = 0; i < 18; i++)
if (t & (1 << i)) ans |= Ans[i][x], x = Fa[i][x];
if (x == y) return ans;
for (i = 17; ~i; i--)
if (Fa[i][x] != Fa[i][y])
ans |= Ans[i][x] | Ans[i][y], x = Fa[i][x], y = Fa[i][y];
return ans | Ans[0][x] | Ans[0][y];
}
int main() {
int i, x, y;
n = _R(), m = _R();
for (i = 1; i <= m; i++) ADDE(i);
for (i = 1; i <= n; i++)
if (!dfn[i]) tarjan(i, 0), circle(i);
for (i = 1; i <= n; i++) ufa[i] = i;
for (i = 1; i <= m; i++)
if (gf(Eu[i]) != gf(Ev[i])) LINK(i);
for (i = 1; i <= n; i++)
if (!dep[i]) dfs(i, 0);
for (i = 1; i <= m; i++)
if (!mark[i] && id[Eu[i]] == id[Ev[i]])
has[id[Eu[i]]] |= ((dep[Eu[i]] ^ dep[Ev[i]]) & 1) ^ 1;
for (i = 1; i <= m; i++)
if (mark[i] && id[Eu[i]] == id[Ev[i]])
x = dep[Eu[i]] > dep[Ev[i]] ? Eu[i] : Ev[i], Ans[0][x] = has[id[x]];
for (i = 1; i <= n; i++)
if (!Fa[0][i]) dfs2(i, 0);
q = _R();
while (q--) {
x = _R(), y = _R();
if (gf(x) != gf(y))
puts("No");
else
puts(GA(x, y) ? "Yes" : "No");
}
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int n, m, q, cnt, vis[100010], used[100010], dep[100010], val[100010],
sum[100010], p[100010], par[21][100010];
vector<int> v[100010], g[100010];
vector<pair<int, int> > e;
inline int find_set(int x) { return p[x] == x ? x : p[x] = find_set(p[x]); }
inline void dfs(int x, int pr, int dist) {
vis[x] = cnt;
par[0][x] = pr;
dep[x] = dist;
for (int i = 0; i < v[x].size(); i++)
if (v[x][i] != pr) {
int u = v[x][i];
if (vis[u]) {
if (dep[x] < dep[u]) e.push_back(make_pair(x, u));
} else {
g[x].push_back(u);
g[u].push_back(x);
dfs(u, x, dist + 1);
}
}
}
inline void init() {
for (int i = 0; i < 20; i++) {
for (int j = 1; j <= n; j++) {
if (par[i][j] == -1)
par[i + 1][j] = -1;
else
par[i + 1][j] = par[i][par[i][j]];
}
}
}
inline int lca(int x, int y) {
if (dep[x] > dep[y]) swap(x, y);
int k = dep[y] - dep[x];
for (int i = 0; i <= 20; i++)
if (k & (1 << i)) {
y = par[i][y];
}
if (x == y) return x;
for (int i = 20; i >= 0; i--) {
if (par[i][x] != par[i][y]) {
x = par[i][x];
y = par[i][y];
}
}
return par[0][x];
}
inline void dfs2(int x, int pr) {
used[x] = 1;
for (int i = 0; i < g[x].size(); i++)
if (g[x][i] != pr) {
dfs2(g[x][i], x);
if (find_set(x) == find_set(g[x][i])) val[x] |= val[g[x][i]];
}
}
inline void dfs3(int x, int pr) {
used[x] = 1;
for (int i = 0; i < g[x].size(); i++)
if (g[x][i] != pr) {
if (find_set(x) == find_set(g[x][i])) val[g[x][i]] |= val[x];
dfs3(g[x][i], x);
}
}
inline void dfs4(int x, int pr) {
used[x] = 1;
sum[x] += val[x];
for (int i = 0; i < g[x].size(); i++)
if (g[x][i] != pr) {
sum[g[x][i]] += sum[x];
dfs4(g[x][i], x);
}
}
signed main() {
ios::sync_with_stdio(false);
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int x, y;
cin >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
for (int i = 1; i <= n; i++)
if (!vis[i]) cnt++, dfs(i, -1, 0);
init();
for (int i = 1; i <= n; i++) p[i] = i;
for (int i = 0; i < e.size(); i++) {
int x = e[i].first, y = e[i].second;
int pos = find_set(y);
while (dep[pos] > dep[x] + 1) {
p[pos] = par[0][pos];
pos = find_set(pos);
}
if ((dep[y] - dep[x]) % 2 == 0) {
val[y] = 1;
}
}
memset(used, 0, sizeof(used));
for (int i = 1; i <= n; i++)
if (!used[i]) dfs2(i, -1);
memset(used, 0, sizeof(used));
for (int i = 1; i <= n; i++)
if (!used[i]) dfs3(i, -1);
memset(used, 0, sizeof(used));
for (int i = 1; i <= n; i++)
if (!used[i]) dfs4(i, -1);
cin >> q;
for (int i = 1; i <= q; i++) {
int x, y;
cin >> x >> y;
if (vis[x] != vis[y]) {
cout << "No\n";
} else {
int l = lca(x, y);
if ((dep[x] + dep[y] - 2 * dep[l]) % 2 == 1) {
cout << "Yes\n";
} else if ((sum[x] + sum[y] - 2 * sum[l]) > 0) {
cout << "Yes\n";
} else
cout << "No\n";
}
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const long long inf = 0x7f7f7f7f7f7f;
long long mod;
const long double eps = 1e-8;
inline long long add(long long x) { return x >= mod ? x - mod : x; }
inline long long sub(long long x) { return x < 0 ? x + mod : x; }
inline void Add(long long &x, long long y) {
if ((x += y) >= mod) x -= mod;
}
inline void Sub(long long &x, long long y) {
if ((x -= y) < 0) x += mod;
}
long long read() {
long long x = 0, f = 1;
char c = getchar();
while (!isdigit(c)) {
if (c == '-') f = -1;
c = getchar();
}
while (isdigit(c)) {
x = (x << 3) + (x << 1) + (c ^ 48);
c = getchar();
}
return x * f;
}
const long long N = 1e5 + 10;
long long n, m, q;
long long fa[N];
long long bz[18][N];
long long dep[N], s[N], a[N];
std::vector<long long> e[N];
inline long long find_r(long long x) {
return x == fa[x] ? x : fa[x] = find_r(fa[x]);
}
void dfs1(long long u) {
dep[u] = dep[bz[0][u]] + 1;
for (long long v : e[u]) {
if (!dep[v]) {
bz[0][v] = u;
dfs1(v);
if (find_r(u) == find_r(v)) a[u] |= a[v];
} else if (dep[v] < dep[u] - 1) {
if ((dep[u] + dep[v] + 1) & 1) a[u] = 1;
for (long long x = find_r(u); dep[x] > dep[v] + 1; x = find_r(x))
fa[x] = bz[0][x];
}
}
}
void dfs2(long long u) {
s[u] += a[u];
for (long long v : e[u]) {
if (dep[v] == dep[u] + 1) {
if (find_r(u) == find_r(v)) a[v] |= a[u];
s[v] = s[u];
dfs2(v);
}
}
}
long long LCA(long long x, long long y) {
if (dep[x] < dep[y]) swap(x, y);
for (long long i = 16; i >= 0; i--)
if (dep[bz[i][x]] >= dep[y]) x = bz[i][x];
if (x == y) return x;
for (long long i = 16; i >= 0; i--)
if (bz[i][x] != bz[i][y]) x = bz[i][x], y = bz[i][y];
return bz[0][x];
}
signed main() {
n = read(), m = read();
for (long long i = 1; i <= m; i++) {
long long u = read(), v = read();
e[u].push_back(v), e[v].push_back(u);
}
for (long long i = 1; i <= n; i++) fa[i] = i;
for (long long i = 1; i <= n; i++)
if (!dep[i]) dfs1(i), dfs2(i);
for (long long j = 1; j <= 16; j++)
for (long long i = 1; i <= n; i++) bz[j][i] = bz[j - 1][bz[j - 1][i]];
q = read();
while (q--) {
long long u = read(), v = read();
long long l_a = LCA(u, v);
if (!l_a)
puts("No");
else if (((dep[u] + dep[v]) & 1) || s[u] + s[v] - 2 * s[l_a])
puts("Yes");
else
puts("No");
}
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n, m, q, dfn[N], low[N], dfs_clock, sta1[N * 2], top1, scc_num,
scc_id[N * 2], edgenum;
int fa[N][21], vet[N * 2], head[N], Next[N * 2], dep[N], beg[N * 2],
sta2[N * 4], top2, w[N][21];
bool flag[N * 2], vis[N];
bool used[N];
void addedge(int u, int v) {
vet[++edgenum] = v;
beg[edgenum] = u;
Next[edgenum] = head[u];
head[u] = edgenum;
}
void Tarjan(int u, int father) {
dfn[u] = low[u] = ++dfs_clock;
for (int e = head[u]; e; e = Next[e]) {
int v = vet[e];
int x = e;
if (!scc_id[(e + 1) / 2]) sta1[++top1] = e;
if (v == father) continue;
if (!dfn[v]) {
Tarjan(v, u);
low[u] = min(low[u], low[v]);
if (low[v] >= dfn[u]) {
top2 = 0;
int x;
x = sta1[top1];
scc_num++;
bool flag1 = 0;
do {
x = sta1[top1--];
scc_id[(x + 1) / 2] = scc_num;
sta2[++top2] = x;
int d1 = dep[beg[x]] % 2, d2 = dep[vet[x]] % 2;
if (d1 == d2) flag1 = 1;
} while (beg[x] != u);
if (flag1) {
while (top2) {
x = sta2[top2];
flag[sta2[top2]] = 1;
if (sta2[top2] % 2 == 0)
flag[sta2[top2--] - 1] = 1;
else
flag[sta2[top2--] + 1] = 1;
}
}
}
} else
low[u] = min(low[u], dfn[v]);
}
}
void dfs(int u, int father) {
vis[u] = 1;
dep[u] = dep[father] + 1;
fa[u][0] = father;
for (int i = 1; i < 21; i++) fa[u][i] = fa[fa[u][i - 1]][i - 1];
for (int e = head[u]; e; e = Next[e]) {
int v = vet[e];
if (!vis[v]) dfs(v, u);
}
}
int LCA(int u, int v, int &l) {
if (dep[u] < dep[v]) swap(u, v);
int delta = dep[u] - dep[v], ret = 0;
for (int i = 0; i < 21; i++)
if (delta & (1 << i)) ret += w[u][i], u = fa[u][i];
if (u == v) {
l = u;
return ret;
}
for (int i = 20; i >= 0; i--)
if (fa[u][i] != fa[v][i]) {
ret += w[u][i] + w[v][i];
u = fa[u][i];
v = fa[v][i];
}
l = fa[u][0];
return ret + w[u][0] + w[v][0];
}
void dfs1(int u, int father) {
vis[u] = 1;
for (int i = 1; i < 21; i++) w[u][i] = w[u][i - 1] + w[fa[u][i - 1]][i - 1];
for (int e = head[u]; e; e = Next[e]) {
int v = vet[e];
if (!vis[v]) {
if (flag[e]) w[v][0] = 1;
dfs1(v, u);
}
}
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++) {
int u, v;
scanf("%d%d", &u, &v);
addedge(u, v);
addedge(v, u);
}
for (int i = 1; i <= n; i++)
if (!vis[i]) dfs(i, 0);
for (int i = 1; i <= n; i++)
if (!dfn[i]) Tarjan(i, 0);
memset(vis, 0, sizeof(vis));
for (int i = 1; i <= n; i++)
if (!vis[i]) dfs1(i, 0);
scanf("%d", &q);
while (q--) {
int u, v, l;
scanf("%d%d", &u, &v);
int dist = LCA(u, v, l);
if (l == 0 || u == v) {
puts("No");
continue;
}
int dis = dep[u] + dep[v] - 2 * dep[l];
if (dis & 1)
puts("Yes");
else {
if (dist)
puts("Yes");
else
puts("No");
}
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const double eps = 1e-10;
const int oo = ~0u >> 2, mo = (int)1e9 + 7;
const int mn = 110000, mm = 110000 * 2, ml = 18;
struct Edge {
int y, l;
} E[mm];
int g[mn], d[mn], s[mn], blg[mn];
int fa[mn][ml], f[mn];
bool o[mn];
int n, m, tt(1);
void add(int x, int y) {
E[++tt].y = y, E[tt].l = g[x], g[x] = tt;
E[++tt].y = x, E[tt].l = g[y], g[y] = tt;
}
int LOG2(int x) {
int t = (int)log2(x);
while ((1 << t) > x) --t;
while ((1 << (t + 1)) <= x) ++t;
return t;
}
int find(int x) {
int i = x, t;
while (f[i] != i) i = f[i];
while (x != i) t = f[x], f[x] = i, x = t;
return x;
}
struct Stack {
int x, i;
Stack() {}
Stack(int _x) { x = _x, i = -1; }
void init(int _x) { x = _x, i = -1; }
} Q[mn];
void init() {
for (int i = 1; i <= n; ++i)
if (!d[i]) {
int r = 0;
d[i] = 1, fa[i][0] = i, blg[i] = i, f[i] = i;
Q[++r].init(i);
while (r) {
int x = Q[r].x, &i = Q[r].i, y;
(d[0] = max(d[0], d[x]));
if (i == -1)
f[x] = x, i = g[x];
else {
y = E[i].y;
if (find(x) == find(y)) o[x] |= o[y];
i = E[i].l;
}
y = E[i].y;
if (!i) {
--r;
continue;
}
if (!d[y]) {
d[y] = d[x] + 1, fa[y][0] = x, blg[y] = blg[x], f[y] = y;
Q[++r].init(y);
} else if (d[x] > d[y] + 1) {
int z = find(x);
while (d[z] > d[y] + 1) f[z] = fa[z][0], z = find(z);
o[x] |= !((d[x] - d[y]) % 2);
}
}
Q[r = 1].init(i);
while (r) {
int x = Q[r].x, &i = Q[r].i, y;
if (i == -1)
s[x] += o[find(x)], i = g[x];
else
i = E[i].l;
y = E[i].y;
if (!i) {
--r;
continue;
}
if (d[y] == d[x] + 1) {
if (find(x) == find(y)) o[y] |= o[x];
s[y] = s[x], Q[++r].init(y);
}
}
}
int t = LOG2(d[0]);
for (int j = 1; j <= t; ++j)
for (int i = 1; i <= n; ++i) fa[i][j] = fa[fa[i][j - 1]][j - 1];
}
int lca(int x, int y) {
if (d[x] < d[y]) swap(x, y);
while (d[x] > d[y]) x = fa[x][LOG2(d[x] - d[y])];
if (x == y) return x;
for (int i = LOG2(d[x]); i >= 0; --i)
if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i];
return fa[x][0];
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; ++i) {
int x, y;
scanf("%d%d", &x, &y);
add(x, y);
}
init();
scanf("%d", &tt);
while (tt--) {
int x, y, z;
scanf("%d%d", &x, &y);
if (blg[x] != blg[y]) {
printf("No\n");
continue;
}
z = lca(x, y);
if (((d[x] + d[y]) & 1) || s[x] + s[y] - 2 * s[z])
printf("Yes\n");
else
printf("No\n");
}
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int fr[100010], sa[200010], ne[200010], v[200010], bs = 0;
void addb(int a, int b) {
sa[bs] = a;
v[bs] = b;
ne[bs] = fr[a];
fr[a] = bs++;
}
bool bk[100010], jh[100010];
int sd[100010], fa[100010], son[100010], top[100010], ro[100010], jl[100010];
vector<int> ve[100010], bc[100010], bb[100010];
int dfs1(int u, int f, int ge) {
bk[u] = true;
sd[u] = sd[f] + 1;
fa[u] = f;
son[u] = -1;
int he = 1, ma = -1;
ro[u] = ge;
for (int i = fr[u]; i != -1; i = ne[i]) {
if (!bk[v[i]]) {
ve[u].push_back(v[i]);
int rt = dfs1(v[i], u, ge);
if (rt > ma) {
ma = rt;
son[u] = v[i];
}
he += rt;
}
}
return he;
}
void dfs2(int u, int f, int tp) {
jl[u] = jl[f] + jh[u];
top[u] = tp;
if (son[u] == -1) return;
dfs2(son[u], u, tp);
for (int i = 0; i < ve[u].size(); i++) {
int a = ve[u][i];
if (a != son[u]) dfs2(a, u, a);
}
}
int st[100010], tp = 0, sb[100010], tb = 0, dfn[100010], low[100010], tm = 0,
ss = 0;
void tarjan(int u, int f) {
st[tp++] = u;
dfn[u] = low[u] = ++tm;
for (int i = fr[u]; i != -1; i = ne[i]) {
if (v[i] == f) continue;
if (!dfn[v[i]]) {
int la = tp, lb = tb;
sb[tb++] = i;
tarjan(v[i], u);
if (low[v[i]] >= dfn[u]) {
ss += 1;
for (int j = la; j < tp; j++) bc[ss].push_back(st[j]);
for (int j = lb; j < tb; j++) bb[ss].push_back(sb[j]);
tp = la;
tb = lb;
bc[ss].push_back(u);
}
low[u] = low[u] < low[v[i]] ? low[u] : low[v[i]];
} else {
low[u] = low[u] < dfn[v[i]] ? low[u] : dfn[v[i]];
sb[tb++] = i;
}
}
}
int co[100010];
vector<int> to[100010];
bool check(int u, int f) {
co[u] = 3 - f;
for (int i = 0; i < to[u].size(); i++) {
int t = to[u][i];
if (!co[t]) {
if (check(t, co[u])) return true;
} else if (co[t] == co[u])
return true;
}
return false;
}
bool check(int x) {
for (int i = 0; i < bb[x].size(); i++) {
int a = sa[bb[x][i]], b = v[bb[x][i]];
to[a].push_back(b);
to[b].push_back(a);
}
bool rt = check(bc[x][0], 1);
for (int i = 0; i < bc[x].size(); i++) {
int a = bc[x][i];
co[a] = 0;
to[a].clear();
}
return rt;
}
int getlca(int x, int y) {
while (top[x] != top[y]) {
if (sd[top[x]] > sd[top[y]])
x = fa[top[x]];
else
y = fa[top[y]];
}
if (sd[x] < sd[y])
return x;
else
return y;
}
int ge[100010];
int main() {
int n, m, q, rs = 0;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) fr[i] = -1;
for (int i = 0; i < m; i++) {
int a, b;
scanf("%d%d", &a, &b);
addb(a, b);
addb(b, a);
}
for (int i = 1; i <= n; i++) {
if (!dfn[i]) {
tarjan(i, 0);
tp = tb = 0;
}
}
for (int i = 1; i <= n; i++) {
if (!bk[i]) {
ge[rs++] = i;
dfs1(i, 0, i);
}
}
for (int i = 1; i <= ss; i++) {
if (check(i)) {
for (int j = 0; j < bb[i].size(); j++) {
int a = sa[bb[i][j]], b = v[bb[i][j]];
if (fa[a] == b)
jh[a] = true;
else if (fa[b] == a)
jh[b] = true;
}
}
}
for (int i = 0; i < rs; i++) dfs2(ge[i], 0, ge[i]);
scanf("%d", &q);
for (int i = 0; i < q; i++) {
int a, b;
scanf("%d%d", &a, &b);
if (ro[a] != ro[b])
printf("No\n");
else {
if ((sd[a] + sd[b]) % 2 == 1)
printf("Yes\n");
else {
int lc = getlca(a, b);
if (jl[a] + jl[b] - jl[lc] - jl[lc] > 0)
printf("Yes\n");
else
printf("No\n");
}
}
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int head[1000009], to[1000009], nex[1000009], cnt;
inline void add(int u, int v) {
nex[++cnt] = head[u];
head[u] = cnt;
to[cnt] = v;
}
int f[1000009][30], dep[1000009], root[1000009], num, ins[1000009], s[1000009];
int c[1000009], odd[1000009], st[1000009], top, color, dfn[1000009],
low[1000009];
void dfs(int u, int fa) {
c[u] = color;
dfn[u] = low[u] = ++num;
dep[u] = dep[fa] + 1;
f[u][0] = fa;
st[++top] = u;
ins[u] = 1;
for (int i = 1; (1 << i) <= dep[u]; i++) f[u][i] = f[f[u][i - 1]][i - 1];
for (int i = head[u]; i; i = nex[i]) {
if (to[i] == fa) continue;
if (!dfn[to[i]]) {
dfs(to[i], u);
low[u] = min(low[u], low[to[i]]);
} else if (ins[to[i]]) {
low[u] = min(dfn[to[i]], low[u]);
if (dep[u] % 2 == dep[to[i]] % 2) {
odd[u] = 1;
}
}
}
ins[u] = 0;
if (low[u] == dfn[u]) {
bool flag = 0;
for (int i = top; i >= 1; i--) {
if (st[i] == u) break;
if (odd[st[i]]) {
flag = 1;
break;
}
}
if (flag) {
while (top > 0 && st[top] != u) {
s[st[top]]++;
ins[st[top]] = 0;
top--;
}
top--;
} else {
while (top > 0 && st[top] != u) {
ins[st[top]] = 0;
top--;
}
top--;
}
}
}
int lca(int x, int y) {
if (dep[x] < dep[y]) swap(x, y);
for (int i = 25; i >= 0; i--) {
if (dep[f[x][i]] >= dep[y]) x = f[x][i];
}
if (x == y) return x;
for (int i = 25; i >= 0; i--) {
if (f[x][i] != f[y][i]) {
x = f[x][i];
y = f[y][i];
}
}
return f[x][0];
}
void dfs1(int u) {
for (int i = head[u]; i; i = nex[i])
if (f[to[i]][0] == u) {
s[to[i]] += s[u];
dfs1(to[i]);
}
}
int main() {
int m, n;
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++) {
int x, y;
scanf("%d%d", &x, &y);
add(x, y);
add(y, x);
}
for (int i = 1; i <= n; i++)
if (!c[i]) {
color++;
dfs(i, 0);
root[i] = 1;
}
for (int i = 1; i <= n; i++)
if (root[i]) dfs1(i);
int q;
scanf("%d", &q);
while (q--) {
int x, y;
scanf("%d%d", &x, &y);
if (c[x] != c[y]) {
printf("No\n");
continue;
}
int anc = lca(x, y);
if ((dep[x] + dep[y] - 2 * dep[anc]) % 2 || s[x] + s[y] - 2 * s[anc] > 0) {
printf("Yes\n");
} else
printf("No\n");
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 100000;
vector<int> s[N + 5];
int fa[N + 5], pa[N + 5][20], dis[N + 5], odd[N + 5], sum[N + 5];
int Find(int k) {
if (fa[k] == k) return k;
return fa[k] = Find(fa[k]);
}
void dfs1(int u, int add) {
dis[u] = add;
for (int i = 0; i < s[u].size(); i++) {
int v = s[u][i];
if (dis[v] == -1) {
pa[v][0] = u;
dfs1(v, add + 1);
if (Find(u) == Find(v)) {
odd[u] |= odd[v];
}
} else if (dis[v] + 1 < dis[u]) {
if ((dis[u] - dis[v]) % 2 == 0) {
odd[u] = 1;
}
int x = Find(u);
while (dis[v] + 1 < dis[x]) {
fa[x] = pa[x][0];
x = Find(x);
}
}
}
}
int cnt[N + 5];
void dfs2(int u) {
cnt[u] += odd[u];
for (int i = 0; i < s[u].size(); i++) {
int v = s[u][i];
if (dis[v] == dis[u] + 1) {
if (Find(u) == Find(v)) odd[v] |= odd[u];
cnt[v] = cnt[u];
dfs2(v);
}
}
}
int lca(int a, int b) {
if (dis[a] > dis[b]) swap(a, b);
int d = dis[b] - dis[a];
for (int i = 18; i >= 0; i--) {
if (d & (1 << i)) {
b = pa[b][i];
}
}
for (int i = 18; i >= 0; i--) {
if (pa[a][i] != pa[b][i]) {
a = pa[a][i];
b = pa[b][i];
}
}
if (a != b) a = pa[a][0];
return a;
}
int check(int x, int y) {
int pt = lca(x, y);
if (pt == 0) return 0;
if ((dis[x] + dis[y]) % 2) return 1;
if (cnt[x] + cnt[y] - 2 * cnt[pt] > 0) return 1;
return 0;
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 0; i < m; i++) {
int x, y;
scanf("%d%d", &x, &y);
s[x].push_back(y);
s[y].push_back(x);
}
for (int i = 1; i <= n; i++) {
fa[i] = i;
}
memset(dis, -1, sizeof(dis));
for (int i = 0; i < n; i++) {
if (dis[i] == -1) {
dfs1(i, 0);
dfs2(i);
}
}
for (int i = 1; i <= 17; i++) {
for (int j = 1; j <= n; j++) {
pa[j][i] = pa[pa[j][i - 1]][i - 1];
}
}
int t;
scanf("%d", &t);
while (t--) {
int x, y;
scanf("%d%d", &x, &y);
if (check(x, y))
puts("Yes");
else
puts("No");
}
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
vector<int> from[200010], g[200010];
int id[200010], low[200010], TOT, ALL;
vector<int> S;
int col[200010], vis[200010], TOTOT = 233;
int val[200010];
int fa[200010][18], dep[200010];
bool check(int x, int c = 1) {
col[x] = c;
for (int i = 0; i < from[x].size(); i++) {
int v = from[x][i];
if (vis[v] != TOTOT) continue;
if (col[v] == -1) {
if (check(v, c ^ 1)) return 1;
} else if (col[v] != (c ^ 1))
return 1;
}
return 0;
}
void solve(vector<int> node) {
int now = ALL++;
TOTOT++;
for (int i = 0; i < node.size(); i++) {
g[now].push_back(node[i]);
g[node[i]].push_back(now);
col[node[i]] = -1, vis[node[i]] = TOTOT;
}
if (check(node[0])) val[now] = 1;
}
void dfs(int x) {
id[x] = low[x] = ++TOT;
S.push_back(x);
for (int i = 0; i < from[x].size(); i++) {
int v = from[x][i];
if (!id[v]) {
dfs(v), low[x] = min(low[x], low[v]);
if (low[v] >= id[x]) {
vector<int> node;
while (1) {
int tmp = S.back();
S.pop_back();
node.push_back(tmp);
if (tmp == v) break;
}
node.push_back(x);
solve(node);
}
} else
low[x] = min(low[x], id[v]);
}
}
void dfsg(int x, int last) {
fa[x][0] = last, vis[x] = 1;
for (int i = 0; i < g[x].size(); i++) {
int v = g[x][i];
if (v == last) continue;
dep[v] = dep[x] + 1, val[v] += val[x], dfsg(v, x);
}
}
int get_lca(int x, int y, int fa[][18], int dep[]) {
if (dep[x] < dep[y]) swap(x, y);
int tmp = dep[x] - dep[y], cnt = 0;
while (tmp) {
if (tmp & 1) x = fa[x][cnt];
tmp >>= 1, cnt++;
}
if (x == y) return x;
for (int i = 17; i >= 0; i--) {
if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i];
}
if (fa[x][0] != fa[y][0]) return -1;
return fa[x][0];
}
int f[200010][18], ddd[200010];
void dfsccc(int x, int last) {
f[x][0] = last, vis[x] = 1;
for (int i = 0; i < from[x].size(); i++) {
int v = from[x][i];
if (vis[v]) continue;
ddd[v] = ddd[x] + 1, dfsccc(v, x);
}
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
ALL = n + 1;
for (int i = 0; i < m; i++) {
int x, y;
scanf("%d%d", &x, &y);
from[x].push_back(y);
from[y].push_back(x);
}
for (int i = 1; i <= n; i++) {
if (!id[i]) S.clear(), dfs(i);
}
memset(vis, 0, sizeof(vis));
for (int i = 1; i <= n; i++) {
if (!vis[i]) dfsg(i, i);
}
memset(vis, 0, sizeof(vis));
for (int i = 1; i <= n; i++) {
if (!vis[i]) dfsccc(i, i);
}
for (int j = 1; j < 18; j++) {
for (int i = 1; i <= n; i++) {
fa[i][j] = fa[fa[i][j - 1]][j - 1];
f[i][j] = f[f[i][j - 1]][j - 1];
}
}
int q;
scanf("%d", &q);
while (q--) {
int x, y;
scanf("%d%d", &x, &y);
int lca = get_lca(x, y, fa, dep);
if (lca == -1)
printf("No\n");
else if (val[x] + val[y] - val[lca] -
(fa[lca][0] == lca ? 0 : val[fa[lca][0]]) >
0) {
printf("Yes\n");
} else if (ddd[x] + ddd[y] - 2 * ddd[get_lca(x, y, f, ddd)] & 1)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
namespace IO {
char buf[1000000], *p1, *p2;
inline char getc() {
if (p1 == p2) p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin);
return p1 == p2 ? EOF : *(p1++);
}
template <typename tp>
inline void r(tp &n) {
n = 0;
char c = getc();
while (!isdigit(c)) c = getc();
while (isdigit(c)) n = n * 10 + c - 48, c = getc();
}
template <typename tp>
inline void w(tp n) {
if (n / 10) w(n / 10);
putchar(n % 10 + 48);
}
}; // namespace IO
using namespace IO;
const int N = 1e5 + 5, M = 1e5 + 5;
using namespace std;
int n, m, q, num, tot, top;
int dep[N], dis[N], dfn[N], low[N], col[N], stk[N], odd[N], cur[N], f[N][21];
int head[N], nt[M << 1], to[M << 1];
void Add(int x, int y) {
++num;
nt[num] = head[x];
head[x] = num;
to[num] = y;
++num;
nt[num] = head[y];
head[y] = num;
to[num] = x;
}
int ins[N];
void Tar(int p, int fa) {
col[p] = col[fa];
dep[p] = dep[fa] + 1;
dfn[p] = low[p] = ++tot;
stk[++top] = p;
ins[p] = 1;
for (int i = head[p]; i; i = nt[i])
if (to[i] ^ fa) {
int v = to[i];
if (!dfn[v]) {
f[v][0] = p;
for (int i = 1; i <= 20; ++i) f[v][i] = f[f[v][i - 1]][i - 1];
Tar(v, p);
low[p] = min(low[p], low[v]);
} else if (ins[v])
low[p] = min(low[p], dfn[v]), cur[p] |= (!(dep[p] + dep[v] & 1));
}
if (dfn[p] == low[p]) {
int flg = 0, t = top;
while (stk[t] != p && !flg) flg = cur[stk[t]], --t;
while (stk[top] != p) {
odd[stk[top]] |= flg;
ins[stk[top]] = 0;
--top;
}
--top, ins[p] = 0;
}
}
void DFS(int p) {
dis[p] += odd[p];
for (int i = head[p]; i; i = nt[i])
if (dep[to[i]] == dep[p] + 1) dis[to[i]] = dis[p], DFS(to[i]);
}
int LCA(int x, int y) {
if (dep[x] < dep[y]) x ^= y ^= x ^= y;
for (int i = 20; i >= 0; --i)
if (dep[f[x][i]] >= dep[y]) x = f[x][i];
if (x == y) return x;
for (int i = 20; i >= 0; --i)
if (f[x][i] != f[y][i]) x = f[x][i], y = f[y][i];
return f[x][0];
}
int DIS(int x, int y) { return dis[x] + dis[y] - 2 * dis[LCA(x, y)]; }
bool check(int x, int y) {
if (col[x] != col[y] || x == y) return false;
if (dep[x] + dep[y] & 1) return true;
return DIS(x, y) > 0;
}
int main() {
r(n), r(m);
for (int i = 1, x, y; i <= m; ++i) r(x), r(y), Add(x, y);
for (int i = 1; i <= n; ++i)
if (!dfn[i]) col[i] = i, Tar(i, i), DFS(i);
r(q);
for (int i = 1, x, y; i <= q; ++i)
r(x), r(y), puts(check(x, y) ? "Yes" : "No");
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int p[200010], n1[200010], h[100010], ee = 0, anc[17][100010], F[100010],
d[100010], odd[100010], sum[100010], n, m;
void ae(int x, int y) {
p[ee] = y;
n1[ee] = h[x];
h[x] = ee++;
}
int gf(int x) { return (F[x] == x) ? x : F[x] = gf(F[x]); }
int lca(int x, int y) {
if (d[x] < d[y]) swap(x, y);
for (int k = d[x] - d[y], j = 0; k; k >>= 1, j++)
if (k & 1) x = anc[j][x];
if (x == y) return x;
for (int i = 16; ~i; i--)
if (anc[i][x] != anc[i][y]) x = anc[i][x], y = anc[i][y];
return anc[0][x];
}
void dfs(int u) {
for (int i = h[u]; ~i; i = n1[i]) {
int v = p[i];
if (!~d[v]) {
d[v] = d[u] + 1;
anc[0][v] = u;
dfs(v);
if (gf(v) == gf(u)) odd[u] |= odd[v];
} else if (d[u] > d[v] + 1) {
if ((d[u] - d[v]) % 2 == 0) odd[u] = 1;
for (int w = gf(u); d[w] > d[v] + 1; w = gf(w)) F[w] = anc[0][w];
}
}
}
void work(int u) {
sum[u] += odd[u];
for (int i = h[u]; ~i; i = n1[i]) {
int v = p[i];
if (d[v] == d[u] + 1) {
if (gf(u) == gf(v)) odd[v] |= odd[u];
sum[v] = sum[u];
work(v);
}
}
}
int check(int x, int y) {
int z = lca(x, y);
if (!z) return 0;
return ((d[x] + d[y]) & 1) || (sum[x] + sum[y] - 2 * sum[z] > 0);
}
int main() {
scanf("%d%d", &n, &m);
memset(h, -1, sizeof(h));
memset(d, -1, sizeof(d));
int Q, x, y;
for (int i = 1; i <= m; i++) scanf("%d%d", &x, &y), ae(x, y), ae(y, x);
for (int i = 1; i <= n; i++) F[i] = i;
for (int i = 1; i <= n; i++)
if (!~d[i]) d[i] = 0, dfs(i), work(i);
for (int j = 1; j <= 16; j++)
for (int i = 1; i <= n; i++) anc[j][i] = anc[j - 1][anc[j - 1][i]];
scanf("%d", &Q);
while (Q--) {
scanf("%d%d", &x, &y);
printf("%s\n", check(x, y) ? "Yes" : "No");
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
inline long long rd() {
long long x = 0, w = 1;
char ch = 0;
while (ch < '0' || ch > '9') {
if (ch == '-') w = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 3) + (x << 1) + (ch ^ 48);
ch = getchar();
}
return x * w;
}
struct graph {
int to[N << 1], nt[N << 1], hd[N], tot;
graph() { tot = 1; }
inline void add(int x, int y) {
++tot, to[tot] = y, nt[tot] = hd[x], hd[x] = tot;
++tot, to[tot] = x, nt[tot] = hd[y], hd[y] = tot;
}
} ee, tr;
int n, m, q;
bool p[N << 1];
int be[N], tot, dfn[N], low[N], ti, st[N << 1], tp, co[N];
int e[N], te, dd[N], td;
inline void tj(int x, int ffa) {
dfn[x] = low[x] = ++ti, be[x] = tot;
for (int i = ee.hd[x]; i; i = ee.nt[i]) {
int y = ee.to[i];
if (i == ffa || dfn[y] > dfn[x]) continue;
st[++tp] = i;
if (!dfn[y]) {
dd[++td] = i;
tr.add(ee.to[i], ee.to[i ^ 1]);
co[y] = co[x] ^ 1, tj(y, i ^ 1), low[x] = min(low[x], low[y]);
if (low[y] >= dfn[x]) {
int z = 0;
te = 0;
while (z != i) e[++te] = z = st[tp--];
bool ok = 0;
for (int j = 1; j <= te && !ok; ++j)
ok = co[ee.to[e[j]]] ^ co[ee.to[e[j] ^ 1]] ^ 1;
for (int j = 1; j <= te; ++j) p[e[j]] = p[e[j] ^ 1] = ok;
}
} else
low[x] = min(low[x], dfn[y]);
}
}
int fa[N], sz[N], hson[N], top[N], de[N], a[N];
void dfs1(int x) {
sz[x] = 1;
for (int i = tr.hd[x]; i; i = tr.nt[i]) {
int y = tr.to[i];
if (y == fa[x]) continue;
fa[y] = x, de[y] = de[x] + 1, a[y] = a[x] + p[dd[(i >> 1)]], dfs1(y),
sz[x] += sz[y];
hson[x] = sz[hson[x]] > sz[y] ? hson[x] : y;
}
}
void dfs2(int x, int ntp) {
top[x] = ntp;
if (hson[x]) dfs2(hson[x], ntp);
for (int i = tr.hd[x]; i; i = tr.nt[i]) {
int y = tr.to[i];
if (y == fa[x] || y == hson[x]) continue;
dfs2(y, y);
}
}
inline int glca(int x, int y) {
while (top[x] != top[y]) {
if (de[top[x]] < de[top[y]]) swap(x, y);
x = fa[top[x]];
}
return de[x] < de[y] ? x : y;
}
int main() {
n = rd(), m = rd();
for (int i = 1; i <= m; ++i) ee.add(rd(), rd());
for (int i = 1; i <= n; ++i)
if (!dfn[i]) tp = 0, ++tot, tj(i, 0), de[i] = 1, dfs1(i), dfs2(i, i);
q = rd();
while (q--) {
int x = rd(), y = rd();
if (be[x] ^ be[y])
puts("No");
else {
int lca = glca(x, y);
(((de[x] + de[y] - (de[lca] << 1)) & 1) |
(a[x] + a[y] - (a[lca] << 1) > 0))
? puts("Yes")
: puts("No");
}
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int maxN = 100 * 1000 + 5;
const long long mod = 1000 * 1000 * 1000 + 7;
const int maxL = 20;
vector<int> a[maxN];
int mark[maxN], dad[maxN], par[maxN];
int c[maxN], cid, bad[maxN], B[maxN];
int h[maxN], bl[maxN];
int get_par(int v) { return (par[v] == v ? v : par[v] = get_par(par[v])); }
void dfs(int v, int p, int col) {
mark[v] = 1;
bl[v] = cid;
c[v] = col;
col ^= 1;
h[v] = h[p] + 1;
dad[v] = p;
for (auto u : a[v]) {
if (mark[u] == 0)
dfs(u, v, col);
else if (h[u] < h[v]) {
int x = v;
while (h[x] > h[u] + 1) {
par[get_par(x)] = get_par(dad[x]);
x = get_par(x);
}
}
}
}
int D[maxN];
int lca[maxN][maxL];
void make_lca(int v, int p) {
lca[v][0] = p;
for (int i = 1; i < maxL; i++) lca[v][i] = lca[lca[v][i - 1]][i - 1];
}
void DFS(int v, int p = 0) {
mark[v] = 1;
make_lca(v, p);
D[v] += bad[v];
for (auto u : a[v])
if (mark[u] == 0) D[u] = D[v], DFS(u, v);
}
int LCA(int v, int u) {
if (h[v] < h[u]) swap(v, u);
int dif = h[v] - h[u];
for (int i = 0; i < maxL; i++)
if (dif >> i & 1) v = lca[v][i];
if (v == u) return v;
for (int i = maxL - 1; i >= 0; i--)
if (lca[v][i] != lca[u][i]) v = lca[v][i], u = lca[u][i];
return lca[v][0];
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
for (int i = 1; i < n + 1; i++) par[i] = i;
for (int i = 0; i < m; i++) {
int v, u;
cin >> v >> u;
a[v].push_back(u);
a[u].push_back(v);
}
for (int i = 1; i < n + 1; i++)
if (!mark[i]) dfs(i, 0, 0), cid++;
for (int i = 1; i < n + 1; i++)
for (auto u : a[i])
if (c[i] == c[u]) B[get_par(h[i] < h[u] ? u : i)] = 1;
for (int i = 1; i < n + 1; i++)
if (B[get_par(i)]) bad[i] = 1;
memset(mark, 0, sizeof mark);
for (int i = 1; i < n + 1; i++)
if (mark[i] == 0) DFS(i);
int q;
cin >> q;
for (int i = 0; i < q; i++) {
int v, u;
cin >> v >> u;
if (v == u) {
cout << "No" << endl;
continue;
}
if (bl[v] != bl[u]) {
cout << "No" << endl;
continue;
}
if (c[v] != c[u]) {
cout << "Yes" << endl;
continue;
}
int pa = LCA(v, u);
int ans = D[v] + D[u] - 2 * D[pa];
if (ans)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -f;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - 48;
ch = getchar();
}
return x * f;
}
const int N = 100010;
const int M = 18;
int ea[N * 2], eb[N * 2], ec[N], etot;
int rt[N], dep[N], fa[N][M], dfn[N], low[N], sum[N], stk[N], bel[N];
bool vis[N], flag[N];
int cnt, tp, idx;
void addEdge(int x, int y) {
etot++;
ea[etot] = y;
eb[etot] = ec[x];
ec[x] = etot;
}
void dfs1(int x) {
for (int i = 1; i <= 16; ++i) fa[x][i] = fa[fa[x][i - 1]][i - 1];
for (int i = ec[x]; i; i = eb[i]) {
int y = ea[i];
if (!dep[y]) {
fa[y][0] = x;
dep[y] = dep[x] + 1;
rt[y] = rt[x];
dfs1(y);
}
}
}
int lca(int x, int y) {
if (dep[x] < dep[y]) swap(x, y);
for (int i = 16; i >= 0; --i) {
if (dep[fa[x][i]] >= dep[y]) x = fa[x][i];
}
for (int i = 16; i >= 0; --i) {
if (fa[x][i] != fa[y][i]) {
x = fa[x][i];
y = fa[y][i];
}
}
return x != y ? fa[x][0] : x;
}
void dfs2(int x) {
for (int i = ec[x]; i; i = eb[i]) {
int y = ea[i];
if (fa[y][0] == x) {
sum[y] += sum[x];
dfs2(y);
}
}
}
void tarjan(int x, int fa) {
dfn[x] = low[x] = ++idx;
stk[++tp] = x;
vis[x] = 1;
for (int i = ec[x]; i; i = eb[i]) {
int y = ea[i];
if (y != fa) {
if (!dfn[y]) {
tarjan(y, x);
low[x] = min(low[x], low[y]);
} else
low[x] = min(low[x], dfn[y]);
}
}
if (low[x] == dfn[x]) {
cnt++;
while (stk[tp + 1] != x) {
vis[stk[tp]] = 0;
bel[stk[tp--]] = cnt;
}
}
}
signed main() {
int n = read(), m = read();
for (int i = 1; i <= m; ++i) {
int x = read(), y = read();
addEdge(x, y);
addEdge(y, x);
}
for (int i = 1; i <= n; ++i) {
if (!dep[i]) {
dep[i] = 1;
dfs1(rt[i] = i);
}
}
for (int i = 1; i <= n; ++i) {
if (dep[i] == 1) tarjan(i, 0);
}
for (int x = 1; x <= n; ++x) {
if (!flag[bel[x]]) {
for (int i = ec[x]; i; i = eb[i]) {
int y = ea[i];
if (!((dep[x] + dep[y]) & 1) && bel[x] == bel[y]) {
flag[bel[x]] = 1;
break;
}
}
}
}
for (int i = 1; i <= n; ++i) {
if (fa[i][0] && bel[i] == bel[fa[i][0]] && flag[bel[i]]) sum[i]++;
}
for (int i = 1; i <= n; ++i) {
if (dep[i] == 1) dfs2(i);
}
int q = read();
while (q-- > 0) {
int x = read(), y = read();
if (rt[x] != rt[y] || x == y ||
(!((dep[x] + dep[y]) & 1) && sum[x] + sum[y] == 2 * sum[lca(x, y)]))
puts("No");
else
puts("Yes");
}
fclose(stdin);
fclose(stdout);
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int n, m, q;
vector<int> g[100005];
int dep[100005], fa[100005][17], odd[100005], ufa[100005], ccnt, ic[100005];
int find(int x) {
if (ufa[x] == x) return x;
return ufa[x] = find(ufa[x]);
}
void dfs(int x, int p, int cd) {
fa[x][0] = p;
dep[x] = cd;
ic[x] = ccnt;
for (int &y : g[x])
if (!ic[y]) {
dfs(y, x, cd + 1);
odd[x] |= odd[y] && find(x) == find(y);
} else if (dep[x] > dep[y] + 1) {
odd[x] |= (dep[x] ^ dep[y]) & 1 ^ 1;
for (int i = find(x); dep[i] > dep[y] + 1; i = find(i)) ufa[i] = fa[i][0];
}
}
void init() {
for (int i = 1; i <= n; i++)
if (!ic[i]) {
ccnt++;
dfs(i, -1, 0);
}
for (int j = 0; j < 16; j++) {
for (int i = 1; i <= n; i++) {
if (fa[i][j] == -1)
fa[i][j + 1] = -1;
else
fa[i][j + 1] = fa[fa[i][j]][j];
}
}
}
int lca(int x, int y) {
if (dep[x] < dep[y]) swap(x, y);
for (int i = 16; i >= 0; i--) {
if (fa[x][i] != -1 && dep[fa[x][i]] >= dep[y]) x = fa[x][i];
}
if (x == y) return x;
for (int i = 16; i >= 0; i--) {
if (fa[x][i] != fa[y][i]) {
x = fa[x][i];
y = fa[y][i];
}
}
return fa[x][0];
}
void push1(int x) {
for (int &y : g[x])
if (fa[y][0] == x) {
odd[y] |= odd[x] && find(x) == find(y);
push1(y);
}
}
void push2(int x) {
for (int &y : g[x])
if (fa[y][0] == x) {
odd[y] += odd[x];
push2(y);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
iota(ufa, ufa + 100001, 0);
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int a, b;
cin >> a >> b;
g[a].emplace_back(b);
g[b].emplace_back(a);
}
init();
for (int i = 1; i <= n; i++)
if (fa[i][0] == -1) {
push1(i);
push2(i);
}
cin >> q;
while (q--) {
int u, v;
cin >> u >> v;
if (ic[u] != ic[v]) {
cout << "No\n";
continue;
}
if ((dep[u] ^ dep[v]) & 1) {
cout << "Yes\n";
continue;
}
if (odd[u] + odd[v] - 2 * odd[lca(u, v)]) {
cout << "Yes\n";
continue;
}
cout << "No\n";
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 1e5;
int n, m, q;
vector<int> e[N + 1];
int rt[N + 1], fa[N + 1], dep[N + 1], siz[N + 1];
int read() {
int ret = 0;
char c = getchar();
while (!isdigit(c)) {
c = getchar();
}
while (isdigit(c)) {
ret = ret * 10 + c - '0';
c = getchar();
}
return ret;
}
namespace segmentTree {
struct tree {
int sum;
bool tag;
};
tree t[N * 4];
inline void set(int num, int l, int r) {
t[num] = tree{r - l + 1, true};
return;
}
inline void pushUp(int num) {
t[num].sum = t[num << 1].sum + t[num << 1 | 1].sum;
return;
}
void pushDown(int num, int l, int r) {
if (t[num].tag) {
int mid = (l + r) / 2;
set(num << 1, l, mid), set(num << 1 | 1, mid + 1, r);
t[num].tag = false;
}
return;
}
void update(int num, int l, int r, int fro, int to) {
if ((fro <= l) && (to >= r)) {
set(num, l, r);
return;
}
int mid = (l + r) / 2;
pushDown(num, l, r);
if (fro <= mid) {
update(num << 1, l, mid, fro, to);
}
if (to > mid) {
update(num << 1 | 1, mid + 1, r, fro, to);
}
pushUp(num);
return;
}
int query(int num, int l, int r, int fro, int to) {
if ((fro <= l) && (to >= r)) {
return t[num].sum;
}
int mid = (l + r) / 2, ret = 0;
pushDown(num, l, r);
if (fro <= mid) {
ret += query(num << 1, l, mid, fro, to);
}
if (to > mid) {
ret += query(num << 1 | 1, mid + 1, r, fro, to);
}
return ret;
}
} // namespace segmentTree
namespace HLD {
int son[N + 1], top[N + 1], dfn[N + 1];
void dfs1(int u, int f, int r) {
rt[u] = r, fa[u] = f, dep[u] = dep[f] + 1, siz[u] = 1;
for (int v : e[u]) {
if (rt[v]) {
continue;
}
dfs1(v, u, r);
siz[u] += siz[v];
son[u] = siz[v] > siz[son[u]] ? v : son[u];
}
return;
}
void dfs2(int u, int t) {
static int tot;
top[u] = t, dfn[u] = ++tot;
if (!son[u]) {
return;
}
dfs2(son[u], t);
for (int v : e[u]) {
if ((fa[v] == u) && (v != son[u])) {
dfs2(v, v);
}
}
return;
}
void updateLink(int u, int v) {
using segmentTree::update;
while (top[u] != top[v]) {
if (dep[top[u]] > dep[top[v]]) {
update(1, 1, n, dfn[top[u]], dfn[u]);
u = fa[top[u]];
} else {
update(1, 1, n, dfn[top[v]], dfn[v]);
v = fa[top[v]];
}
}
if (u != v) {
if (dep[u] > dep[v]) {
swap(u, v);
}
update(1, 1, n, dfn[u] + 1, dfn[v]);
}
return;
}
int queryLink(int u, int v) {
using segmentTree::query;
int ret = 0;
while (top[u] != top[v]) {
if (dep[top[u]] > dep[top[v]]) {
ret += query(1, 1, n, dfn[top[u]], dfn[u]);
u = fa[top[u]];
} else {
ret += query(1, 1, n, dfn[top[v]], dfn[v]);
v = fa[top[v]];
}
}
if (u != v) {
if (dep[u] > dep[v]) {
swap(u, v);
}
ret += query(1, 1, n, dfn[u] + 1, dfn[v]);
}
return ret;
}
} // namespace HLD
void dfsUpdate(int u) {
for (int v : e[u]) {
if (v == fa[u]) {
continue;
}
if (fa[v] == u) {
dfsUpdate(v);
} else {
if ((dep[v] < dep[u]) && !(dep[u] - dep[v] & 1)) {
HLD::updateLink(u, v);
}
}
}
return;
}
void dfsFindOddRing(int u) {
for (int v : e[u]) {
if (v == fa[u]) {
continue;
}
if (fa[v] == u) {
dfsFindOddRing(v);
} else {
if ((dep[v] < dep[u]) && HLD::queryLink(u, v)) {
HLD::updateLink(u, v);
}
}
}
return;
}
int main() {
n = read(), m = read();
for (int i = 1, u, v; i <= m; ++i) {
u = read(), v = read();
e[u].push_back(v), e[v].push_back(u);
}
for (int i = 1; i <= n; ++i) {
if (rt[i]) {
continue;
}
HLD::dfs1(i, 0, i), HLD::dfs2(i, i);
dfsUpdate(i), dfsFindOddRing(i);
}
q = read();
while (q--) {
int u = read(), v = read();
if ((rt[u] != rt[v]) || (u == v)) {
puts("No");
} else {
puts(((dep[u] ^ dep[v]) & 1) || HLD::queryLink(u, v) ? "Yes" : "No");
}
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int f[1000005][20];
int dfn[1000005], low[1000005], zhan[1000005 * 2], head[1000005], bcc[1000005];
int pdltk[1000005], num, dep[1000005];
int root[1000005];
int n, m;
bitset<1000005> vis1, vis2;
int cnt, tot;
int sum[1000005];
vector<int> e[1000005];
struct dian {
int x, y;
} s[2 * 1000005];
void dfs1(int u, int fa = 0) {
pdltk[u] = num;
dep[u] = dep[fa] + 1;
f[u][0] = fa;
vis1[u] = 1;
for (register int i = 1; i <= 19; i++) f[u][i] = f[f[u][i - 1]][i - 1];
for (register int i = 0; i < e[u].size(); i++) {
int v = e[u][i];
if (!vis1[v]) dfs1(v, u);
}
}
int top = 0;
void tarjan(int u, int fa = 0) {
static int idx = 0;
dfn[u] = low[u] = ++idx;
for (register int i = 0; i < e[u].size(); i++) {
int v = e[u][i];
if (v != fa && dfn[v] < dfn[u]) {
s[++top] = (dian){u, v};
if (!dfn[v]) {
tarjan(v, u);
low[u] = min(low[u], low[v]);
if (low[v] >= dfn[u]) {
int x, y, flag = 0, tmp = top;
do {
x = s[top].x;
y = s[top--].y;
if ((dep[x] & 1) == (dep[y] & 1)) {
flag = 1;
break;
}
} while (!(x == u && y == v));
if (!flag) continue;
top = tmp;
do {
x = s[top].x;
y = s[top--].y;
sum[x] = sum[y] = 1;
} while (!(x == u && y == v));
sum[u] = 0;
}
} else
low[u] = min(low[u], dfn[v]);
}
}
}
void dfs2(int u, int fa) {
sum[u] += sum[fa];
vis2[u] = 1;
for (register int i = 0; i < e[u].size(); i++)
if (!vis2[e[u][i]]) dfs2(e[u][i], u);
}
int lca(int x, int y) {
if (dep[x] < dep[y]) swap(x, y);
for (register int i = 19; i >= 0; i--) {
if (dep[f[x][i]] >= dep[y]) x = f[x][i];
if (x == y) return x;
}
for (register int i = 19; i >= 0; i--) {
if (f[x][i] != f[y][i]) {
x = f[x][i];
y = f[y][i];
}
}
return f[x][0];
}
bool pd(int u, int v) {
if (u == v) return 0;
if (pdltk[u] != pdltk[v]) return 0;
if ((dep[u] & 1) ^ (dep[v] & 1)) return 1;
return (sum[u] + sum[v] - 2 * sum[lca(u, v)]) > 0;
}
int main() {
scanf("%d%d", &n, &m);
for (register int i = 1; i <= m; i++) {
int a1, a2;
scanf("%d%d", &a1, &a2);
e[a1].push_back(a2);
e[a2].push_back(a1);
}
for (register int i = 1; i <= n; i++)
if (!dfn[i]) {
num++;
dfs1(i);
tarjan(i);
dfs2(i, 0);
}
int t;
scanf("%d", &t);
for (register int i = 1; i <= t; i++) {
int u, v;
scanf("%d%d", &u, &v);
if (pd(u, v))
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
long long n, m, q, fa[100010], fa2[100010], dep[100010], f[100010][25],
par[100010], sum[100010];
bool vis[100010], odd[100010];
vector<long long> vt[100010];
long long getf(long long x) {
if (x == fa[x]) {
return x;
}
return fa[x] = getf(fa[x]);
}
long long getf2(long long x) {
if (x == fa2[x]) {
return x;
}
return fa2[x] = getf2(fa2[x]);
}
void dfs(long long x, long long lst, long long d) {
vis[x] = true;
par[x] = lst;
dep[x] = d;
long long i, j;
for (i = 0; i < 22; i++) {
f[x][i + 1] = f[f[x][i]][i];
}
for (i = 0; i < vt[x].size(); i++) {
if (vt[x][i] != lst) {
if (!vis[vt[x][i]]) {
f[vt[x][i]][0] = x;
dfs(vt[x][i], x, d + 1);
if (odd[vt[x][i]] && getf2(x) == getf2(vt[x][i])) {
odd[x] = true;
}
} else {
if (dep[vt[x][i]] < dep[x]) {
if ((dep[x] - dep[vt[x][i]]) % 2 == 0) {
odd[x] = true;
}
j = getf2(x);
while (dep[j] > dep[vt[x][i]] + 1) {
fa2[j] = par[j];
j = getf2(j);
}
}
}
}
}
return;
}
void dfs2(long long x, long long lst) {
vis[x] = true;
long long i;
for (i = 0; i < vt[x].size(); i++) {
if (!vis[vt[x][i]]) {
if (odd[x] && getf2(x) == getf2(vt[x][i])) {
odd[vt[x][i]] = true;
}
dfs2(vt[x][i], x);
}
}
return;
}
void dfs3(long long x, long long lst) {
vis[x] = true;
sum[x] = odd[x];
if (lst != -1) {
sum[x] += sum[lst];
}
long long i;
for (i = 0; i < vt[x].size(); i++) {
if (!vis[vt[x][i]]) {
dfs3(vt[x][i], x);
}
}
return;
}
long long glca(long long x, long long y) {
long long i;
if (dep[x] < dep[y]) {
swap(x, y);
}
for (i = 22; i >= 0; i--) {
if (dep[f[x][i]] >= dep[y]) {
x = f[x][i];
}
if (x == y) {
return x;
}
}
for (i = 22; i >= 0; i--) {
if (f[x][i] != f[y][i]) {
x = f[x][i];
y = f[y][i];
}
}
return f[x][0];
}
int main() {
long long i, j, x, y, a, b;
scanf("%lld%lld", &n, &m);
for (i = 0; i < n; i++) {
fa[i] = i;
}
for (i = 0; i < m; i++) {
scanf("%lld%lld", &x, &y);
x--;
y--;
vt[x].push_back(y);
vt[y].push_back(x);
a = getf(x);
b = getf(y);
fa[b] = a;
}
for (i = 0; i < n; i++) {
fa[i] = getf(i);
fa2[i] = i;
odd[i] = false;
}
memset(vis, false, sizeof(vis));
for (i = 0; i < n; i++) {
if (!vis[i]) {
dfs(i, -1, 0);
}
}
memset(vis, false, sizeof(vis));
for (i = 0; i < n; i++) {
if (!vis[i]) {
dfs2(i, -1);
}
}
memset(vis, false, sizeof(vis));
memset(sum, 0, sizeof(sum));
for (i = 0; i < n; i++) {
if (!vis[i]) {
dfs3(i, -1);
}
}
scanf("%lld", &q);
while (q--) {
scanf("%lld%lld", &x, &y);
x--;
y--;
if (fa[x] != fa[y] || x == y) {
puts("No");
continue;
}
a = glca(x, y);
if ((dep[x] + dep[y] - dep[a] * 2) % 2 == 1) {
puts("Yes");
} else if ((sum[x] - sum[a]) != 0) {
puts("Yes");
} else if ((sum[y] - sum[a]) != 0) {
puts("Yes");
} else {
puts("No");
}
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 100111;
const int MAXM = 100111;
int N, M;
int F[MAXN];
int Find(int a) {
if (F[a] != F[F[a]]) F[a] = Find(F[a]);
return F[a];
}
struct Vert {
int FE;
int Dep;
int Cnt;
int Dps;
int Dfn, Low;
bool Vis;
} V[MAXN];
struct Edge {
int x, y, next, neg;
bool u, odd;
int Bel;
} E[MAXM << 1];
int Ecnt;
void addE(int a, int b) {
++Ecnt;
E[Ecnt].x = a;
E[Ecnt].y = b;
E[Ecnt].next = V[a].FE;
V[a].FE = Ecnt;
E[Ecnt].neg = Ecnt + 1;
++Ecnt;
E[Ecnt].y = a;
E[Ecnt].x = b;
E[Ecnt].next = V[b].FE;
V[b].FE = Ecnt;
E[Ecnt].neg = Ecnt - 1;
}
int DFN;
void DFS(int at) {
V[at].Dps = DFN;
V[at].Vis = true;
for (int k = V[at].FE, to; k > 0; k = E[k].next) {
to = E[k].y;
if (V[to].Vis) continue;
E[k].u = true;
E[E[k].neg].u = true;
V[to].Dep = V[at].Dep + 1;
DFS(to);
}
}
int St[MAXM << 1], Top;
int Bcnt;
bool Odd[MAXM << 1];
void PBC(int at, int e = 0) {
++DFN;
V[at].Low = V[at].Dfn = DFN;
for (int k = V[at].FE, to; k > 0; k = E[k].next) {
if (k == E[e].neg) continue;
to = E[k].y;
if (V[to].Dfn) {
if (V[to].Dfn < V[at].Dfn) St[++Top] = k;
V[at].Low = min(V[at].Low, V[to].Dfn);
} else {
St[++Top] = k;
PBC(to, k);
V[at].Low = min(V[at].Low, V[to].Low);
}
}
if (V[at].Low >= V[E[e].x].Dfn) {
++Bcnt;
while (Top > 1 && St[Top] != e) {
E[St[Top]].Bel = Bcnt;
--Top;
}
E[St[Top]].Bel = Bcnt;
--Top;
}
}
void Push(int at) {
V[at].Vis = true;
for (int k = V[at].FE, to; k > 0; k = E[k].next) {
to = E[k].y;
if (V[to].Vis) continue;
V[to].Cnt = V[at].Cnt + E[k].odd;
Push(to);
if (!E[k].odd) F[to] = at;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin >> N >> M;
for (int i = 1, a, b; i <= M; ++i) {
cin >> a >> b;
addE(a, b);
}
for (int i = 1; i <= N; ++i)
if (!V[i].Vis) {
++DFN;
DFS(i);
}
for (int i = 1, a, b; i <= Ecnt; ++i) {
if (E[i].u) continue;
a = E[i].x;
b = E[i].y;
if (V[a].Dep < V[b].Dep) swap(a, b);
if ((V[a].Dep - V[b].Dep) & 1) continue;
E[i].odd = true;
}
for (int i = 1; i <= N; ++i)
if (!V[i].Dfn) {
Top = 0;
PBC(i);
}
for (int i = 1; i <= Ecnt; ++i)
if (E[i].Bel && E[i].odd) Odd[E[i].Bel] = true;
for (int i = 1; i <= Ecnt; ++i)
if (Odd[E[i].Bel]) E[i].odd = true;
for (int i = 1; i <= Ecnt; ++i)
if (E[E[i].neg].odd) E[i].odd = true;
for (int i = 1; i <= N; ++i) V[i].Vis = false;
for (int i = 1; i <= N; ++i) F[i] = i;
for (int i = 1; i <= N; ++i)
if (!V[i].Vis) Push(i);
int Qcnt;
cin >> Qcnt;
for (int i = 1, a, b; i <= Qcnt; ++i) {
cin >> a >> b;
if (V[a].Dps != V[b].Dps)
puts("No");
else {
if (V[a].Dep < V[b].Dep) swap(a, b);
if ((V[a].Dep - V[b].Dep) & 1)
puts("Yes");
else if (Find(a) != Find(b))
puts("Yes");
else
puts("No");
}
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int n, m;
int head[N], nxt[N * 2], to[N * 2], tot;
int vis[N], stp[N];
int dp[N][20];
int dfn[N], low[N], dep = 0;
int stk[N * 5], top = 0;
int belong[N], block = 0;
int odd[N], col[N];
void init() {
memset(head, -1, sizeof(head));
tot = 0;
}
void add(int x, int y) {
to[tot] = y;
nxt[tot] = head[x];
head[x] = tot++;
}
void DFS_st(int u, int fa) {
vis[u] = 1;
stp[u] = stp[fa] + 1;
dp[u][0] = fa;
for (int i = 1; i < 20; i++) dp[u][i] = dp[dp[u][i - 1]][i - 1];
for (int i = head[u]; ~i; i = nxt[i]) {
int v = to[i];
if (!vis[v]) DFS_st(v, u);
}
}
void tarjan(int u, int fa) {
dfn[u] = low[u] = ++dep;
stk[top++] = u;
vis[u] = 1;
for (int i = head[u]; ~i; i = nxt[i]) {
int v = to[i];
if (v == fa) continue;
if (!dfn[v]) {
tarjan(v, u);
low[u] = min(low[u], low[v]);
} else if (vis[v])
low[u] = min(low[u], dfn[v]);
}
if (low[u] == dfn[u]) {
block++;
do {
top--;
belong[stk[top]] = block;
vis[stk[top]] = 0;
} while (stk[top] != u);
}
}
void DFS(int u, int fa) {
vis[u] = 1;
for (int i = head[u]; ~i; i = nxt[i]) {
int v = to[i];
if (!vis[v]) {
col[v] += col[u];
DFS(v, u);
}
}
}
int LCA(int x, int y) {
if (stp[x] < stp[y]) swap(x, y);
int delta = stp[x] - stp[y];
for (int i = 0; i < 20; i++)
if (delta & (1 << i)) x = dp[x][i];
if (x == y) return x;
for (int i = 19; i >= 0; i--)
if (dp[x][i] != dp[y][i]) x = dp[x][i], y = dp[y][i];
return dp[x][0];
}
int main() {
init();
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++) {
int x, y;
scanf("%d%d", &x, &y);
add(x, y);
add(y, x);
}
for (int u = 1; u <= n; u++)
if (!vis[u]) DFS_st(u, 0);
memset(vis, 0, sizeof(vis));
for (int u = 1; u <= n; u++)
if (!dfn[u]) tarjan(u, 0);
for (int u = 1; u <= n; u++) {
if (!odd[belong[u]]) {
for (int i = head[u]; ~i; i = nxt[i]) {
int v = to[i];
if ((stp[u] + stp[v]) % 2 == 0 && belong[v] == belong[u]) {
odd[belong[u]] = 1;
break;
}
}
}
}
for (int u = 1; u <= n; u++)
if (belong[u] == belong[dp[u][0]] && dp[u][0] != 0 && odd[belong[u]])
col[u]++;
memset(vis, 0, sizeof(vis));
for (int u = 1; u <= n; u++)
if (!vis[u]) DFS(u, 0);
int q;
scanf("%d", &q);
while (q--) {
int u, v;
scanf("%d%d", &u, &v);
int lca = LCA(u, v);
if (lca == 0) {
printf("No\n");
} else if ((stp[u] + stp[v] - stp[lca] * 2) % 2 == 1)
printf("Yes\n");
else {
if (col[u] + col[v] != col[lca] * 2)
printf("Yes\n");
else
printf("No\n");
}
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int nodes, edges, counter, counter2;
vector<int> adj[100001];
set<int> adjset[100001];
int seen[100001];
int low[100001];
int depth[100001];
bool iscut[100001];
set<int> group[100001];
int dfn[100001];
stack<pair<int, int> > s;
set<pair<int, int> > ins;
bool isOld[100001];
int father[100001];
void findComponents(int node) {
if (false) cout << "Happy\n";
isOld[node] = true;
counter2++;
dfn[node] = counter2;
low[node] = dfn[node];
while (adjset[node].size()) {
if (false) cout << "Happier\n";
int w = *adjset[node].begin();
adjset[node].erase(w);
adjset[w].erase(node);
if (ins.count(make_pair(node, w)) == 0) {
ins.insert(make_pair(node, w));
ins.insert(make_pair(w, node));
s.push(make_pair(node, w));
}
if (!isOld[w]) {
father[w] = node;
findComponents(w);
if (low[w] >= dfn[node]) {
iscut[node] = true;
if (false) cout << "Happiest\n";
while (true) {
pair<int, int> top = s.top();
s.pop();
ins.erase(top);
ins.erase(make_pair(top.second, top.first));
group[counter].insert(top.first);
group[counter].insert(top.second);
if (top == make_pair(w, node) || top == make_pair(node, w)) break;
}
counter++;
}
low[node] = min(low[node], low[w]);
} else if (w != father[node]) {
low[node] = min(low[node], dfn[w]);
}
}
}
int parity[100001];
int Rank[100001];
vector<int> evenGroupOf[100001];
set<int> evenClusterOf[100001];
int islandOf[100001];
int isEven[100001];
int parent[100001];
set<int> clusterAlpha[100001];
set<int> evenCluster[100001];
map<pair<int, int>, bool> answercache;
int find(int n) {
if (parent[n] != n) parent[n] = find(parent[n]);
return parent[n];
}
void join(int a, int b) {
a = find(a);
b = find(b);
if (Rank[a] > Rank[b]) {
parent[a] = b;
} else if (Rank[a] < Rank[b]) {
parent[b] = a;
} else {
parent[a] = b;
Rank[b]++;
}
}
void addToGroup(int node, int g) {
if (group[g].count(node) == 1) return;
seen[node] = true;
group[g].insert(node);
if (iscut[node]) return;
for (int i = 0; i < adj[node].size(); i++) {
addToGroup(adj[node][i], g);
}
}
bool findEven(int node, int g, int p = -1) {
seen[node] = true;
if (group[g].count(node) == 0) return 1;
if (parity[node] == p) return 1;
if (parity[node] == p * -1) return 0;
parity[node] = p;
for (int i = 0; i < adj[node].size(); i++) {
if (findEven(adj[node][i], g, p * -1) == 0) {
return 0;
}
}
return 1;
}
bool parityOf(int g) {
if (isEven[g] == 0) {
if (findEven(*group[g].begin(), g) == 1) {
isEven[g] = 1;
} else {
isEven[g] = -1;
}
for (set<int>::iterator i = group[g].begin(); i != group[g].end(); i++) {
parity[*i] = 0;
}
}
return (isEven[g] == 1 ? 0 : 1);
}
void addToIsland(int node, int island) {
if (seen[node]) return;
seen[node] = true;
islandOf[node] = island;
for (int i = 0; i < adj[node].size(); i++) {
addToIsland(adj[node][i], island);
}
}
void findclusterp(int node, int cluster, int parity = -1) {
if (false) cout << "Hi\n";
if (seen[node] == cluster) return;
if (evenCluster[cluster].count(node) == 0) return;
seen[node] = cluster;
if (false) cout << node << " " << parity << "\n";
if (parity == 1) clusterAlpha[cluster].insert(node);
for (int i = 0; i < adj[node].size(); i++) {
findclusterp(adj[node][i], cluster, parity * -1);
}
}
int main() {
cin >> nodes >> edges;
for (int i = 0; i < edges; i++) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
adjset[a].insert(b);
adjset[b].insert(a);
}
for (int i = 1; i <= nodes; i++) seen[i] = false;
for (int i = 1; i <= nodes; i++) {
if (!seen[i]) {
addToIsland(i, i);
findComponents(i);
}
}
if (false) {
for (int i = 1; i <= nodes; i++) {
if (iscut[i]) cout << i << "," << low[i] << "," << depth[i] << " ";
}
cout << "\n";
}
for (int i = 1; i <= nodes; i++) parent[i] = i;
for (int i = 1; i <= nodes; i++) {
seen[i] = false;
}
for (int i = 1; i <= nodes; i++) {
seen[i] = false;
}
for (int i = 0; i < counter; i++) {
if (parityOf(i) == 0) {
for (set<int>::iterator j = group[i].begin(); j != group[i].end(); j++) {
evenGroupOf[*j].push_back(i);
}
}
}
for (int i = 0; i <= nodes; i++) {
if (evenGroupOf[i].size() <= 1) continue;
for (int j = 1; j < evenGroupOf[i].size(); j++) {
join(evenGroupOf[i][j - 1], evenGroupOf[i][j]);
}
}
for (int i = 1; i <= nodes; i++) {
for (int j = 0; j < evenGroupOf[i].size(); i++) {
evenClusterOf[i].insert(find(evenGroupOf[i][j]));
evenCluster[find(evenGroupOf[i][j])].insert(i);
}
}
for (int i = 0; i <= nodes; i++) seen[i] = -1;
for (int i = 0; i <= nodes; i++) {
if (false) cout << evenCluster[i].size() << " ";
if (evenCluster[i].size() >= 1) {
findclusterp(*evenCluster[i].begin(), i);
}
}
if (false) {
cout << "#groups: " << counter << "\n";
for (int i = 0; i < counter; i++) {
for (set<int>::iterator j = group[i].begin(); j != group[i].end(); j++) {
cout << (*j) << " ";
}
cout << "(" << find(i) << ")";
if (parityOf(i) == 0) {
cout << "E "
<< " (";
for (set<int>::iterator j = clusterAlpha[find(i)].begin();
j != clusterAlpha[find(i)].end(); j++) {
cout << *j << " ";
}
cout << ")";
}
cout << "\n";
}
}
int queries;
cin >> queries;
for (int i = 0; i < queries; i++) {
int a, b;
cin >> a >> b;
if (answercache.count(make_pair(a, b)) == 1) {
cout << (answercache[make_pair(a, b)] ? "Yes\n" : "No\n");
continue;
}
bool ans;
if (islandOf[a] != islandOf[b]) {
ans = 0;
} else if (a == b) {
ans = 0;
} else {
ans = 1;
int inCommon = -1;
for (set<int>::iterator j = evenClusterOf[a].begin();
j != evenClusterOf[a].end(); j++) {
if (evenClusterOf[b].count(*j) == 1) {
ans = 0;
inCommon = *j;
}
}
if (ans == 0) {
if (clusterAlpha[inCommon].count(a) !=
clusterAlpha[inCommon].count(b)) {
ans = 1;
}
}
}
answercache[make_pair(a, b)] = ans;
cout << (ans ? "Yes\n" : "No\n");
}
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
struct DJS {
int p[101010];
void init(int n) {
for (int i = 0; i <= n; i++) p[i] = i;
}
int find_p(int x) { return x == p[x] ? x : p[x] = find_p(p[x]); }
void uni(int x, int y) { p[find_p(x)] = find_p(y); }
} djs, cc;
int n, m;
vector<int> g[101010], s[101010];
void init() {
scanf("%d%d", &n, &m);
djs.init(n);
cc.init(n);
while (m--) {
int ui, vi;
scanf("%d%d", &ui, &vi);
g[ui].push_back(vi);
g[vi].push_back(ui);
cc.uni(ui, vi);
}
}
bool got[101010], got2[101010];
int p[20][101010], dep[101010];
inline int lca(int ui, int vi) {
if (dep[ui] > dep[vi]) swap(ui, vi);
int dlt = dep[vi] - dep[ui];
for (int i = 0; i < 20; i++)
if ((dlt >> i) & 1) vi = p[i][vi];
if (ui == vi) return ui;
for (int i = 20 - 1; i >= 0; i--)
if (p[i][ui] != p[i][vi]) {
ui = p[i][ui];
vi = p[i][vi];
}
return p[0][ui];
}
vector<pair<int, int> > e;
void go(int now, int prt, int dp) {
got[now] = true;
dep[now] = dp;
p[0][now] = prt;
for (int son : g[now]) {
if (son == prt) continue;
if (got[son])
e.push_back({now, son});
else {
s[now].push_back(son);
go(son, now, dp + 1);
}
}
}
bool od[101010];
int sum[101010];
void go2(int now) {
got2[now] = true;
if (od[djs.find_p(now)] && now != djs.find_p(now)) sum[now]++;
for (int son : s[now]) {
sum[son] = sum[now];
go2(son);
}
}
void solve() {
for (int i = 1; i <= n; i++)
if (!got[i]) go(i, i, 0);
for (int i = 1; i < 20; i++)
for (int j = 1; j <= n; j++) p[i][j] = p[i - 1][p[i - 1][j]];
for (pair<int, int> tp : e) {
int ui = tp.first, vi = tp.second;
int lc = lca(ui, vi);
if ((dep[vi] - dep[ui]) % 2 == 0) od[lc] = true;
ui = djs.find_p(ui);
vi = djs.find_p(vi);
while (ui != vi) {
if (dep[ui] > dep[vi]) swap(ui, vi);
djs.uni(vi, p[0][vi]);
vi = djs.find_p(vi);
}
}
for (int i = 1; i <= n; i++)
if (od[i] && i != djs.find_p(i)) {
od[djs.find_p(i)] = true;
od[i] = false;
}
for (int i = 1; i <= n; i++)
if (!got2[i]) go2(i);
int q;
scanf("%d", &q);
while (q--) {
int ui, vi;
scanf("%d%d", &ui, &vi);
int lc = lca(ui, vi);
bool pos = false;
if (sum[ui] != sum[lc] || sum[vi] != sum[lc] || (dep[ui] + dep[vi]) % 2)
pos = true;
if (cc.find_p(ui) != cc.find_p(vi) || ui == vi) pos = false;
puts(pos ? "Yes" : "No");
}
}
int main() {
init();
solve();
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
template <typename T>
inline bool chkmax(T &a, T b) {
return a < b ? a = b, true : false;
}
template <typename T>
inline bool chkmin(T &a, T b) {
return a > b ? a = b, true : false;
}
template <typename T>
void read(T &first) {
int f = 1;
char ch;
for (ch = getchar(); !isdigit(ch); ch = getchar()) {
if (ch == '-') f = -1;
}
for (first = 0; isdigit(ch); ch = getchar()) {
first = first * 10 + ch - '0';
}
first *= f;
}
const int MAXN = 1e5 + 5, MAXM = 1e5 + 5;
struct Edge {
int v, next;
Edge() {}
Edge(int v_, int next_) : v(v_), next(next_) {}
};
int N, M, Q;
int tote, head[MAXN];
Edge edge[MAXM * 2 + 5];
int fa[MAXN], dep[MAXN], size[MAXN], hson[MAXN], top[MAXN];
int curtid, tid[MAXN];
int sum[MAXN];
int dfsclock, dfn[MAXN], low[MAXN];
std::pair<int, int> st[MAXN];
int sttop;
inline void addEdge(int u, int v) {
edge[++tote] = Edge(v, head[u]);
head[u] = tote;
}
void dfs1(int u) {
tid[u] = curtid;
size[u] = 1;
for (int i = head[u]; i; i = edge[i].next) {
int v = edge[i].v;
if (!tid[v]) {
fa[v] = u;
dep[v] = dep[u] + 1;
dfs1(v);
size[u] += size[v];
if (size[v] > size[hson[u]]) hson[u] = v;
}
}
}
void dfs2(int u) {
if (hson[u]) {
top[hson[u]] = top[u];
dfs2(hson[u]);
for (int i = head[u]; i; i = edge[i].next) {
int v = edge[i].v;
if (fa[v] == u && v != hson[u]) {
top[v] = v;
dfs2(v);
}
}
}
}
inline long long getLCA(int u, int v) {
while (top[u] != top[v]) {
if (dep[top[u]] > dep[top[v]])
u = fa[top[u]];
else
v = fa[top[v]];
}
return dep[u] > dep[v] ? v : u;
}
void tarjan(int u) {
dfn[u] = low[u] = ++dfsclock;
for (int i = head[u]; i; i = edge[i].next) {
int v = edge[i].v;
std::pair<int, int> cur = std::make_pair(u, v);
if (fa[v] == u) {
assert(dfn[v] == 0);
st[++sttop] = cur;
tarjan(v);
chkmin(low[u], low[v]);
if (low[v] >= dfn[u]) {
int f = 0;
for (int j = sttop;; --j) {
if ((dep[st[j].first] & 1) == (dep[st[j].second] & 1)) {
f = 1;
break;
}
if (st[j] == cur) break;
}
do {
if (st[sttop].first != u) sum[st[sttop].first] = f;
if (st[sttop].second != u) sum[st[sttop].second] = f;
} while (st[sttop--] != cur);
}
} else if (dfn[v] < dfn[u] && fa[u] != v) {
chkmin(low[u], dfn[v]);
st[++sttop] = cur;
}
}
}
void getsum(int u) {
sum[u] += sum[fa[u]];
for (int i = head[u]; i; i = edge[i].next) {
int v = edge[i].v;
if (fa[v] == u) getsum(v);
}
}
bool haveOddPath(int u, int v) {
if (tid[u] != tid[v]) return false;
if ((dep[u] & 1) != (dep[v] & 1)) return true;
int lca = getLCA(u, v);
return sum[u] + sum[v] - (sum[lca] << 1) > 0;
}
void input() {
read(N);
read(M);
for (int i = 1; i <= M; ++i) {
int u, v;
read(u);
read(v);
addEdge(u, v);
addEdge(v, u);
}
read(Q);
}
void solve() {
for (int i = 1; i <= N; ++i) {
if (!tid[i]) {
++curtid;
dep[i] = 1;
dfs1(i);
top[i] = i;
dfs2(i);
dfsclock = 0;
tarjan(i);
getsum(i);
}
}
while (Q--) {
int u, v;
read(u);
read(v);
puts(haveOddPath(u, v) ? "Yes" : "No");
}
}
int main() {
input();
solve();
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int read() {
int a = 0;
char c = getchar();
while (!isdigit(c)) c = getchar();
while (isdigit(c)) {
a = a * 10 + c - 48;
c = getchar();
}
return a;
}
const int _ = 2e5 + 7;
struct Edge {
int end, upEd;
};
int N, M;
namespace tree {
Edge Ed[_ << 1];
int head[_], cntEd, cnt;
long long val[_];
void addEd(int a, int b) {
Ed[++cntEd] = (Edge){b, head[a]};
head[a] = cntEd;
}
int dep[_], up[_][19];
long long sum[_][19];
void dfs(int x, int p) {
up[x][0] = p;
sum[x][0] = val[x];
dep[x] = dep[p] + 1;
for (int i = 1; up[x][i - 1]; ++i) {
up[x][i] = up[up[x][i - 1]][i - 1];
sum[x][i] = sum[x][i - 1] + sum[up[x][i - 1]][i - 1];
}
for (int i = head[x]; i; i = Ed[i].upEd)
if (Ed[i].end != p) dfs(Ed[i].end, x);
}
pair<int, long long> LCA(int x, int y) {
if (dep[x] < dep[y]) swap(x, y);
long long s = 0;
for (int i = 18; i >= 0; --i)
if (dep[x] - (1 << i) >= dep[y]) {
s += sum[x][i];
x = up[x][i];
}
if (x == y) return make_pair(x, s);
for (int i = 18; i >= 0; --i)
if (up[x][i] != up[y][i]) {
s = s + sum[x][i] + sum[y][i];
x = up[x][i];
y = up[y][i];
}
return make_pair(up[x][0],
s + val[x] + val[y] + (up[x][0] > N ? val[up[x][0]] : 0));
}
void work() {
dfs(0, 0);
for (int Q = read(); Q; --Q) {
int x = read(), y = read();
pair<int, long long> t = LCA(x, y);
if (t.first && ((t.second & 1) || t.second > cnt))
puts("Yes");
else
puts("No");
}
}
} // namespace tree
namespace graph {
Edge Ed[_ << 1];
int head[_], cntEd;
void addEd(int a, int b) {
Ed[++cntEd] = (Edge){b, head[a]};
head[a] = cntEd;
}
int col[_], dfn[_], low[_], stk[_], top, ts;
bool color(int x, int c) {
bool flg = 1;
col[x] = c;
for (int i = head[x]; i; i = Ed[i].upEd)
if (col[Ed[i].end] == col[x])
flg = 0;
else if (col[Ed[i].end] == -1)
flg &= color(Ed[i].end, c ^ 1);
return flg;
}
void pop(int x, int p) {
int cur = top, rt = ++tree::cnt;
col[p] = -1;
tree::addEd(p, rt);
do {
col[stk[cur]] = -1;
tree::addEd(rt, stk[cur]);
} while (stk[cur--] != x);
tree::val[rt] = !color(x, 0) * 1e6;
if (!p) col[p] = 0;
for (int i = cur + 1; i <= top; ++i) {
tree::val[stk[i]] = col[p] ^ col[stk[i]];
col[stk[i]] = 1e8;
}
top = cur;
col[p] = 1e8;
}
void tarjan(int x, int p) {
low[x] = dfn[x] = ++ts;
stk[++top] = x;
int cnt = 0;
for (int i = head[x]; i; i = Ed[i].upEd)
if (dfn[Ed[i].end])
low[x] = min(low[x], dfn[Ed[i].end]);
else {
tarjan(Ed[i].end, x);
low[x] = min(low[x], low[Ed[i].end]);
if (p && low[Ed[i].end] >= dfn[x] || (!p && ++cnt >= 2))
pop(Ed[i].end, x);
}
}
void init() {
memset(col, 0x3f, sizeof(col));
for (int i = 1; i <= M; ++i) {
int x = read(), y = read();
addEd(x, y);
addEd(y, x);
}
tree::cnt = N;
for (int i = 1; i <= N; ++i)
if (!dfn[i]) {
tarjan(i, 0);
pop(i, 0);
}
}
} // namespace graph
int main() {
N = read();
M = read();
graph::init();
tree::work();
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int n, m, q;
vector<int> v[100100];
int dept[100100];
int par[100100][22];
int dsu_par[100100];
bool tag[100100];
int root[100100];
int get_par(int now) {
return dsu_par[now] = (dsu_par[now] == now) ? now : get_par(dsu_par[now]);
}
void dfs1(int now) {
for (int i = 0; i < 20; i++) par[now][i + 1] = par[par[now][i]][i];
for (int i = 0; i < (int)v[now].size(); i++) {
if (dept[v[now][i]] < 0) {
root[v[now][i]] = root[now];
par[v[now][i]][0] = now;
dept[v[now][i]] = dept[now] + 1;
dfs1(v[now][i]);
if (get_par(now) == get_par(v[now][i]) && tag[v[now][i]]) tag[now] = 1;
} else if (dept[v[now][i]] < dept[now] - 1) {
if ((dept[now] - dept[v[now][i]]) % 2 == 0) tag[now] = 1;
int pos = get_par(now);
while (get_par(pos) != get_par(v[now][i]) && par[pos][0] != v[now][i]) {
dsu_par[get_par(pos)] = get_par(par[pos][0]);
pos = get_par(pos);
}
}
}
}
int sum[100100];
void dfs2(int now) {
sum[now] = sum[par[now][0]] + tag[now];
for (int i = 0; i < (int)v[now].size(); i++) {
if (dept[v[now][i]] == dept[now] + 1) {
if (get_par(v[now][i]) == get_par(now) && tag[now]) tag[v[now][i]] = 1;
dfs2(v[now][i]);
}
}
}
int lca(int a, int b) {
if (dept[a] > dept[b]) swap(a, b);
for (int i = 19; i >= 0; i--)
if (dept[par[b][i]] >= dept[a]) b = par[b][i];
for (int i = 19; i >= 0; i--)
if (par[b][i] != par[a][i]) b = par[b][i], a = par[a][i];
if (a == b) return a;
return par[a][0];
}
int main() {
ios::sync_with_stdio(false);
cin >> n >> m;
for (int i = 0; i < n; i++) dsu_par[i] = i;
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
a--, b--;
v[a].push_back(b);
v[b].push_back(a);
}
memset(dept, -1, sizeof(dept));
for (int i = 0; i < n; i++)
if (dept[i] < 0) {
root[i] = i;
dept[i] = 0;
for (int j = 0; j < 20; j++) par[i][j] = i;
dfs1(i), dfs2(i);
}
cin >> q;
for (int i = 0; i < q; i++) {
int a, b;
cin >> a >> b;
a--, b--;
int now_lca = lca(a, b);
if (root[a] == root[b] &&
((dept[a] + dept[b]) % 2 == 1 ||
sum[a] + sum[b] - sum[now_lca] - sum[now_lca] > 0))
cout << "Yes" << endl;
else
cout << "No" << endl;
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
template <typename T>
inline T read() {
register T sum = 0;
register char cc = getchar();
int sym = 1;
while (cc != '-' && (cc > '9' || cc < '0')) cc = getchar();
if (cc == '-') sym = -1, cc = getchar();
sum = sum * 10 + cc - '0';
cc = getchar();
while (cc >= '0' && cc <= '9') sum = sum * 10 + cc - '0', cc = getchar();
return sym * sum;
}
template <typename T>
inline T read(T &a) {
a = read<T>();
return a;
}
template <typename T, typename... Others>
inline void read(T &a, Others &...b) {
a = read(a);
read(b...);
}
struct Edge {
int v;
Edge *next;
Edge(int a = 0, Edge *b = NULL) {
v = a;
next = b;
}
} * head[100010];
struct Node {
int v;
int tag;
} node[100010 << 2];
int n, m, Q, cnt, f[100010], x[100010], y[100010], used[100010];
int fa[100010], top[100010], dep[100010], dfn[100010], siz[100010], son[100010];
void Effect(int k, int l, int r, int v) {
node[k].v += (r - l + 1) * v;
node[k].tag += v;
}
void down(int k, int l, int r) {
if (node[k].tag) {
int mid = (l + r) >> 1;
Effect(k << 1, l, mid, node[k].tag);
Effect(k << 1 | 1, mid + 1, r, node[k].tag);
node[k].tag = 0;
}
}
void change(int k, int l, int r, int x, int y) {
if (l >= x && r <= y) {
Effect(k, l, r, 1);
return;
}
down(k, l, r);
int mid = (l + r) >> 1;
if (x <= mid) change(k << 1, l, mid, x, y);
if (y > mid) change(k << 1 | 1, mid + 1, r, x, y);
node[k].v = node[k << 1].v + node[k << 1 | 1].v;
}
int query(int k, int l, int r, int x, int y) {
if (l >= x && r <= y) return node[k].v;
down(k, l, r);
int mid = (l + r) >> 1, ans = 0;
if (x <= mid) ans += query(k << 1, l, mid, x, y);
if (y > mid) ans += query(k << 1 | 1, mid + 1, r, x, y);
return ans;
}
void change(int x, int y) {
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]]) swap(x, y);
change(1, 1, n, dfn[top[x]], dfn[x]);
x = fa[top[x]];
}
if (dfn[x] > dfn[y]) swap(x, y);
if (dfn[x] < dfn[y]) change(1, 1, n, dfn[x] + 1, dfn[y]);
}
bool query(int x, int y) {
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]]) swap(x, y);
if (query(1, 1, n, dfn[top[x]], dfn[x])) return true;
x = fa[top[x]];
}
if (dfn[x] > dfn[y]) swap(x, y);
if (dfn[x] < dfn[y]) return query(1, 1, n, dfn[x] + 1, dfn[y]);
return false;
}
void dfs1(int k) {
siz[k] = 1;
for (Edge *i = head[k]; i != NULL; i = i->next) {
if (i->v == fa[k]) continue;
fa[i->v] = k;
dep[i->v] = dep[k] + 1;
dfs1(i->v);
siz[k] += siz[i->v];
if (siz[son[k]] < siz[i->v]) son[k] = i->v;
}
}
void dfs2(int k, int t) {
top[k] = t;
dfn[k] = ++cnt;
if (!son[k]) return;
dfs2(son[k], t);
for (Edge *i = head[k]; i != NULL; i = i->next)
if (i->v != fa[k] && i->v != son[k]) dfs2(i->v, i->v);
}
int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); }
int main() {
read(n, m);
for (int i = 1; i <= n; i++) f[i] = i;
for (int i = 1; i <= m; i++) {
read(x[i], y[i]);
if (find(x[i]) != find(y[i])) {
head[x[i]] = new Edge(y[i], head[x[i]]);
head[y[i]] = new Edge(x[i], head[y[i]]);
f[find(x[i])] = find(y[i]);
used[i] = true;
}
}
for (int i = 1; i <= n; i++)
if (!dfn[i]) {
dfs1(i);
dfs2(i, i);
}
for (int i = 1; i <= m; i++)
if (!used[i] && !((dep[x[i]] & 1) ^ (dep[y[i]] & 1))) change(x[i], y[i]);
for (int i = 1; i <= m; i++)
if (!used[i] && ((dep[x[i]] & 1) ^ (dep[y[i]] & 1)) && query(x[i], y[i]))
change(x[i], y[i]);
Q = read<int>();
for (int i = 1; i <= Q; i++) {
int x, y;
read(x, y);
if (find(x) != find(y) || (!((dep[x] & 1) ^ (dep[y] & 1)) && !query(x, y)))
puts("No");
else
puts("Yes");
}
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int n, m, q, dsu[100100], dis[100100];
int find(int x) { return dsu[x] == x ? x : dsu[x] = find(dsu[x]); }
void merge(int x, int y) {
x = find(x), y = find(y);
if (x != y) dsu[y] = x;
}
namespace Tree {
int head[200100], cnt;
struct Edge {
int to, next;
} edge[400100];
void ae(int u, int v) {
edge[cnt].next = head[u], edge[cnt].to = v, head[u] = cnt++;
edge[cnt].next = head[v], edge[cnt].to = u, head[v] = cnt++;
}
int tot;
bool odd[200100], ok[200100][20];
int anc[200100][20], dep[200100];
void dfs(int x, int fa) {
anc[x][0] = fa, ok[x][0] = odd[fa], dep[x] = dep[fa] + 1;
for (int i = 1; (1 << i) < dep[x]; i++)
anc[x][i] = anc[anc[x][i - 1]][i - 1],
ok[x][i] = (ok[x][i - 1] | ok[anc[x][i - 1]][i - 1]);
for (int i = head[x]; i != -1; i = edge[i].next)
if (edge[i].to != fa) dfs(edge[i].to, x);
}
bool query(int x, int y) {
if (find(x) != find(y)) return false;
if ((dis[x] ^ dis[y]) & 1) return true;
if (dep[x] > dep[y]) swap(x, y);
bool ret = false;
for (int i = 19; i >= 0; i--)
if (dep[x] <= dep[y] - (1 << i)) ret |= ok[y][i], y = anc[y][i];
if (x == y) return ret;
for (int i = 19; i >= 0; i--)
if (anc[x][i] != anc[y][i])
ret |= (ok[x][i] | ok[y][i]), x = anc[x][i], y = anc[y][i];
return ret | ok[x][0];
}
} // namespace Tree
namespace Graph {
int head[100100], cnt;
struct Edge {
int to, next;
} edge[200100];
void ae(int u, int v) {
edge[cnt].next = head[u], edge[cnt].to = v, head[u] = cnt++;
edge[cnt].next = head[v], edge[cnt].to = u, head[v] = cnt++;
}
priority_queue<pair<int, int> > q;
bool vis[100100];
void Dijkstra(int S) {
dis[S] = 0, q.push(make_pair(0, S));
while (!q.empty()) {
int x = q.top().second;
q.pop();
if (vis[x]) continue;
vis[x] = true;
for (int i = head[x], y; i != -1; i = edge[i].next)
if (dis[y = edge[i].to] > dis[x] + 1)
dis[y] = dis[x] + 1, q.push(make_pair(-dis[y], y));
}
}
stack<int> s;
int dfn[100100], low[100100], tot, col[100100], DYE[100100];
bool Dye(int x) {
for (int i = head[x]; i != -1; i = edge[i].next) {
if (col[edge[i].to] != Tree::tot) continue;
if (!DYE[edge[i].to]) {
DYE[edge[i].to] = DYE[x] ^ 3;
if (Dye(edge[i].to)) return true;
} else if (DYE[edge[i].to] == DYE[x])
return true;
}
return false;
}
void Tarjan(int x) {
dfn[x] = low[x] = ++tot, s.push(x);
for (int i = head[x]; i != -1; i = edge[i].next) {
if (!dfn[edge[i].to]) {
Tarjan(edge[i].to), low[x] = min(low[x], low[edge[i].to]);
if (low[edge[i].to] >= dfn[x]) {
Tree::tot++;
while (true) {
int y = s.top();
Tree::ae(Tree::tot, y), col[y] = Tree::tot, DYE[y] = 0, s.pop();
if (y == edge[i].to) break;
}
Tree::ae(Tree::tot, x), DYE[x] = 1, col[x] = Tree::tot;
Tree::odd[Tree::tot] = Dye(x);
}
} else
low[x] = min(low[x], dfn[edge[i].to]);
}
}
} // namespace Graph
int main() {
scanf("%d%d", &n, &m), Tree::tot = n,
memset(Tree::head, -1, sizeof(Tree::head)),
memset(Graph::head, -1, sizeof(Graph::head)),
memset(dis, 0x3f, sizeof(dis));
for (int i = 1; i <= n; i++) dsu[i] = i;
for (int i = 1, x, y; i <= m; i++)
scanf("%d%d", &x, &y), Graph::ae(x, y), merge(x, y);
for (int i = 1; i <= n; i++) {
if (dsu[i] != i) continue;
Graph::Dijkstra(i);
while (!Graph::s.empty()) Graph::s.pop();
Graph::Tarjan(i);
Tree::dfs(i, 0);
}
scanf("%d", &q);
for (int i = 1, x, y; i <= q; i++)
scanf("%d%d", &x, &y), puts(Tree::query(x, y) ? "Yes" : "No");
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int n, m, Q, fa[N], f[N][18];
int dep[N], S[N], fl[N];
vector<int> e[N];
int get(int x) { return x == fa[x] ? x : fa[x] = get(fa[x]); }
void dfs1(int x) {
dep[x] = dep[f[x][0]] + 1;
for (auto i : e[x])
if (!dep[i]) {
f[i][0] = x;
dfs1(i);
if (get(x) == get(i)) fl[x] |= fl[i];
} else if (dep[i] + 1 < dep[x]) {
if ((dep[i] + dep[x] + 1) & 1) fl[x] = 1;
for (int y = get(x); dep[y] > dep[i] + 1; y = get(y)) fa[y] = f[y][0];
}
}
void dfs2(int x) {
S[x] += fl[x];
for (auto i : e[x])
if (dep[i] == dep[x] + 1) {
if (get(x) == get(i)) fl[i] |= fl[x];
S[i] = S[x];
dfs2(i);
}
}
int LCA(int x, int y) {
if (dep[x] < dep[y]) swap(x, y);
int tmp = dep[x] - dep[y];
for (int i = (int)(16); i >= (int)(0); i--)
if (tmp & (1 << i)) x = f[x][i];
for (int i = (int)(16); i >= (int)(0); i--)
if (f[x][i] != f[y][i]) x = f[x][i], y = f[y][i];
return x == y ? x : f[x][0];
}
bool ask(int x, int y) {
int L = LCA(x, y);
if (!L) return 0;
if ((dep[x] + dep[y]) & 1) return 1;
return S[x] + S[y] - 2 * S[L];
}
int main() {
scanf("%d%d", &n, &m);
for (int i = (int)(1); i <= (int)(m); i++) {
int x, y;
scanf("%d%d", &x, &y);
e[x].push_back(y);
e[y].push_back(x);
}
for (int i = (int)(1); i <= (int)(n); i++) fa[i] = i;
for (int i = (int)(1); i <= (int)(n); i++)
if (!dep[i]) dfs1(i), dfs2(i);
for (int i = (int)(1); i <= (int)(16); i++)
for (int j = (int)(1); j <= (int)(n); j++) f[j][i] = f[f[j][i - 1]][i - 1];
scanf("%d", &Q);
while (Q--) {
int x, y;
scanf("%d%d", &x, &y);
puts(ask(x, y) ? "Yes" : "No");
}
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int n, m, q;
vector<int> G[1000006];
int dfn[1000006], low[1000006], g[1000006][19], clo, stk[1000006], top,
ins[1000006], dep[1000006], odd[1000006];
int od[1000006], bel[1000006];
void tarjan(int u, int f) {
bel[u] = bel[f];
dfn[u] = low[u] = ++clo, ins[u] = 1;
stk[++top] = u;
for (int v : G[u]) {
if (v == f) continue;
if (!dfn[v]) {
g[v][0] = u;
for (int k = (1), kend = (18); k <= kend; ++k)
if (g[g[v][k - 1]][k - 1])
g[v][k] = g[g[v][k - 1]][k - 1];
else
break;
dep[v] = dep[u] + 1;
tarjan(v, u), low[u] = min(low[u], low[v]);
} else if (ins[v])
low[u] = min(low[u], dfn[v]), odd[u] |= (~(dep[v] + dep[u]) & 1);
}
if (dfn[u] == low[u]) {
int flg = 0, t = top;
while (stk[t] != u && !flg) flg = odd[stk[t]], --t;
while (stk[top] != u) {
od[stk[top]] |= flg;
ins[stk[top]] = 0;
--top;
}
--top, ins[u] = 0;
}
}
inline int lca(int u, int v) {
if (dep[u] < dep[v]) u ^= v ^= u ^= v;
if (dep[u] != dep[v])
for (int k = (18), kend = (0); k >= kend; --k)
if (dep[g[u][k]] >= dep[v]) u = g[u][k];
if (u == v) return u;
for (int k = (18), kend = (0); k >= kend; --k)
if (g[u][k] != g[v][k]) u = g[u][k], v = g[v][k];
return g[u][0];
}
int S[1000006];
void dfs(int u, int f) {
S[u] += od[u];
for (int v : G[u])
if (v != f && dep[v] == dep[u] + 1) S[v] += S[u], dfs(v, u);
}
inline bool fuck(int u, int v) {
if (bel[u] != bel[v] || u == v) return false;
if (dep[u] + dep[v] & 1) return true;
return S[u] + S[v] - 2 * S[lca(u, v)] > 0;
}
void solve() {
cin >> n >> m;
int u, v;
for (int i = (1), iend = (m); i <= iend; ++i) {
scanf("%d%d", &u, &v);
G[u].push_back(v), G[v].push_back(u);
}
for (int i = (1), iend = (n); i <= iend; ++i)
if (!dfn[i]) bel[i] = i, dep[i] = 1, tarjan(i, i), dfs(i, i);
cin >> q;
for (int i = (1), iend = (q); i <= iend; ++i)
scanf("%d%d", &u, &v), puts(fuck(u, v) ? "Yes" : "No");
}
signed main() { solve(); }
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
namespace zzc {
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - 48;
ch = getchar();
}
return x * f;
}
const int maxn = 2e5 + 5;
int head[maxn], son[maxn], siz[maxn], dfn[maxn], top[maxn], fa[maxn];
int val[maxn << 2], f[maxn], dep[maxn];
int n, m, q, cnt = 0, idx;
struct edge {
int to, nxt;
} e[maxn << 1];
void add(int u, int v) {
e[++cnt].to = v;
e[cnt].nxt = head[u];
head[u] = cnt;
}
int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); }
void pushdown(int rt, int l, int r) {
if (val[rt] == r - l + 1) {
int mid = (l + r) >> 1;
val[rt << 1] = mid - l + 1;
val[rt << 1 | 1] = r - mid;
}
}
void pushup(int rt) { val[rt] = val[rt << 1] + val[rt << 1 | 1]; }
void update(int rt, int l, int r, int ql, int qr) {
if (ql <= l && r <= qr) {
val[rt] = r - l + 1;
return;
}
pushdown(rt, l, r);
int mid = (l + r) >> 1;
if (ql <= mid) update(rt << 1, l, mid, ql, qr);
if (qr > mid) update(rt << 1 | 1, mid + 1, r, ql, qr);
pushup(rt);
}
void update_tree(int x, int y) {
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]]) swap(x, y);
update(1, 1, n, dfn[top[x]], dfn[x]);
x = fa[top[x]];
}
if (dep[x] < dep[y]) swap(x, y);
update(1, 1, n, dfn[y] + 1, dfn[x]);
}
int query(int rt, int l, int r, int ql, int qr) {
if (ql <= l && r <= qr) return val[rt];
pushdown(rt, l, r);
int mid = (l + r) >> 1;
int res = 0;
if (ql <= mid) res += query(rt << 1, l, mid, ql, qr);
if (qr > mid) res += query(rt << 1 | 1, mid + 1, r, ql, qr);
return res;
}
int query_tree(int x, int y) {
int res = 0;
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]]) swap(x, y);
res += query(1, 1, n, dfn[top[x]], dfn[x]);
x = fa[top[x]];
}
if (dep[x] < dep[y]) swap(x, y);
res += query(1, 1, n, dfn[y], dfn[x]);
return res;
}
void dfs1(int u, int ff) {
dep[u] = dep[ff] + 1;
fa[u] = ff;
siz[u] = 1;
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].to;
if (v == ff || dep[v]) continue;
dfs1(v, u);
siz[u] += siz[v];
if (siz[son[u]] < siz[v]) son[u] = v;
}
}
void dfs2(int u, int bel) {
top[u] = bel;
dfn[u] = ++idx;
if (son[u]) dfs2(son[u], bel);
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].to;
if (v == son[u] || dep[v] < dep[u]) continue;
if (fa[v] == u) dfs2(v, v);
}
}
void dfs3(int u) {
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].to;
if (v == fa[u]) continue;
if (fa[v] == u)
dfs3(v);
else if (dep[v] > dep[u])
continue;
else if (!((dep[u] - dep[v]) & 1))
update_tree(u, v);
}
}
void dfs4(int u) {
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].to;
if (v == fa[u]) continue;
if (fa[v] == u)
dfs4(v);
else if (dep[v] > dep[u])
continue;
else if ((dep[u] - dep[v]) & 1) {
int tmp = query_tree(u, v) - query(1, 1, n, dfn[v], dfn[v]);
if (tmp > 0) update_tree(u, v);
}
}
}
int lca(int x, int y) {
while (top[x] != top[y]) {
if (dep[top[x]] > dep[top[y]])
x = fa[top[x]];
else
y = fa[top[y]];
}
return dep[x] < dep[y] ? x : y;
}
int dis(int x, int y) { return (dep[x] + dep[y] - (dep[lca(x, y)] << 1)); }
void work() {
int a, b;
n = read();
m = read();
for (int i = 1; i <= n; i++) f[i] = i;
for (int i = 1; i <= m; i++) {
a = read();
b = read();
add(a, b);
add(b, a);
int fx = find(a);
int fy = find(b);
f[fy] = fx;
}
for (int i = 1; i <= n; i++)
if (!dep[i]) {
dfs1(i, 0);
dfs2(i, i);
dfs3(i);
dfs4(i);
}
q = read();
for (int i = 1; i <= q; i++) {
a = read();
b = read();
if (find(a) != find(b))
printf("No\n");
else {
int tmp = lca(a, b);
if (dis(a, b) & 1)
printf("Yes\n");
else if (query_tree(a, b) - query(1, 1, n, dfn[tmp], dfn[tmp]) > 0)
printf("Yes\n");
else
printf("No\n");
}
}
}
} // namespace zzc
int main() {
zzc::work();
return 0;
}
| CPP |
97_E. Leaders | After a revolution in Berland the new dictator faced an unexpected challenge: the country has to be somehow ruled. The dictator is a very efficient manager, yet he can't personally give orders to each and every citizen. That's why he decided to pick some set of leaders he would control. Those leaders will directly order the citizens. However, leadership efficiency turned out to vary from person to person (i.e. while person A makes an efficient leader, person B may not be that good at it). That's why the dictator asked world-famous berland scientists for help. The scientists suggested an innovatory technology β to make the leaders work in pairs.
A relationship graph is some undirected graph whose vertices correspond to people. A simple path is a path with no repeated vertices. Long and frighteningly expensive research showed that a pair of people has maximum leadership qualities if a graph of relationships has a simple path between them with an odd number of edges. The scientists decided to call such pairs of different people leader pairs. Secret services provided the scientists with the relationship graph so that the task is simple β we have to learn to tell the dictator whether the given pairs are leader pairs or not. Help the scientists cope with the task.
Input
The first line contains integers n and m (1 β€ n β€ 105, 0 β€ m β€ 105) β the number of vertices and edges in the relationship graph correspondingly. Next m lines contain pairs of integers a and b which mean that there is an edge between the a-th and the b-th vertices (the vertices are numbered starting from 1, 1 β€ a, b β€ n). It is guaranteed that the graph has no loops or multiple edges.
Next line contains number q (1 β€ q β€ 105) β the number of pairs the scientists are interested in. Next q lines contain these pairs (in the same format as the edges, the queries can be repeated, a query can contain a pair of the identical vertices).
Output
For each query print on a single line "Yes" if there's a simple odd path between the pair of people; otherwise, print "No".
Examples
Input
7 7
1 3
1 4
2 3
2 4
5 6
6 7
7 5
8
1 2
1 3
1 4
2 4
1 5
5 6
5 7
6 7
Output
No
Yes
Yes
Yes
No
Yes
Yes
Yes
Note
Notes to the samples:
1) Between vertices 1 and 2 there are 2 different simple paths in total: 1-3-2 and 1-4-2. Both of them consist of an even number of edges.
2) Vertices 1 and 3 are connected by an edge, that's why a simple odd path for them is 1-3.
5) Vertices 1 and 5 are located in different connected components, there's no path between them. | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10, maxm = maxn << 1;
int read() {
int x = 0, f = 1;
char c = getchar();
while (!isdigit(c)) {
if (c == '-') f = 0;
c = getchar();
}
while (isdigit(c)) {
x = (x << 3) + (x << 1) + (c ^ 48);
c = getchar();
}
return f ? x : -x;
}
int to[maxm], nxt[maxm], head[maxn], e;
int dep[maxn], anc[20][maxn], w[maxn], vis[maxn];
void Add(int x, int y) {
to[++e] = y;
nxt[e] = head[x];
head[x] = e;
to[++e] = x;
nxt[e] = head[y];
head[y] = e;
}
void dfs(int u, int fa) {
dep[u] = dep[fa] + 1;
anc[0][u] = fa;
for (int i = head[u]; i; i = nxt[i]) {
int v = to[i];
if (v == fa) continue;
dfs(v, u);
}
}
int lca(int u, int v) {
if (dep[u] < dep[v]) swap(u, v);
for (int i = 19; i >= 0; --i)
if (dep[anc[i][u]] >= dep[v]) u = anc[i][u];
if (u == v) return u;
for (int i = 19; i >= 0; --i)
if (anc[i][u] != anc[i][v]) u = anc[i][u], v = anc[i][v];
return anc[0][u];
}
void gao(int u) {
w[u] += w[anc[0][u]];
for (int i = head[u]; i; i = nxt[i]) {
int v = to[i];
if (v == anc[0][u]) continue;
gao(v);
}
}
namespace tra {
int to[maxm], head[maxn], nxt[maxm], e;
void add(int x, int y) {
to[++e] = y;
nxt[e] = head[x];
head[x] = e;
to[++e] = x;
nxt[e] = head[y];
head[y] = e;
}
void dfs(int u, int fg) {
vis[u] = fg;
for (int i = head[u]; i; i = nxt[i]) {
int v = to[i];
if (vis[v]) continue;
Add(u, v);
dfs(v, fg);
}
}
int dfn[maxn], low[maxn], top, tot;
pair<int, int> sta[maxn];
void tarjan(int u, int fa) {
dfn[u] = low[u] = ++tot;
for (int i = head[u]; i; i = nxt[i]) {
int v = to[i];
if (v == fa || dfn[v] > dfn[u]) continue;
sta[++top] = make_pair(u, v);
if (!dfn[v])
tarjan(v, u), low[u] = min(low[u], low[v]);
else
low[u] = min(low[u], dfn[v]);
}
if (low[u] >= dfn[fa]) {
int fg = 0;
if (!top) return;
int tmp = top;
while (1) {
if (!top) break;
if ((dep[sta[top].first] & 1) == (dep[sta[top].second] & 1)) fg = 1;
--top;
if (sta[top + 1].first == fa && sta[top + 1].second == u) break;
}
if (!fg) return;
top = tmp;
while (1) {
if (!top) break;
if (anc[0][sta[top].first] == sta[top].second) w[sta[top].first]++;
if (anc[0][sta[top].second] == sta[top].first) w[sta[top].second]++;
--top;
if (sta[top + 1].first == fa && sta[top + 1].second == u) break;
}
}
}
} // namespace tra
int main() {
int n = read(), m = read();
for (int i = 1; i <= m; ++i) tra::add(read(), read());
for (int i = 1; i <= n; ++i)
if (!vis[i]) tra::dfs(i, i);
for (int i = 1; i <= n; ++i)
if (!dep[i]) dfs(i, 0);
for (int i = 1; i <= 19; ++i)
for (int j = 1; j <= n; ++j) anc[i][j] = anc[i - 1][anc[i - 1][j]];
for (int i = 1; i <= n; ++i)
if (vis[i] == i) tra::tarjan(i, 0), gao(i);
int Q = read();
while (Q--) {
int x = read(), y = read();
if (vis[x] != vis[y]) {
puts("No");
continue;
}
int tmp = lca(x, y);
if (((dep[x] + dep[y] - dep[tmp] * 2) & 1) || w[x] + w[y] - w[tmp] * 2)
puts("Yes");
else
puts("No");
}
return 0;
}
| CPP |
99_E. Help Greg the Dwarf | A very unusual citizen lives in a far away kingdom β Dwarf Gracula. However, his unusual name is not the weirdest thing (besides, everyone long ago got used to calling him simply Dwarf Greg). What is special about Dwarf Greg β he's been living for over 200 years; besides, he lives in a crypt on an abandoned cemetery and nobody has ever seen him out in daytime. Moreover, nobody has ever seen Greg buy himself any food. That's why nobody got particularly surprised when after the infernal dragon's tragic death cattle continued to disappear from fields. The people in the neighborhood were long sure that the harmless dragon was never responsible for disappearing cattle (considering that the dragon used to be sincere about his vegetarian views). But even that's not the worst part of the whole story.
The worst part is that merely several minutes ago Dwarf Greg in some unintelligible way got inside your house and asked you to help him solve a problem. The point is that a short time ago Greg decided to order a new coffin (knowing his peculiar character, you are not surprised at all). But the problem is: a very long in both directions L-shaped corridor leads to Greg's crypt, and you can't drag just any coffin through that corridor. That's why he asked you to help.
<image>
You've formalized the task on a plane like this: let the corridor's width before and after the turn be equal to a and b correspondingly (see the picture). The corridor turns directly at a right angle, the coffin is a rectangle whose length and width are equal to l and w (l β₯ w) correspondingly. Dwarf Greg has already determined the coffin's length (l), which is based on his height; your task is to determine the coffin's maximally possible width (w), at which it can be brought to the crypt. Besides, due to its large mass (pure marble!) the coffin is equipped with rotating wheels; therefore it is impossible to lift it off the ground, however, arbitrary moves and rotations of the coffin in the plane become possible. The coffin may be rotated arbitrarily just before you drag it into crypt and move through the corridor.
Greg promised that if you help him, he will grant you immortality (I wonder how?). And if you don't, well... trust me, you don't want to know what happens if you don't help him...
Input
The first line contains three space-separated integers a, b and l from the problem's statement (1 β€ a, b, l β€ 104).
Output
Print the maximally possible width of a coffin with absolute or relative error no more than 10 - 7. If a coffin with the given length and positive width (the coffin that would meet the conditions from the problem's statement) does not exist, print "My poor head =(" (without quotes).
It is guaranteed that if the answer is positive, it will be not less than 10 - 7. All the hacks will also be checked to meet that condition.
Examples
Input
2 2 1
Output
1.0000000
Input
2 2 2
Output
2.0000000
Input
2 2 3
Output
1.3284271
Input
2 2 6
Output
My poor head =(
Note
In the first example the answer is restricted by the coffin's length (remember β coffin's widths should not be larger than it's length).
In the second example it is possible to drag the coffin through the corridor thanks to rotating wheels: firstly, drag it forward by one side while it will not be hampered by the wall, then move it forward by adjacent side perpendicularly to the initial movement direction (remember β arbitrary moves and rotations of the coffin are possible). | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
template <typename T, typename TT>
ostream& operator<<(ostream& s, pair<T, TT> t) {
return s << "(" << t.first << "," << t.second << ")";
}
template <typename T>
ostream& operator<<(ostream& s, vector<T> t) {
for (int i = 0; i < t.size(); i++) s << t[i] << " ";
return s;
}
double a, b, len;
const double eps = 1e-10;
double cal(double first) {
double w;
double p = acos(first / len), q = atan2(a, b - first), d = a / sin(q);
w = d * sin(q + p);
return w;
}
double check() {
double l = 0, r = len;
while (r - l > eps) {
double x1 = (2. * l + r) / 3.;
double x2 = (2. * r + l) / 3.;
if (cal(x1) + eps > cal(x2))
l = x1;
else
r = x2;
}
return cal((l + r + eps) / 2.);
}
int main() {
int i, j, T, k, n;
while (~scanf("%lf%lf%lf", &a, &b, &len)) {
if (a > b) swap(a, b);
if (len <= b) {
printf("%lf\n", min(a, len));
continue;
}
double ans = min(min(a, b), len);
ans = min(ans, check());
if (ans < 1e-8)
puts("My poor head =(");
else
printf("%.12lf\n", ans);
}
return 0;
}
| CPP |
99_E. Help Greg the Dwarf | A very unusual citizen lives in a far away kingdom β Dwarf Gracula. However, his unusual name is not the weirdest thing (besides, everyone long ago got used to calling him simply Dwarf Greg). What is special about Dwarf Greg β he's been living for over 200 years; besides, he lives in a crypt on an abandoned cemetery and nobody has ever seen him out in daytime. Moreover, nobody has ever seen Greg buy himself any food. That's why nobody got particularly surprised when after the infernal dragon's tragic death cattle continued to disappear from fields. The people in the neighborhood were long sure that the harmless dragon was never responsible for disappearing cattle (considering that the dragon used to be sincere about his vegetarian views). But even that's not the worst part of the whole story.
The worst part is that merely several minutes ago Dwarf Greg in some unintelligible way got inside your house and asked you to help him solve a problem. The point is that a short time ago Greg decided to order a new coffin (knowing his peculiar character, you are not surprised at all). But the problem is: a very long in both directions L-shaped corridor leads to Greg's crypt, and you can't drag just any coffin through that corridor. That's why he asked you to help.
<image>
You've formalized the task on a plane like this: let the corridor's width before and after the turn be equal to a and b correspondingly (see the picture). The corridor turns directly at a right angle, the coffin is a rectangle whose length and width are equal to l and w (l β₯ w) correspondingly. Dwarf Greg has already determined the coffin's length (l), which is based on his height; your task is to determine the coffin's maximally possible width (w), at which it can be brought to the crypt. Besides, due to its large mass (pure marble!) the coffin is equipped with rotating wheels; therefore it is impossible to lift it off the ground, however, arbitrary moves and rotations of the coffin in the plane become possible. The coffin may be rotated arbitrarily just before you drag it into crypt and move through the corridor.
Greg promised that if you help him, he will grant you immortality (I wonder how?). And if you don't, well... trust me, you don't want to know what happens if you don't help him...
Input
The first line contains three space-separated integers a, b and l from the problem's statement (1 β€ a, b, l β€ 104).
Output
Print the maximally possible width of a coffin with absolute or relative error no more than 10 - 7. If a coffin with the given length and positive width (the coffin that would meet the conditions from the problem's statement) does not exist, print "My poor head =(" (without quotes).
It is guaranteed that if the answer is positive, it will be not less than 10 - 7. All the hacks will also be checked to meet that condition.
Examples
Input
2 2 1
Output
1.0000000
Input
2 2 2
Output
2.0000000
Input
2 2 3
Output
1.3284271
Input
2 2 6
Output
My poor head =(
Note
In the first example the answer is restricted by the coffin's length (remember β coffin's widths should not be larger than it's length).
In the second example it is possible to drag the coffin through the corridor thanks to rotating wheels: firstly, drag it forward by one side while it will not be hampered by the wall, then move it forward by adjacent side perpendicularly to the initial movement direction (remember β arbitrary moves and rotations of the coffin are possible). | 2 | 11 | #include <bits/stdc++.h>
using namespace std;
int a, b, l;
double f(double x) { return a * cos(x) + b * sin(x) - l * sin(x) * cos(x); }
int main() {
scanf("%d%d%d", &a, &b, &l);
if (a > b) swap(a, b);
if (l <= b) {
printf("%.7f", (double)min(a, l));
return 0;
}
double L = 0, R = acos(0);
for (int _ = 100; _--;) {
double lm = (L * 2 + R) / 3, rm = (L + R * 2) / 3;
if (f(lm) > f(rm))
L = lm;
else
R = rm;
}
if (f(L) >= 1e-7)
printf("%.7f", min(f(L), (double)l));
else
puts("My poor head =(");
return 0;
}
| CPP |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.