#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
struct disj{
int pa[707], r[707];
void init(int n){
memset(r,0,sizeof(r));
for(int i=0; i<=n; i++) pa[i] = i;
}
int find(int x){
if(pa[x] == x) return x;
else return pa[x] = find(pa[x]);
}
void uni(int p, int q){
p = find(p);
q = find(q);
if(r[p] > r[q]) pa[q] = p;
else pa[p] = q;
if(r[p] == r[q]) r[p]++;
}
}disj;
int n,m;
int u[125000], v[125000];
vector<int> inc, dec;
int main(){
scanf("%d %d",&n,&m);
for (int i=0; i<m; i++) {
scanf("%d %d",&u[i],&v[i]);
}
disj.init(n);
for (int i=0; i<m; i++) {
if(disj.find(u[i]) != disj.find(v[i])){
disj.uni(u[i],v[i]);
inc.push_back(i+1);
}
}
disj.init(n);
for (int i=m-1; i>=0; i--) {
if(disj.find(u[i]) != disj.find(v[i])){
disj.uni(u[i],v[i]);
dec.push_back(i+1);
}
}
int q;
scanf("%d",&q);
while (q--) {
int x,y;
scanf("%d %d",&x,&y);
disj.init(n);
for (int i=0; i<inc.size(); i++) {
if(inc[i] <= x-1){
disj.uni(u[inc[i]-1],v[inc[i]-1]);
}
else break;
}
for (int i=0; i<dec.size(); i++) {
if(dec[i] >= y+1){
disj.uni(u[dec[i]-1],v[dec[i]-1]);
}
else break;
}
int cnt = 0;
for (int i=1; i<=n; i++) {
if(disj.find(i) == i) cnt++;
}
printf("%d\n",cnt);
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2192 KB |
Output is correct |
2 |
Correct |
0 ms |
2192 KB |
Output is correct |
3 |
Correct |
0 ms |
2192 KB |
Output is correct |
4 |
Correct |
0 ms |
2192 KB |
Output is correct |
5 |
Correct |
0 ms |
2192 KB |
Output is correct |
6 |
Correct |
0 ms |
2192 KB |
Output is correct |
7 |
Correct |
0 ms |
2192 KB |
Output is correct |
8 |
Correct |
0 ms |
2192 KB |
Output is correct |
9 |
Correct |
0 ms |
2192 KB |
Output is correct |
10 |
Correct |
0 ms |
2192 KB |
Output is correct |
11 |
Correct |
0 ms |
2192 KB |
Output is correct |
12 |
Correct |
0 ms |
2192 KB |
Output is correct |
13 |
Correct |
0 ms |
2192 KB |
Output is correct |
14 |
Correct |
0 ms |
2192 KB |
Output is correct |
15 |
Correct |
0 ms |
2192 KB |
Output is correct |
16 |
Correct |
0 ms |
2192 KB |
Output is correct |
17 |
Correct |
0 ms |
2192 KB |
Output is correct |
18 |
Correct |
0 ms |
2192 KB |
Output is correct |
19 |
Correct |
0 ms |
2192 KB |
Output is correct |
20 |
Correct |
0 ms |
2192 KB |
Output is correct |
21 |
Correct |
0 ms |
2192 KB |
Output is correct |
22 |
Correct |
0 ms |
2192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
2192 KB |
Output is correct |
2 |
Correct |
44 ms |
2192 KB |
Output is correct |
3 |
Correct |
48 ms |
2192 KB |
Output is correct |
4 |
Correct |
56 ms |
2192 KB |
Output is correct |
5 |
Correct |
44 ms |
2192 KB |
Output is correct |
6 |
Correct |
448 ms |
2192 KB |
Output is correct |
7 |
Correct |
436 ms |
2192 KB |
Output is correct |
8 |
Correct |
424 ms |
2192 KB |
Output is correct |
9 |
Correct |
36 ms |
2192 KB |
Output is correct |
10 |
Correct |
40 ms |
2192 KB |
Output is correct |
11 |
Correct |
40 ms |
2192 KB |
Output is correct |
12 |
Correct |
56 ms |
2192 KB |
Output is correct |
13 |
Correct |
48 ms |
2192 KB |
Output is correct |
14 |
Correct |
52 ms |
2192 KB |
Output is correct |
15 |
Correct |
32 ms |
2192 KB |
Output is correct |
16 |
Correct |
36 ms |
2192 KB |
Output is correct |
17 |
Correct |
16 ms |
2192 KB |
Output is correct |
18 |
Correct |
16 ms |
2192 KB |
Output is correct |
19 |
Correct |
8 ms |
2192 KB |
Output is correct |
20 |
Correct |
8 ms |
2192 KB |
Output is correct |
21 |
Correct |
4 ms |
2192 KB |
Output is correct |
22 |
Correct |
0 ms |
2192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1580 ms |
2192 KB |
Output is correct |
2 |
Correct |
1568 ms |
2192 KB |
Output is correct |
3 |
Correct |
1548 ms |
2192 KB |
Output is correct |
4 |
Correct |
1560 ms |
2192 KB |
Output is correct |
5 |
Correct |
1584 ms |
2192 KB |
Output is correct |
6 |
Correct |
1524 ms |
2192 KB |
Output is correct |
7 |
Correct |
1512 ms |
2192 KB |
Output is correct |
8 |
Correct |
1664 ms |
2192 KB |
Output is correct |
9 |
Correct |
1424 ms |
2192 KB |
Output is correct |
10 |
Correct |
1432 ms |
2192 KB |
Output is correct |
11 |
Correct |
1536 ms |
2192 KB |
Output is correct |
12 |
Correct |
1552 ms |
2192 KB |
Output is correct |
13 |
Correct |
1484 ms |
2192 KB |
Output is correct |
14 |
Correct |
1480 ms |
2192 KB |
Output is correct |
15 |
Correct |
1304 ms |
2192 KB |
Output is correct |
16 |
Correct |
1216 ms |
2192 KB |
Output is correct |
17 |
Correct |
792 ms |
2192 KB |
Output is correct |
18 |
Correct |
756 ms |
2192 KB |
Output is correct |
19 |
Correct |
280 ms |
2192 KB |
Output is correct |
20 |
Correct |
364 ms |
2192 KB |
Output is correct |
21 |
Correct |
92 ms |
2192 KB |
Output is correct |
22 |
Correct |
88 ms |
2192 KB |
Output is correct |