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;
#define pi pair<int, int>
#define fi first
#define se second
#define all(v) v.begin(), v.end()
const int N = 2e5 + 5;
using tp = tuple<int, int, int>;
int n, m, q;
pi edge[N];
tp Q[N];
vector<int> ad[N];
namespace sub1 {
bool ok = 0;
int col[N];
void dfs(int u, int c) {
col[u] = c;
for(auto v : ad[u]) {
if(col[v] == col[u]) ok = 1;
if(col[v] == -1) dfs(v, c ^ 1);
}
}
void solve() {
while(q--) {
int l, r; cin >> l >> r;
for(int i = 1; i <= n; ++i) ad[i].clear();
for(int i = 1; i < l; ++i) {
int u, v; tie(u, v) = edge[i];
ad[u].push_back(v);
ad[v].push_back(u);
}
for(int i = r + 1; i <= m; ++i) {
int u, v; tie(u, v) = edge[i];
ad[u].push_back(v);
ad[v].push_back(u);
}
for(int i = 1; i <= n; ++i) col[i] = -1;
ok = 0;
for(int i = 1; i <= n; ++i)
if(col[i] == -1) dfs(i, 0);
cout << (ok ? "YES\n" : "NO\n");
}
}
}
///
namespace sub3 {
int par[N], sz[N], lz[N], ans[N];
vector<int> L;
vector<pi> queries[N];
void make_set() {
for(int i = 1; i <= n; ++i) par[i] = i, sz[i] = 1, lz[i] = 0;
}
int find_set(int v, int &c) {
c ^= lz[v];
if(v == par[v]) return v;
return find_set(par[v], c);
}
void union_set(int a, int b) {
int ca = 0, cb = 0;
a = find_set(a, ca); b = find_set(b, cb);
if(a != b) {
if(sz[a] < sz[b]) swap(a, b);
sz[a] += sz[b];
par[b] = a;
if(ca == cb) lz[b] = 1;
}
}
bool check(int a, int b) {
int ca = 0, cb = 0;
a = find_set(a, ca); b = find_set(b, cb);
if(a == b && ca == cb) return true;
return false;
}
void solve() {
sort(Q + 1, Q + q + 1);
for(int i = 1, cur = 0; i <= q; ++i) {
int l, r, id; tie(l, r, id) = Q[i];
if(l != cur) cur = l, L.push_back(cur);
queries[cur].emplace_back(r, id);
}
for(auto x : L) sort(all(queries[x]), greater<pi>());
for(auto l : L) {
make_set();
bool ok = 0;
for(int i = 1; i < l; ++i) {
int u, v; tie(u, v) = edge[i];
if(check(u, v)) ok = 1;
union_set(u, v);
}
int j = m;
for(int i = m; i > queries[l][0].fi; --i) {
int u, v; tie(u, v) = edge[i];
if(check(u, v)) ok = 1;
union_set(u, v);
j = i;
}
ans[queries[l][0].se] = ok;
for(int i = 1; i < queries[l].size(); ++i) {
int r, id; tie(r, id) = queries[l][i];
if(ok) {
ans[id] = ok;
continue;
}
while(j > r) { --j;
if(check(edge[j].fi, edge[j].se)) ok = 1;
union_set(edge[j].fi, edge[j].se);
}
ans[id] = ok;
}
}
for(int i = 1; i <= q; ++i) cout << (ans[i] ? "YES\n" : "NO\n");
}
}
signed main() {
cin.tie(0)->sync_with_stdio(0);
if(fopen("task.inp", "r")) {
freopen("task.inp", "r", stdin);
freopen("task.out", "w", stdout);
}
cin >> n >> m >> q;
for(int i = 1; i <= m; ++i) {
int u, v; cin >> u >> v;
edge[i] = {u, v};
}
bool chk = 1;
for(int i = 1; i <= q; ++i) {
int l, r; cin >> l >> r;
Q[i] = {l, r, i};
if(l > 200) chk = 0;
}
if(chk) sub3 :: solve();
else if(n <= 200 && m <= 200 && q <= 200) sub1 :: solve();
}
Compilation message (stderr)
Joker.cpp: In function 'void sub3::solve()':
Joker.cpp:123:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
123 | for(int i = 1; i < queries[l].size(); ++i) {
| ~~^~~~~~~~~~~~~~~~~~~
Joker.cpp: In function 'int main()':
Joker.cpp:146:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
146 | freopen("task.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Joker.cpp:147:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
147 | freopen("task.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |