이 제출은 이전 버전의 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;
struct query {
int l, r;
int index;
query() {
l = r = 0;
index = -1;
}
};
vector <pii> graph[N], edges;
bool ans[N], odd_cycle = 0;
int color[N], used[N];
int link[N], saizu[N];
int cnt, L, R;
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) || (L <= to.second && to.second <= R)) 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 n, m, q;
cin >> n >> m >> q;
vector <query> queries(q+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;
});
int j;
for (int i = q; i; i--) {
if (i == q || queries[i].l != queries[i+1].l) {
j = m;
odd_cycle = 0;
cnt = 0;
for (int l = 1; l <= n; l++) {
link[l] = l;
saizu[l] = 1;
used[l] = 0;
color[l] = -1;
}
}
L = queries[i].l; R = queries[i].r;
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
*/
컴파일 시 표준 에러 (stderr) 메시지
Joker.cpp: In function 'void solve()':
Joker.cpp:96:9: warning: 'j' may be used uninitialized in this function [-Wmaybe-uninitialized]
96 | int j;
| ^
# | 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... |