#include<stdio.h>
#include<string.h>
using namespace std;
int cnt,group[710],ran[710],a,b,n,m,k,x[150010],y[150010],arr[710],arr2[710],w,w2,sw;
int up(int x)
{
if(x==group[x]) return x;
return group[x]=up(group[x]);
}
void union_find(int x,int y,int i)
{
int p=up(x),q=up(y);
if(p==q) return;
if(ran[p]>ran[q]) group[q]=p;
else if(ran[p]<ran[q]) group[p]=q;
else group[p]=q, ran[q]++;
if(sw==0) arr[++w]=i;
if(sw==1) arr2[++w2]=i;
if(sw==2) cnt++;
}
void query(int a,int b)
{
int i;
sw=2; cnt=0;
for(i=1;i<=w;i++)
{
if(arr[i]>=a) break;
union_find(x[arr[i]],y[arr[i]],arr[i]);
}
for(i=1;i<=w2;i++)
{
if(arr2[i]<=b) break;
union_find(x[arr2[i]],y[arr2[i]],arr2[i]);
}
printf("%d\n",n-cnt);
}
int main()
{
int i,j;
scanf("%d %d",&n,&m);
for(i=1;i<=n;i++) group[i]=i;
for(i=1;i<=m;i++)
{
scanf("%d %d",&x[i],&y[i]);
union_find(x[i],y[i],i);
}
for(i=1;i<=n;i++) group[i]=i;
memset(ran,0,sizeof ran);
sw=1;
for(i=m;i>=1;i--) union_find(x[i],y[i],i);
scanf("%d",&k);
for(i=1;i<=k;i++)
{
for(j=1;j<=n;j++) group[j]=j;
memset(ran,0,sizeof ran);
scanf("%d %d",&a,&b);
query(a,b);
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2264 KB |
Output is correct |
2 |
Correct |
0 ms |
2264 KB |
Output is correct |
3 |
Correct |
0 ms |
2264 KB |
Output is correct |
4 |
Correct |
0 ms |
2264 KB |
Output is correct |
5 |
Correct |
0 ms |
2264 KB |
Output is correct |
6 |
Correct |
0 ms |
2264 KB |
Output is correct |
7 |
Correct |
1 ms |
2264 KB |
Output is correct |
8 |
Correct |
0 ms |
2264 KB |
Output is correct |
9 |
Correct |
0 ms |
2264 KB |
Output is correct |
10 |
Correct |
0 ms |
2264 KB |
Output is correct |
11 |
Correct |
0 ms |
2264 KB |
Output is correct |
12 |
Correct |
0 ms |
2264 KB |
Output is correct |
13 |
Correct |
0 ms |
2264 KB |
Output is correct |
14 |
Correct |
1 ms |
2264 KB |
Output is correct |
15 |
Correct |
0 ms |
2264 KB |
Output is correct |
16 |
Correct |
0 ms |
2264 KB |
Output is correct |
17 |
Correct |
0 ms |
2264 KB |
Output is correct |
18 |
Correct |
0 ms |
2264 KB |
Output is correct |
19 |
Correct |
0 ms |
2264 KB |
Output is correct |
20 |
Correct |
0 ms |
2264 KB |
Output is correct |
21 |
Correct |
0 ms |
2264 KB |
Output is correct |
22 |
Correct |
0 ms |
2264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
2264 KB |
Output is correct |
2 |
Correct |
28 ms |
2264 KB |
Output is correct |
3 |
Correct |
33 ms |
2264 KB |
Output is correct |
4 |
Correct |
44 ms |
2264 KB |
Output is correct |
5 |
Correct |
45 ms |
2264 KB |
Output is correct |
6 |
Correct |
306 ms |
2264 KB |
Output is correct |
7 |
Correct |
314 ms |
2264 KB |
Output is correct |
8 |
Correct |
321 ms |
2264 KB |
Output is correct |
9 |
Correct |
29 ms |
2264 KB |
Output is correct |
10 |
Correct |
28 ms |
2264 KB |
Output is correct |
11 |
Correct |
28 ms |
2264 KB |
Output is correct |
12 |
Correct |
37 ms |
2264 KB |
Output is correct |
13 |
Correct |
31 ms |
2264 KB |
Output is correct |
14 |
Correct |
37 ms |
2264 KB |
Output is correct |
15 |
Correct |
25 ms |
2264 KB |
Output is correct |
16 |
Correct |
29 ms |
2264 KB |
Output is correct |
17 |
Correct |
11 ms |
2264 KB |
Output is correct |
18 |
Correct |
9 ms |
2264 KB |
Output is correct |
19 |
Correct |
3 ms |
2264 KB |
Output is correct |
20 |
Correct |
4 ms |
2264 KB |
Output is correct |
21 |
Correct |
0 ms |
2264 KB |
Output is correct |
22 |
Correct |
2 ms |
2264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1063 ms |
2264 KB |
Output is correct |
2 |
Correct |
992 ms |
2264 KB |
Output is correct |
3 |
Correct |
1151 ms |
2264 KB |
Output is correct |
4 |
Correct |
1056 ms |
2264 KB |
Output is correct |
5 |
Correct |
1063 ms |
2264 KB |
Output is correct |
6 |
Correct |
1108 ms |
2264 KB |
Output is correct |
7 |
Correct |
1071 ms |
2264 KB |
Output is correct |
8 |
Correct |
1142 ms |
2264 KB |
Output is correct |
9 |
Correct |
1034 ms |
2264 KB |
Output is correct |
10 |
Correct |
998 ms |
2264 KB |
Output is correct |
11 |
Correct |
1031 ms |
2264 KB |
Output is correct |
12 |
Correct |
1005 ms |
2264 KB |
Output is correct |
13 |
Correct |
1044 ms |
2264 KB |
Output is correct |
14 |
Correct |
955 ms |
2264 KB |
Output is correct |
15 |
Correct |
909 ms |
2264 KB |
Output is correct |
16 |
Correct |
878 ms |
2264 KB |
Output is correct |
17 |
Correct |
470 ms |
2264 KB |
Output is correct |
18 |
Correct |
413 ms |
2264 KB |
Output is correct |
19 |
Correct |
141 ms |
2264 KB |
Output is correct |
20 |
Correct |
169 ms |
2264 KB |
Output is correct |
21 |
Correct |
42 ms |
2264 KB |
Output is correct |
22 |
Correct |
49 ms |
2264 KB |
Output is correct |