#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]);
}
int uni(int p, int q){
p = find(p);
q = find(q);
if(p == q) return 0;
if(r[p] > r[q]) pa[q] = p;
else pa[p] = q;
if(r[p] == r[q]) r[p]++;
return 1;
}
}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.uni(u[i],v[i])){
inc.push_back(i+1);
}
}
disj.init(n);
for (int i=m-1; i>=0; i--) {
if(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);
int cnt = n;
for (int i=0; i<inc.size(); i++) {
if(inc[i] <= x-1){
cnt -= 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){
cnt -= disj.uni(u[dec[i]-1],v[dec[i]-1]);
}
else break;
}
printf("%d\n",cnt);
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2196 KB |
Output is correct |
2 |
Correct |
0 ms |
2196 KB |
Output is correct |
3 |
Correct |
0 ms |
2196 KB |
Output is correct |
4 |
Correct |
0 ms |
2196 KB |
Output is correct |
5 |
Correct |
0 ms |
2196 KB |
Output is correct |
6 |
Correct |
0 ms |
2196 KB |
Output is correct |
7 |
Correct |
0 ms |
2196 KB |
Output is correct |
8 |
Correct |
0 ms |
2196 KB |
Output is correct |
9 |
Correct |
0 ms |
2196 KB |
Output is correct |
10 |
Correct |
0 ms |
2196 KB |
Output is correct |
11 |
Correct |
0 ms |
2196 KB |
Output is correct |
12 |
Correct |
1 ms |
2196 KB |
Output is correct |
13 |
Correct |
0 ms |
2196 KB |
Output is correct |
14 |
Correct |
0 ms |
2196 KB |
Output is correct |
15 |
Correct |
0 ms |
2196 KB |
Output is correct |
16 |
Correct |
0 ms |
2196 KB |
Output is correct |
17 |
Correct |
0 ms |
2196 KB |
Output is correct |
18 |
Correct |
0 ms |
2196 KB |
Output is correct |
19 |
Correct |
0 ms |
2196 KB |
Output is correct |
20 |
Correct |
0 ms |
2196 KB |
Output is correct |
21 |
Correct |
0 ms |
2196 KB |
Output is correct |
22 |
Correct |
0 ms |
2196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
2196 KB |
Output is correct |
2 |
Correct |
24 ms |
2196 KB |
Output is correct |
3 |
Correct |
27 ms |
2196 KB |
Output is correct |
4 |
Correct |
32 ms |
2196 KB |
Output is correct |
5 |
Correct |
32 ms |
2196 KB |
Output is correct |
6 |
Correct |
212 ms |
2196 KB |
Output is correct |
7 |
Correct |
209 ms |
2196 KB |
Output is correct |
8 |
Correct |
206 ms |
2196 KB |
Output is correct |
9 |
Correct |
30 ms |
2196 KB |
Output is correct |
10 |
Correct |
26 ms |
2196 KB |
Output is correct |
11 |
Correct |
29 ms |
2196 KB |
Output is correct |
12 |
Correct |
28 ms |
2196 KB |
Output is correct |
13 |
Correct |
34 ms |
2196 KB |
Output is correct |
14 |
Correct |
35 ms |
2196 KB |
Output is correct |
15 |
Correct |
26 ms |
2196 KB |
Output is correct |
16 |
Correct |
30 ms |
2196 KB |
Output is correct |
17 |
Correct |
10 ms |
2196 KB |
Output is correct |
18 |
Correct |
7 ms |
2196 KB |
Output is correct |
19 |
Correct |
1 ms |
2196 KB |
Output is correct |
20 |
Correct |
3 ms |
2196 KB |
Output is correct |
21 |
Correct |
1 ms |
2196 KB |
Output is correct |
22 |
Correct |
0 ms |
2196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
794 ms |
2196 KB |
Output is correct |
2 |
Correct |
775 ms |
2196 KB |
Output is correct |
3 |
Correct |
749 ms |
2196 KB |
Output is correct |
4 |
Correct |
792 ms |
2196 KB |
Output is correct |
5 |
Correct |
813 ms |
2196 KB |
Output is correct |
6 |
Correct |
712 ms |
2196 KB |
Output is correct |
7 |
Correct |
750 ms |
2196 KB |
Output is correct |
8 |
Correct |
797 ms |
2196 KB |
Output is correct |
9 |
Correct |
1049 ms |
2196 KB |
Output is correct |
10 |
Correct |
1057 ms |
2196 KB |
Output is correct |
11 |
Correct |
761 ms |
2196 KB |
Output is correct |
12 |
Correct |
723 ms |
2196 KB |
Output is correct |
13 |
Correct |
738 ms |
2196 KB |
Output is correct |
14 |
Correct |
739 ms |
2196 KB |
Output is correct |
15 |
Correct |
952 ms |
2196 KB |
Output is correct |
16 |
Correct |
910 ms |
2196 KB |
Output is correct |
17 |
Correct |
399 ms |
2196 KB |
Output is correct |
18 |
Correct |
364 ms |
2196 KB |
Output is correct |
19 |
Correct |
108 ms |
2196 KB |
Output is correct |
20 |
Correct |
150 ms |
2196 KB |
Output is correct |
21 |
Correct |
34 ms |
2196 KB |
Output is correct |
22 |
Correct |
41 ms |
2196 KB |
Output is correct |