#include<bits/stdc++.h>
#define pii pair<int, int>
using namespace std;
int n, m, q;
vector<int>f, sz, same, dif;
void init() {
for (int i = 0 ; i < n ; i++) {
f[i] = i;
same[i] = i;
dif[i] = -1;
sz[i] = 1;
}
}
int find(int x) {
//if (f[x][j] == -1) return -1;
if (x == f[x]) return x;
else return f[x] = find(f[x]);
}
int finds(int x) {
if (x == -1) return -1;
if (x == same[x]) return x;
else return same[x] = find(same[x]);
}
bool add(int a, int b) {
int fa = find(a), fb = find(b);
int sa = finds(a), sb = finds(b);
//cout << fa << " " << sa << " " << fb << " " << sb << "\n";
if (fa == fb) {
if (sa == sb) return true;
else return false;
}
//if (sz[fa] > sz[fb]) swap(fa, fb); swap(sa, sb);
f[fa] = fb;
sz[fb] += sz[fa];
dif[fa] = finds(dif[fa]); dif[fb] = finds(dif[fb]);
if ((fa == sa && fb == sb) || (fa != sa && fb != sb)) {
if (dif[fa] != -1) same[dif[fa]] = fb;
if (dif[fb] != -1) same[fa] = dif[fb];
else dif[fb] = fa;
} else {
same[fa] = fb;
if (dif[fa] != -1 && dif[fb] != -1) same[dif[fa]] = dif[fb];
else if (dif[fa] != -1) dif[fb] = fa;
}
return false;
}
signed main() {
//ios::sync_with_stdio(false);
//cin.tie(0);
cin >> n >> m >> q;
f.resize(n);
sz.resize(n);
same.resize(n);
dif.resize(n);
vector<pii>e;
int a, b;
for (int i = 0 ; i < m ; i++) {
cin >> a >> b; a--, b--;
e.push_back({a, b});
}
int l, r;
for (int i = 0 ; i < q ; i++) {
init();
bool can = 0;
cin >> l>> r; l--, r--;
for (int j = 0 ; j < l && !can ; j++) {
//cout << e[j].first << " " << e[j].second << "\n";
can = add(e[j].first, e[j].second);
//cout << j << " ";
}
for (int j = r+1 ; j < m && !can ; j++) {
can = add(e[j].first, e[j].second);
//cout << j << " ";
}
if (can) cout <<"YES\n";
else cout << "NO\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Execution timed out |
2075 ms |
6592 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |