이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
#define pb push_back
#define mp make_pair
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define lb lower_bound
#define ub upper_bound
#define sz(v) int((v).size())
#define do_not_disturb ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'
const int N = 2e5+7;
vector <int> graph[N];
vector <pii> edges;
struct query {
int l, r;
int index;
query() {
l = r = 0;
index = -1;
}
};
bool ans[N];
int color[N];
int used[N];
bool odd_cycle = 0;
int link[N], saizu[N];
int cnt, j;
map <pii, int> edges_index;
int find(int n) {
if (link[n] == n) return n;
return link[n] = find(link[n]);
}
void dfs(int v, int c);
void unite(int a, int b) {
if (find(a) == find(b)) {
if (color[a] == color[b]) odd_cycle = 1;
return;
}
if (saizu[find(a)] < saizu[find(b)]) swap(a, b);
saizu[find(a)] += saizu[find(b)];
link[find(b)] = find(a);
if (color[a] == -1) {
dfs(a, 0);
}
else {
used[a] = cnt*2;
dfs(b, color[a]^1);
}
}
void dfs(int v, int c) {
used[v] = cnt*2-1;
color[v] = c;
for (auto to : graph[v]) {
if (edges_index[mp(min(v, to), max(v, to))] < j) continue;
if (used[to] == cnt*2 || used[to] == cnt*2-1) {
if (used[to] == cnt*2-1 && color[v] == color[to]) {
odd_cycle = 1;
}
continue;
}
dfs(to, c^1);
}
used[v] = cnt*2;
}
void solve() {
int n, m, q;
cin >> n >> m >> q;
vector <query> queries(q+1);
for (int i = 1; i <= n; i++) {
color[i] = -1;
link[i] = i;
saizu[i] = 1;
}
edges.pb(mp(0, 0));
for (int i = 1; i <= m; i++) {
int a, b;
cin >> a >> b;
if (a > b) swap(a, b);
edges.pb(mp(a, b));
edges_index[mp(a, b)] = i;
graph[a].pb(b);
graph[b].pb(a);
}
for (int i = 1; i <= q; i++) {
cin >> queries[i].l >> queries[i].r;
queries[i].index = i;
}
sort(all(queries), [&](query a, query b) {
if (a.l == b.l) return a.r < b.r;
return a.l < b.l;
});
j = m;
while (j > queries.back().r) {
cnt++;
unite(edges[j].first, edges[j].second);
j--;
}
for (int i = q; i; i--) {
ans[queries[i].index] = odd_cycle;
if (i == q || queries[i].r != queries[i+1].r) {
cnt++;
unite(edges[j].first, edges[j].second);
j--;
}
while (j > 0 && i > 1 && j > queries[i-1].r) {
cnt++;
unite(edges[j].first, edges[j].second);
j--;
}
}
for (int i = 1; i <= q; i++) {
cout << (ans[i] ? "YES" : "NO") << endl;
}
}
int main() {
//~ do_not_disturb
int t = 1;
//~ cin >> t;
while (t--) {
solve();
}
return 0;
}
/*
6 8 8
1 3
1 5
1 6
2 5
2 6
3 4
3 5
5 6
1 8
1 7
1 6
1 5
1 4
1 3
1 2
1 1
*/
# | 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... |