#include <bits/stdc++.h>
using namespace std;
#define int long long
#ifdef LOCAL
void debug_out() {cerr<<endl;}
template <typename Head, typename... Tail>
void debug_out(Head _H, Tail... _T) {cerr<<" "<<to_string(_H);debug_out(_T...);}
#define debug(...) cerr<<"["<<#__VA_ARGS__<<"]:",debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif
const int MAXN = 200005;
const int inf=1000000500ll;
const long long oo =1000000000000000500ll;
const int MOD = (int)1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef pair<int,int> pi;
int n,m,q;
pi A[MAXN];
struct ufds_{
int p[MAXN],dist[MAXN];
vector<array<int,4>>rollback;
int TIME=0;
bool nonbi=0;
ufds_(){
iota(p,p+MAXN,0);
}
pi find(int x){
if(x==p[x])return {x,dist[x]};
else{
rollback.push_back({TIME,x,p[x],dist[x]});
int cur=dist[x];
pi np=find(p[x]);
dist[x]^=np.second;
p[x]=np.first;
return {p[x],dist[x]};
}
}
void merge(int x, int y){
++TIME;
debug(TIME);
int distx=dist[x];
int disty=dist[y];
if(find(x) == find(y)){
debug(dist[x],p[x],dist[y],p[y]);
if((dist[x] ^ dist[y]) == 0){
rollback.push_back({TIME,-1,nonbi,nonbi});
nonbi=1;
} else {
}
return;
}
x=find(x).first;
rollback.push_back({TIME,x,p[x],dist[x]});
dist[x]=distx^disty^1;
p[x]=find(y).first;
}
void rb(int dt){
int tt=TIME-dt;
while(rollback.size() && rollback.back()[0]>tt){
array<int,4>cur=rollback.back();rollback.pop_back();
if(cur[1] == -1){
nonbi=cur[2];
} else{
p[cur[1]]=cur[2];
dist[cur[1]]=cur[3];
}
}
TIME-=dt;
}
}ufds;
int ans[MAXN];
void dnc(int s, int e, int l, int r){
if(s>e)return;
int m=(s+e)/2;
for(int i=s;i<=m;i++){
ufds.merge(A[i].first,A[i].second);
}
if(m==6){
for(int i=1;i<=n;i++){
debug(i,ufds.p[i],ufds.dist[i]);
}
//exit(0);
}
for(int i=r;i>=l;i--){
ufds.merge(A[i].first,A[i].second);
if(ufds.nonbi){
ans[m]=i;
break;
}
}
//if(m==6)exit(0);
ufds.rb(r-ans[m]+1);
if(s!=e){
dnc(m+1,e,ans[m],r);
}
ufds.rb(m-s+1);
if(s!=e){
for(int i=r;i>=ans[m];i--){
ufds.merge(A[i].first,A[i].second);
}
dnc(s,m-1,l,ans[m]);
ufds.rb(r-ans[m]+1);
}
}
int32_t main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>n>>m>>q;
//check if whole graph is bipartite
for(int i=1;i<=m;i++)cin>>A[i].first>>A[i].second;
dnc(1,m,1,m);
for(int i=1;i<=m;i++){
debug(i,ans[i]);
}
while(q--){
int l,r;cin>>l>>r;
if(ans[l-1]>r)cout<<"YES\n";
else cout<<"NO\n";
}
}
Compilation message
Joker.cpp: In member function 'pi ufds_::find(long long int)':
Joker.cpp:33:8: warning: unused variable 'cur' [-Wunused-variable]
33 | int cur=dist[x];
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6492 KB |
Output is correct |
2 |
Correct |
2 ms |
6592 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Incorrect |
2 ms |
6492 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6492 KB |
Output is correct |
2 |
Correct |
2 ms |
6592 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Incorrect |
2 ms |
6492 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6492 KB |
Output is correct |
2 |
Correct |
2 ms |
6592 KB |
Output is correct |
3 |
Execution timed out |
2037 ms |
43188 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6492 KB |
Output is correct |
2 |
Correct |
2 ms |
6592 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Incorrect |
2 ms |
6492 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6492 KB |
Output is correct |
2 |
Correct |
2 ms |
6592 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Incorrect |
2 ms |
6492 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6492 KB |
Output is correct |
2 |
Correct |
2 ms |
6592 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Incorrect |
2 ms |
6492 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |