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("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template<class T> bool cmin(T &i, T j) { return i > j ? i=j,true:false; }
template<class T> bool cmax(T &i, T j) { return i < j ? i=j,true:false; }
constexpr int nax = 200200,block_size = 450;
vector<tuple<int,int,int>> queries[block_size]; // r,l,idx
vector<bool> ans;
int edges[nax][2];
int n,m;
struct dsu_save {
int x,y,lzx,lzy,szx,szy,parx,pary;
};
struct DSU {
int lz[nax];
int par[nax];
int sz[nax];
dsu_save stk[nax];
int T = 0;
DSU(int N) {
for (int i = 0; i < N; i++)
lz[i] = 0, par[i] = i, sz[i] = 1;
}
int get(int x,int &cx) {
cx ^= lz[x];
return (x == par[x] ? x : get(par[x],cx));
}
bool need_reset = 0;
bool unite(int x,int y) {
assert(!need_reset);
// cout << "Adding edge: " << x + 1 << " " << y + 1 << endl;
int cx = 0,cy = 0;
int px = get(x,cx), py = get(y,cy);
// cout << px + 1 << " " << py + 1 << " " << cx << " " << cy << endl;
if (sz[px] < sz[py])
swap(px,py);
stk[T++] = {px,py,lz[px],lz[py],sz[px],sz[py],par[px],par[py]};
if (cx == cy) {
if (px == py) {
need_reset = 1;
return 1;
} else {
lz[py] ^= 1;
sz[px] += sz[py];
par[py] = px;
}
} else {
if (px != py) {
sz[px] += sz[py];
par[py] = px;
}
}
return 0;
}
void rollback(int k) {
need_reset = 0;
// do stuff
assert(T >= k);
while(k--) {
auto &c = stk[--T];
lz[c.x] = c.lzx,lz[c.y] = c.lzy,par[c.x] = c.parx,par[c.y] = c.pary,sz[c.x] = c.szx,sz[c.y] = c.szy;
}
}
};
DSU dsu(nax);
bool solve_block(int block) {
int current_r = m - 1;
for (int i = max(0,(block - 1) * block_size); i < min(m,block * block_size); i++) {
// cout << "edge index: " << i + 1 << endl;
if (dsu.unite(edges[i][0],edges[i][1])) {
for (int B = block; B < block_size; B++)
for (auto &[r,l,idx]: queries[B])
ans[idx] = 1;
return 1;
}
}
for (int i = 0; i < queries[block].size(); i++) {
auto &[r,l,idx] = queries[block][i];
while(current_r >= r) {
// cout << "edge index: " << current_r + 1 << endl;
if (dsu.unite(edges[current_r][0],edges[current_r][1])) {
for (int j = i; j < queries[block].size(); j++) {
int idx2 = get<2>(queries[block][j]);
ans[idx2] = 1;
}
dsu.rollback(m - current_r); // potrebbero essere necessari dei +- 1
return 0;
}
current_r--;
}
--l;
int inserted = 0;
while(l >= block * block_size) {
// devo inserirlo
inserted++;
// cout << "edge index: " << l + 1 << endl;
if (dsu.unite(edges[l][0],edges[l][1])) {
ans[idx] = 1;
break;
}
l--;
}
dsu.rollback(inserted);
}
dsu.rollback(m - current_r - 1);
return 0;
}
vector<bool> trova_cicli(int N, int M, int Q, vector<int> A, vector<int> B, vector<int> L, vector<int> R) {
n = N, m = M;
for (int i = 0; i < M; i++)
edges[i][0] = A[i],edges[i][1] = B[i];
ans.resize(Q);
for (int i = 0; i < Q; i++)
queries[L[i] / block_size].emplace_back(R[i],L[i],i);
for (int i = 0; i < block_size; i++)
sort(queries[i].rbegin(),queries[i].rend());
for (int i = 0; i < block_size; i++)
if (solve_block(i))
break;
return ans;
}
int main() {
int N,M,Q;
cin >> N >> M >> Q;
vector<int> A(M),B(M),L(Q),R(Q);
for (int i = 0; i < M; i++) {
cin >> A[i] >> B[i];
--A[i],--B[i];
}
for (int i = 0; i < Q; i++) {
cin >> L[i] >> R[i];
--L[i];
}
auto res = trova_cicli(N,M,Q,A,B,L,R);
for (int i = 0; i < Q; i++)
cout << (res[i] ? "YES" : "NO") << "\n";
}
Compilation message (stderr)
Joker.cpp: In function 'bool solve_block(int)':
Joker.cpp:87:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::tuple<int, int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
87 | for (int i = 0; i < queries[block].size(); i++) {
| ~~^~~~~~~~~~~~~~~~~~~~~~~
Joker.cpp:92:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::tuple<int, int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
92 | for (int j = i; j < queries[block].size(); j++) {
| ~~^~~~~~~~~~~~~~~~~~~~~~~
# | 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... |