This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
int N, M, Q;
vvpii adj;
vpii E;
struct DSU
{
vi arr;
vi color;
vi cxorp; // xor with color of parent?
bool oddCycle;
void init()
{
oddCycle = false, arr.resize(N);
iota(all(arr), 0);
color.assign(N, 0), cxorp.assign(N, 0);
}
int find(int i)
{
if (arr[i] == i)
return i;
int p = arr[i];
arr[i] = find(p);
color[i] = color[p] ^ cxorp[i];
cxorp[i] = cxorp[i] ^ cxorp[p];
return arr[i];
}
bool join(int a, int b)
{
int pa = find(a), pb = find(b); // get color to newest
bool sameColor = (color[a] == color[b]);
if (pa == pb)
{
oddCycle = oddCycle || sameColor;
return sameColor;
}
arr[pb] = pa;
if (sameColor)
{
cxorp[pb] = 1;
color[pb] = color[pa] ^ cxorp[pb];
}
return false;
}
};
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin >> N >> M >> Q;
adj.resize(N);
E.resize(M);
for (int i = 0, a, b; i < M; ++i)
{
cin >> a >> b;
--a, --b;
adj[a].push_back({b, i}), adj[b].push_back({a, i});
E[i] = {min(a, b), max(a, b)};
}
vvpii Queries(M + 1);
vi ans(Q);
for (int i = 0, l, r; i < Q; ++i)
{
cin >> l >> r;
--l, --r; // [l, r] inclusive is blocked
l = min(M, l), r = min(M, r);
Queries[l].push_back({r, i});
}
DSU dsu;
for (int l = 0; l < sz(Queries); ++l)
{
if (Queries[l].empty())
continue;
dsu.init();
for (int i = 0; i < l; ++i)
dsu.join(E[i].first, E[i].second);
int r = M - 1;
while (!dsu.oddCycle && r >= l)
{
dsu.join(E[r].first, E[r].second);
--r;
}
for (pii q : Queries[l])
ans[q.second] = (q.first <= r);
}
for (int a : ans)
if (a)
cout << "YES\n";
else
cout << "NO\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |