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 pb push_back
#define fi first
#define se second
#define Mup(a,x) a=max(a, x)
#define mup(a,x) a=min(a, x)
#define all(v) v.begin(),v.end()
#define lb lower_bound
#define INF (1ll<<60)
#define MOD 1'000'000'007
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
int N, M, Q;
int U[202020], V[202020];
int P[404040], R[202020];
vector<pii> rb;
int rt(int u){
if (P[u] < 0) return u;
return rt(P[u]);
}
void uni(int u, int v){
u = rt(u), v = rt(v);
if (u == v) return;
if (P[u] > P[v]) swap(u, v);
rb.pb(pii(u, P[u]));
rb.pb(pii(v, P[v]));
P[u] += P[v], P[v] = u;
}
void undo(){
pii t = rb.back();
rb.pop_back();
P[t.fi] = t.se;
}
void dnc(int s, int e, int r){
if (s > e) return;
int m=s+e>>1, rs=rb.size();
for (int i=max(s,1); i<=m; i++){
uni(U[i], V[i]+N);
uni(U[i]+N, V[i]);
if (rt(U[i]) == rt(U[i]+N)){
for (int j=m; j<=e; j++) R[j] = r;
while (rb.size()>rs) undo();
dnc(s, m-1, r);
return;
}
}
int rr=rb.size();
for (int i=r-1; i>s; i--){
uni(U[i], V[i]+N);
uni(U[i]+N, V[i]);
if (rt(U[i]) == rt(U[i]+N)){R[m] = i; break;}
}
while (rb.size()>rr) undo();
dnc(m+1, e, r);
while (rb.size()>rs) undo();
for (int i=r-1; i>=max(R[m],s); i--){
uni(U[i], V[i]+N);
uni(U[i]+N, V[i]);
if (rt(U[i]) == rt(U[i]+N)){
for (int j=s; j<m; j++) R[j] = R[m];
return;
}
}
dnc(s, m-1, R[m]);
}
int main(){
scanf("%d %d %d", &N, &M, &Q);
for (int i=1; i<=M; i++) scanf("%d %d", &U[i], &V[i]);
for (int i=1; i<=2*N; i++) P[i] = -1;
dnc(0, M-1, M+1);
for (int i=0; i<M; i++) printf("## %d : %d\n", i, R[i]);
while (Q--){
int l, r;
scanf("%d %d", &l, &r);
puts(r<R[l-1]?"YES":"NO");
}
return 0;
}
Compilation message (stderr)
Joker.cpp: In function 'void dnc(int, int, int)':
Joker.cpp:42:9: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
42 | int m=s+e>>1, rs=rb.size();
| ~^~
Joker.cpp:48:20: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
48 | while (rb.size()>rs) undo();
| ~~~~~~~~~^~~
Joker.cpp:59:18: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
59 | while (rb.size()>rr) undo();
| ~~~~~~~~~^~~
Joker.cpp:61:18: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
61 | while (rb.size()>rs) undo();
| ~~~~~~~~~^~~
Joker.cpp: In function 'int main()':
Joker.cpp:74:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
74 | scanf("%d %d %d", &N, &M, &Q);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Joker.cpp:75:32: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
75 | for (int i=1; i<=M; i++) scanf("%d %d", &U[i], &V[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~
Joker.cpp:81:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
81 | scanf("%d %d", &l, &r);
| ~~~~~^~~~~~~~~~~~~~~~~
# | 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... |