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>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pl;
typedef pair<ll,pl> ppl;
const ll lim=2e5+5;
ll par[lim],rnk[lim],rel[lim];
ll parcpy[lim],rnkcpy[lim],relcpy[lim];
struct xystate{
ll x,parx,relx,rnkx;
ll y,pary,rely,rnky;
xystate(ll ix,ll iparx,ll irelx,ll irnkx,
ll iy,ll ipary,ll irely,ll irnky){
x=ix,parx=iparx,relx=irelx,rnkx=irnkx;
y=iy,pary=ipary,rely=irely,rnky=irnky;
}
};
vector<xystate>sv;
pl findset(ll par[],ll rel[],ll x){
ll cur=0,og=x;
while(x!=par[x]){
if(rel[x])cur=1-cur;
x=par[x];
}
// par[og]=x;
// rel[og]=cur;
return {x,cur};
}
void mergeset(ll par[],ll rnk[],ll rel[],ll x,ll y){
pl px=findset(par,rel,x),py=findset(par,rel,y);
x=px.first,y=py.first;
if(x==y)return;
if(rnk[x]>rnk[y])swap(x,y);
sv.push_back(xystate(x,par[x],rel[x],rnk[x],y,par[y],rel[y],rnk[y]));
if(rnk[x]==rnk[y])rnk[y]++;
par[x]=y;
rnk[y]++;
rel[x]=(px.second==py.second);
return;
}
void rollback(ll until_sz){
while(sv.size()>until_sz){
xystate c=sv.back();
par[c.x]=c.parx;
rnk[c.x]=c.rnkx;
rel[c.x]=c.relx;
par[c.y]=c.pary;
rnk[c.y]=c.rnky;
rel[c.y]=c.rely;
sv.pop_back();
}
}
int main(){
ios_base::sync_with_stdio(0),cin.tie(NULL);
ll n,m,q;
cin>>n>>m>>q;
for(int i=1;i<=n;i++)par[i]=i;
vector<pl>v;
vector<pl>qs[lim];
for(int i=0;i<m;i++){
ll a,b;
cin>>a>>b;
v.emplace_back(a,b);
}
//v.emplace_back(0,0);
bool ans[lim],cfa=0;
fill(ans,ans+lim,0);
for(int i=0;i<q;i++){
ll a,b;
cin>>a>>b;
b--,b++;//b is the minimum index that can be used on the right side
qs[b].emplace_back(a,i);
}
for(int i=m;i>=m;i--){
sort(qs[i].begin(),qs[i].end());
for(int i=1;i<=n;i++)parcpy[i]=par[i],rnkcpy[i]=rnk[i],relcpy[i]=rel[i];
ll cur=0;
bool oddc=0;
for(auto x:qs[i]){
if(oddc==1){
ans[x.second]=1;
continue;
}
for(;cur+1<x.first;cur++){
pl x=findset(parcpy,relcpy,v[cur].first),y=findset(parcpy,relcpy,v[cur].second);
if(x.first==y.first){
if(x.second==y.second){
oddc=1;
}
}
else mergeset(parcpy,rnkcpy,relcpy,v[cur].first,v[cur].second);
}
ans[x.second]=oddc;
}
}
for(int i=m-1;i>=1;i--){
if(cfa==1){
for(auto x:qs[i])ans[x.second]=1;
continue;
}
pl x=findset(par,rel,v[i].first),y=findset(par,rel,v[i].second);
if(x.first==y.first){
if(x.second==y.second){
cfa=1;
}
}
else mergeset(par,rnk,rel,v[i].first,v[i].second);
if(qs[i].empty())continue;
if(cfa==1){
for(auto x:qs[i])ans[x.second]=1;
continue;
}
sort(qs[i].begin(),qs[i].end());
//for(int i=1;i<=n;i++)parcpy[i]=par[i],rnkcpy[i]=rnk[i],relcpy[i]=rel[i];
ll cur=0,prevsz=sv.size();
bool oddc=0;
for(auto x:qs[i]){
if(oddc==1){
ans[x.second]=1;
continue;
}
for(;cur+1<x.first;cur++){
pl x=findset(par,rel,v[cur].first),y=findset(par,rel,v[cur].second);
if(x.first==y.first){
if(x.second==y.second){
oddc=1;
}
}
else mergeset(par,rnk,rel,v[cur].first,v[cur].second);
}
ans[x.second]=oddc;
}
rollback(prevsz);
}
for(int i=0;i<q;i++){
if(ans[i])cout<<"YES\n";
else cout<<"NO\n";
}
return 0;
}
Compilation message (stderr)
Joker.cpp: In function 'pl findset(ll*, ll*, ll)':
Joker.cpp:23:11: warning: unused variable 'og' [-Wunused-variable]
23 | ll cur=0,og=x;
| ^~
Joker.cpp: In function 'void rollback(ll)':
Joker.cpp:47:17: warning: comparison of integer expressions of different signedness: 'std::vector<xystate>::size_type' {aka 'long unsigned int'} and 'll' {aka 'long long int'} [-Wsign-compare]
47 | while(sv.size()>until_sz){
| ~~~~~~~~~^~~~~~~~~
# | 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... |