#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 |
1 ms |
2200 KB |
Output is correct |
2 |
Correct |
0 ms |
2200 KB |
Output is correct |
3 |
Correct |
0 ms |
2200 KB |
Output is correct |
4 |
Correct |
0 ms |
2200 KB |
Output is correct |
5 |
Correct |
0 ms |
2200 KB |
Output is correct |
6 |
Correct |
0 ms |
2200 KB |
Output is correct |
7 |
Correct |
0 ms |
2200 KB |
Output is correct |
8 |
Correct |
0 ms |
2200 KB |
Output is correct |
9 |
Correct |
0 ms |
2200 KB |
Output is correct |
10 |
Correct |
0 ms |
2200 KB |
Output is correct |
11 |
Correct |
0 ms |
2200 KB |
Output is correct |
12 |
Correct |
0 ms |
2200 KB |
Output is correct |
13 |
Correct |
0 ms |
2200 KB |
Output is correct |
14 |
Correct |
0 ms |
2200 KB |
Output is correct |
15 |
Correct |
0 ms |
2200 KB |
Output is correct |
16 |
Correct |
0 ms |
2200 KB |
Output is correct |
17 |
Correct |
0 ms |
2200 KB |
Output is correct |
18 |
Correct |
0 ms |
2200 KB |
Output is correct |
19 |
Correct |
0 ms |
2200 KB |
Output is correct |
20 |
Correct |
0 ms |
2200 KB |
Output is correct |
21 |
Correct |
0 ms |
2200 KB |
Output is correct |
22 |
Correct |
1 ms |
2200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
2200 KB |
Output is correct |
2 |
Correct |
24 ms |
2200 KB |
Output is correct |
3 |
Correct |
25 ms |
2200 KB |
Output is correct |
4 |
Correct |
30 ms |
2200 KB |
Output is correct |
5 |
Correct |
34 ms |
2200 KB |
Output is correct |
6 |
Correct |
225 ms |
2200 KB |
Output is correct |
7 |
Correct |
224 ms |
2200 KB |
Output is correct |
8 |
Correct |
222 ms |
2200 KB |
Output is correct |
9 |
Correct |
26 ms |
2200 KB |
Output is correct |
10 |
Correct |
30 ms |
2200 KB |
Output is correct |
11 |
Correct |
25 ms |
2200 KB |
Output is correct |
12 |
Correct |
29 ms |
2200 KB |
Output is correct |
13 |
Correct |
29 ms |
2200 KB |
Output is correct |
14 |
Correct |
23 ms |
2200 KB |
Output is correct |
15 |
Correct |
26 ms |
2200 KB |
Output is correct |
16 |
Correct |
30 ms |
2200 KB |
Output is correct |
17 |
Correct |
10 ms |
2200 KB |
Output is correct |
18 |
Correct |
5 ms |
2200 KB |
Output is correct |
19 |
Correct |
3 ms |
2200 KB |
Output is correct |
20 |
Correct |
3 ms |
2200 KB |
Output is correct |
21 |
Correct |
0 ms |
2200 KB |
Output is correct |
22 |
Correct |
0 ms |
2200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
790 ms |
2200 KB |
Output is correct |
2 |
Correct |
785 ms |
2200 KB |
Output is correct |
3 |
Correct |
769 ms |
2200 KB |
Output is correct |
4 |
Correct |
803 ms |
2200 KB |
Output is correct |
5 |
Correct |
825 ms |
2200 KB |
Output is correct |
6 |
Correct |
695 ms |
2200 KB |
Output is correct |
7 |
Correct |
771 ms |
2200 KB |
Output is correct |
8 |
Correct |
818 ms |
2200 KB |
Output is correct |
9 |
Correct |
1049 ms |
2200 KB |
Output is correct |
10 |
Correct |
1093 ms |
2200 KB |
Output is correct |
11 |
Correct |
783 ms |
2200 KB |
Output is correct |
12 |
Correct |
716 ms |
2200 KB |
Output is correct |
13 |
Correct |
737 ms |
2200 KB |
Output is correct |
14 |
Correct |
776 ms |
2200 KB |
Output is correct |
15 |
Correct |
955 ms |
2200 KB |
Output is correct |
16 |
Correct |
927 ms |
2200 KB |
Output is correct |
17 |
Correct |
401 ms |
2200 KB |
Output is correct |
18 |
Correct |
365 ms |
2200 KB |
Output is correct |
19 |
Correct |
112 ms |
2200 KB |
Output is correct |
20 |
Correct |
149 ms |
2200 KB |
Output is correct |
21 |
Correct |
45 ms |
2200 KB |
Output is correct |
22 |
Correct |
44 ms |
2200 KB |
Output is correct |