이 제출은 이전 버전의 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 <pii> graph[N], 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, n;
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 (saizu[find(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 (find(v) != find(to.first) || to.second <= j) continue;
if (used[to.first] < cnt*2-1) {
dfs(to.first, c^1);
}
else if (color[to.first] == color[v]) {
odd_cycle = 1;
}
}
used[v] = cnt*2;
}
void solve() {
int m, q;
cin >> n >> m >> q;
vector <query> queries(q+1);
for (int i = 1; i <= n; i++) {
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));
graph[a].pb(mp(b, i));
graph[b].pb(mp(a, i));
}
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;
for (int i = q; i; i--) {
while (j && j > queries[i].r) {
cnt++;
unite(edges[j].first, edges[j].second);
j--;
}
ans[queries[i].index] = odd_cycle;
}
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 3
1 7
1 5
1 1
1 6
1 4
1 2
1 8
*/
# | 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... |