#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fi first
#define se second
#define pb push_back
#define vi vector<int>
#define pi pair<int, int>
#define all(x) (x).begin(),(x).end()
const int maxn=2e5+10;
vector<pi> edge(maxn);
vi head(maxn);
vi col(maxn,1);
vi depth(maxn,1);
vector<vi> changes;
bool is_bipartite=1;
pi get(int a) {
if (a==head[a]) {
return {a,col[a]};
}
else {
pi temp=get(head[a]);
temp.se^=col[a];
return temp;
}
}
void unite(int a, int b) {
int cola,colb;
tie(a,cola)=get(a);
tie(b,colb)=get(b);
if (a==b) {
if (cola==colb && is_bipartite) {
is_bipartite=0;
}
}
if (depth[a]<depth[b]) {
swap(a,b);
}
changes.pb({b,head[b],col[b],a,int(depth[a]==depth[b])});
head[b]=a;
col[b]=cola^colb^1;
depth[a]+=int(depth[a]==depth[b]);
}
void rollback(int k) {
while (changes.size()>k) {
vi t=changes.back();
changes.pop_back();
head[t[0]]=t[1];
col[t[0]]=t[2];
depth[t[3]]-=t[4];
}
}
int n,m,q;
vector<bool> ans(maxn);
vector<vector<vi>> queries(maxn);
vi mn(4*maxn,1e9),mx(4*maxn,-1e9);
void add(int l, int r, int i, int v=1, int tl=0, int tr=n-1) {
mn[v]=min(mn[v],l);
mx[v]=max(mx[v],r);
if (tl==tr) {
queries[tl].pb({l,r,i});
}
else {
int tm=tl+(tr-tl)/2;
if (l<=tm) {
add(l,r,i,v*2,tl,tm);
}
else {
add(l,r,i,v*2+1,tm+1,tr);
}
}
}
void dfs(int l_added=0, int r_added=m-1, int v=1, int tl=0, int tr=n-1) {
if (mn[v]==1e9) {
return;
}
bool temp_is_bip=is_bipartite;
int temp_sze=changes.size();
while (l_added<mn[v]) {
unite(edge[l_added].fi,edge[l_added].se);
l_added++;
}
while (r_added>mx[v]) {
unite(edge[r_added].fi,edge[r_added].se);
r_added--;
}
if (tl==tr) {
sort(all(queries[tl]),[](vi a, vi b){return a[1]>b[1];});
for (auto temp:queries[tl]) {
while (r_added>temp[1]) {
unite(edge[r_added].fi,edge[r_added].se);
r_added--;
}
ans[temp[2]]=is_bipartite;
}
}
else {
int tm=tl+(tr-tl)/2;
dfs(l_added,r_added,v*2,tl,tm);
dfs(l_added,r_added,v*2+1,tm+1,tr);
}
is_bipartite=temp_is_bip;
rollback(temp_sze);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m >> q;
iota(all(head),0);
for (int i=0; i<m; i++) {
cin >> edge[i].fi >> edge[i].se;
edge[i].fi--;
edge[i].se--;
}
int l,r;
for (int i=0; i<q; i++) {
cin >> l >> r;
add(l-1,r-1,i);
}
dfs();
for (int i=0; i<q; i++) {
cout << (ans[i]?"NO\n":"YES\n");
}
return 0;
}
Compilation message
Joker.cpp: In function 'void rollback(int)':
Joker.cpp:50:26: warning: comparison of integer expressions of different signedness: 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
50 | while (changes.size()>k) {
| ~~~~~~~~~~~~~~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
15196 KB |
Output is correct |
2 |
Correct |
6 ms |
15388 KB |
Output is correct |
3 |
Correct |
7 ms |
15196 KB |
Output is correct |
4 |
Correct |
7 ms |
15380 KB |
Output is correct |
5 |
Correct |
5 ms |
15380 KB |
Output is correct |
6 |
Incorrect |
6 ms |
15380 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
15196 KB |
Output is correct |
2 |
Correct |
6 ms |
15388 KB |
Output is correct |
3 |
Correct |
7 ms |
15196 KB |
Output is correct |
4 |
Correct |
7 ms |
15380 KB |
Output is correct |
5 |
Correct |
5 ms |
15380 KB |
Output is correct |
6 |
Incorrect |
6 ms |
15380 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
15196 KB |
Output is correct |
2 |
Correct |
6 ms |
15388 KB |
Output is correct |
3 |
Correct |
233 ms |
42856 KB |
Output is correct |
4 |
Correct |
212 ms |
42228 KB |
Output is correct |
5 |
Correct |
208 ms |
42896 KB |
Output is correct |
6 |
Correct |
211 ms |
42744 KB |
Output is correct |
7 |
Correct |
253 ms |
42740 KB |
Output is correct |
8 |
Incorrect |
235 ms |
42532 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
15196 KB |
Output is correct |
2 |
Correct |
6 ms |
15388 KB |
Output is correct |
3 |
Correct |
7 ms |
15196 KB |
Output is correct |
4 |
Correct |
7 ms |
15380 KB |
Output is correct |
5 |
Correct |
5 ms |
15380 KB |
Output is correct |
6 |
Incorrect |
6 ms |
15380 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
15196 KB |
Output is correct |
2 |
Correct |
6 ms |
15388 KB |
Output is correct |
3 |
Correct |
7 ms |
15196 KB |
Output is correct |
4 |
Correct |
7 ms |
15380 KB |
Output is correct |
5 |
Correct |
5 ms |
15380 KB |
Output is correct |
6 |
Incorrect |
6 ms |
15380 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
15196 KB |
Output is correct |
2 |
Correct |
6 ms |
15388 KB |
Output is correct |
3 |
Correct |
7 ms |
15196 KB |
Output is correct |
4 |
Correct |
7 ms |
15380 KB |
Output is correct |
5 |
Correct |
5 ms |
15380 KB |
Output is correct |
6 |
Incorrect |
6 ms |
15380 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |