Input 1 8 3 3 2 5 3 6 4 8 Input 2 25 6 1 20 9 Input 3 9999 2222 2 1025 2405 3154 8949
Output 1 3 1 2 Output 2 4 Output 3 1980 2160
1 #include <cstdio> 2 #include <iostream> 3 #include <algorithm> 4 #include <cstring> 5 #include <cmath> 6 using namespace std; 7 const int inf=1000000; 8 int n,m,q,fa[100010],size[100010],day[100010],x,y,u,v,l,r,ans; 9 int getfather(int x,int y) 10 { 11 while (fa[x]!=x&&day[x]<=y) x=fa[x]; 12 return x; 13 } 14 void bcj() 15 { 16 for (int i=m;i>=1;i--) 17 for (int j=2;j<=n/i;j++) 18 { 19 int u=getfather(i,m),v=getfather(i*j,m); 20 if (u!=v) 21 { 22 if (size[u]>size[v]) swap(u,v); 23 day[u]=m-i+1; 24 size[v]+=size[u]; 25 fa[u]=v; 26 if ((++l)>=n-1) return; 27 } 28 } 29 } 30 int main() 31 { 32 // freopen("pictionary.in","r",stdin); 33 // freopen("pictionary.out","w",stdout); 34 scanf("%d%d%d",&n,&m,&q); 35 for (int i=1;i<=n;i++) fa[i]=i,day[i]=inf,size[i]=1; 36 bcj(); 37 for (int i=q;i>=1;i--) 38 { 39 scanf("%d%d",&u,&v); 40 l=0; r=m; 41 while (l<=r) 42 { 43 int mid=(l+r)/2; 44 x=getfather(u,mid),y=getfather(v,mid); 45 if (x==y) ans=mid,r=mid-1; else l=mid+1; 46 } 47 printf("%dn",ans); 48 } 49 return 0; 50 }
转载于:.html
本文发布于:2024-01-28 17:20:46,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/17064336529014.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |