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>
#define f first
#define s second
#define int long long
#define pii pair<int,int>
using namespace std;
const int N = 2e5 + 5, mod = 1e9 + 7; // !
int t, p[N], up[N], sz[N], ans[N], odd = 0;
vector<int> V[N];
pair<int,int> e[N];
stack<int> st;
pair<int,int> find(int u) {
if(p[u] == u) return {u, 0};
pii P = find(p[u]);
P.s ^= up[u];
return P;
}
void merge(int u, int v) {
pii U = find(u), V = find(v);
if(sz[U.f] < sz[V.f]) swap(U, V);
if(U.f != V.f) {
st.push(V.f);
p[V.f] = U.f;
sz[U.f] += sz[V.f];
up[V.f] = (U.s != V.s ? 0 : 1);
return;
}
if(U.s == V.s) ++odd, st.push(-1);
}
void rollback(int X) {
while(st.size() > X) {
int x = st.top();
st.pop();
if(x == -1) --odd;
else sz[p[x]] -= sz[x], p[x] = x, up[x] = 0;
}
}
void solve(int l, int r, int L, int R) {
int mid = (l + r) / 2;
int I = st.size();
for(int i = l; i <= mid; i++) {
merge(e[i].f, e[i].s);
}
int I2 = st.size();
int opt = R;
while(opt && !odd) {
--opt;
if(!opt) break;
merge(e[opt].f, e[opt].s);
}
ans[mid] = opt;
assert(opt >= L);
rollback(I2);
if(mid + 1 <= r) solve(mid + 1, r, opt, R);
rollback(I);
for(int i = max(1ll, opt); i < R; i++) merge(e[opt].f, e[opt].s);
if(l < mid) solve(l, mid - 1, L, opt);
rollback(I);
}
main(){
int n, m, q;
cin >> n >> m >> q;
for(int i = 1; i <= n; i++) p[i] = i, sz[i] = 1;
for(int i = 1; i <= m; i++) cin >> e[i].f >> e[i].s;
e[0] = {0, 1};
solve(0, m, 0, m + 1);
// for(int i = 1; i <= m; i++) cout << ans[i] << " "; cout << endl;
for(int i = 1; i <= q; i++) {
int l, r;
cin >> l >> r;
cout << (ans[l - 1] > r ? "YES\n" : "NO\n");
}
}
Compilation message (stderr)
Joker.cpp: In function 'void rollback(long long int)':
Joker.cpp:31:21: warning: comparison of integer expressions of different signedness: 'std::stack<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
31 | while(st.size() > X) {
| ~~~~~~~~~~^~~
Joker.cpp: At global scope:
Joker.cpp:63:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
63 | main(){
| ^~~~
# | 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... |