This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,fma,bmi,bmi2,sse4.2,popcnt,lzcnt")
*/
#include <bits/stdc++.h>
#define taskname "NOHOME"
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define i64 long long
#define pb push_back
#define ff first
#define ss second
#define isz(x) (int)x.size()
using namespace std;
const int mxN = 2e5 + 5;
const int mod = 1e9 + 7;
const i64 oo = 1e18;
struct DisjointSetUnion01Rollback {
vector<int> lab, val;
vector<pair<int, int>> changes;
DisjointSetUnion01Rollback() {}
DisjointSetUnion01Rollback(int n) {
init(n);
}
void init(int n) {
lab.assign(n + 1, -1);
val.assign(n + 1, 0);
changes.clear();
}
pair<int, int> root(int v) {
if (lab[v] < 0) return {v, val[v]};
auto cpar = root(lab[v]);
return {cpar.ff, val[v] ^ cpar.ss};
}
bool merge(int u, int v, int edge_val) {
auto rootu = root(u);
auto rootv = root(v);
if (rootu.ff != rootv.ff) {
if (-lab[rootu.ff] < -lab[rootv.ff])
swap(u, v), swap(rootu, rootv);
changes.emplace_back(rootu.ff, lab[rootu.ff]);
lab[rootu.ff] += lab[rootv.ff];
changes.emplace_back(rootv.ff, lab[rootv.ff]);
lab[rootv.ff] = rootu.ff;
changes.emplace_back(-rootv.ff, val[rootv.ff]);
val[rootv.ff] = val[rootu.ff] ^ rootu.ss ^ edge_val ^ rootv.ss;
return true;
}
return (rootu.ss ^ rootv.ss ^ edge_val ^ 1);
}
int get_timer() {
return isz(changes);
}
void reverse_to(int timer) {
while (isz(changes) > timer) {
int idx = changes.back().ff;
int oval = changes.back().ss;
changes.pop_back();
if (idx > 0) lab[idx] = oval;
else val[-idx] = oval;
}
}
} dsu;
int n, m, q;
vector<pair<int, int>> edges;
int cntA, cntB, best[mxN];
void solveDP(int l, int r, int optl, int optr) {
if (l > r) return;
int otimer = dsu.get_timer();
int mid = (l + r) >> 1;
for (int i = l; i <= mid; ++i)
if (i) assert(dsu.merge(edges[i].ff, edges[i].ss, 1));
int timer = dsu.get_timer();
int opt = optr;
while (opt >= optl && dsu.merge(edges[opt].ff, edges[opt].ss, 1)) --opt;
best[mid] = opt;
dsu.reverse_to(timer);
solveDP(mid + 1, r, opt, optr);
dsu.reverse_to(otimer);
for (int i = optr; i > opt; --i)
dsu.merge(edges[i].ff, edges[i].ss, 1);
solveDP(l, mid - 1, optl, opt);
dsu.reverse_to(otimer);
}
void solve() {
cin >> n >> m >> q;
edges.emplace_back(0, 0);
for (int i = 1; i <= m; ++i) {
int u, v; cin >> u >> v;
edges.emplace_back(u, v);
}
dsu.init(n);
int ptr = 1;
while (ptr <= m && dsu.merge(edges[ptr].ff, edges[ptr].ss, 1)) ++ptr;
for (int i = ptr; i <= m; ++i) best[i] = m + 1;
dsu.init(n);
solveDP(0, ptr - 1, 1, m);
for (int i = 1; i <= q; ++i) {
int l, r; cin >> l >> r;
cout << (best[l - 1] > r ? "YES" : "NO") << "\n";
}
}
signed main() {
#ifndef CDuongg
if(fopen(taskname".inp", "r"))
assert(freopen(taskname".inp", "r", stdin)), assert(freopen(taskname".out", "w", stdout));
#else
freopen("bai3.inp", "r", stdin);
freopen("bai3.out", "w", stdout);
auto start = chrono::high_resolution_clock::now();
#endif
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1; //cin >> t;
while(t--) solve();
#ifdef CDuongg
auto end = chrono::high_resolution_clock::now();
// cout << "\n"; for(int i = 1; i <= 100; ++i) cout << '=';
// cout << "\nExecution time: " << chrono::duration_cast<chrono::milliseconds> (end - start).count() << "[ms]" << endl;
#endif
}
# | 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... |