// ~~ icebear ~~
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
template<class T>
bool minimize(T &a, const T &b) {
if (a > b) return a = b, true;
return false;
}
template<class T>
bool maximize(T &a, const T &b) {
if (a < b) return a = b, true;
return false;
}
#define FOR(i,a,b) for(int i=(a); i<=(b); ++i)
#define FORR(i,a,b) for(int i=(a); i>=(b); --i)
#define REP(i, n) for(int i=0; i<(n); ++i)
#define RED(i, n) for(int i=(n)-1; i>=0; --i)
#define MASK(i) (1LL << (i))
#define BIT(S, i) (((S) >> (i)) & 1)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define task "gen"
const int MOD = 1e9 + 7;
const int inf = 1e9 + 27092008;
const ll INF = 1e18 + 27092008;
const int N = 2e5 + 5;
int numNode, numEdge, numQuery, ans[N];
ii edge[N];
struct DisjointSet {
vector<int> lab;
bool biparite;
vector<array<int, 4>> history;
DisjointSet(int n = 0): lab(n + 5, -1), biparite(true) {}
int root(int v) {
return (lab[v] < 0 ? v : root(lab[v]));
}
bool unite(int u, int v) {
u = root(u);
v = root(v);
if (u == v) return false;
if (lab[u] > lab[v]) swap(u, v);
history.push_back({u, lab[u], v, lab[v]});
lab[u] += lab[v];
lab[v] = u;
return true;
}
void add_edge(int u, int v) {
unite(u, v + numNode);
unite(v, u + numNode);
if (root(u) == root(u + numNode)) biparite = false;
if (root(v) == root(v + numNode)) biparite = false;
}
void roll_back(ii snapshot) {
while((int)history.size() > snapshot.fi) {
auto H = history.back(); history.pop_back();
lab[H[0]] = H[1];
lab[H[2]] = H[3];
}
biparite = snapshot.se;
}
ii snap() {
return mp((int)history.size(), biparite);
}
} dsu;
void DnC(int queryL, int queryR, int ansL, int ansR) {
if (queryL > queryR) return;
int queryM = (queryL + queryR) >> 1, ansM = ansL;
ii snapshot = dsu.snap();
FOR(i, queryL, queryM - 1) dsu.add_edge(edge[i].fi, edge[i].se);
if (dsu.biparite == false) {
FOR(i, queryM, queryR) ans[i] = numEdge + 1;
dsu.roll_back(snapshot);
DnC(queryL, queryM - 1, ansL, ansR);
return;
}
FORR(i, ansR, max(queryM, ansL)) {
dsu.add_edge(edge[i].fi, edge[i].se);
if (dsu.biparite == false) {
ansM = i;
break;
}
}
ans[queryM] = ansM;
dsu.roll_back(snapshot);
FOR(i, queryL, queryM) dsu.add_edge(edge[i].fi, edge[i].se);
DnC(queryM + 1, queryR, ansM, ansR);
dsu.roll_back(snapshot);
FORR(i, ansR, ansM + 1) dsu.add_edge(edge[i].fi, edge[i].se);
DnC(queryL, queryM - 1, ansL, ansM);
}
void init(void) {
cin >> numNode >> numEdge >> numQuery;
FOR(i, 1, numEdge) cin >> edge[i].fi >> edge[i].se;
}
void process(void) {
dsu = DisjointSet(2 * numNode);
DnC(1, numEdge, 1, numEdge);
while(numQuery--) {
int l, r;
cin >> l >> r;
cout << (ans[l] > r ? "YES\n" : "NO\n");
cerr << (ans[l] > r ? "YES\n" : "NO\n");
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
int tc = 1;
// cin >> tc;
while(tc--) {
init();
process();
}
return 0;
}
Compilation message (stderr)
Joker.cpp: In function 'int main()':
Joker.cpp:135:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
135 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Joker.cpp:136:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
136 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~| # | 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... |