1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| #include<bits/stdc++.h> const int M=1e6+5; template<typename Tp=int>inline Tp read(){Tp x(0);int op(0);char ch=getchar();while(ch<'0'||ch>'9')op|=(ch==45),ch=getchar();while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+(ch^48),ch=getchar();return op?-x:x;}
struct edge{ int u,v; }e[M]; int n,m; std::vector<int> G[M]; int fa[M][21],siz[M]; void adde(int u,int v){G[u].push_back(v);fa[v][0]=u;} int ufs[M]; int ff(int x){return ufs[x]==x?x:ufs[x]=ff(ufs[x]);} bool mrg(int u,int v){ u=ff(u),v=ff(v); return u==v?0:ufs[u]=v,1; } void dfs(int u){ if(u<=n)siz[u]=1; for(int i=1;i<=20;++i)fa[u][i]=fa[fa[u][i-1]][i-1]; for(auto v:G[u])dfs(v),siz[u]+=siz[v]; } void build(){ for(int i=1;i<=n+m;++i)ufs[i]=i; for(int i=1,tot=0;i<=m;++i){ int u=e[i].u,v=e[i].v; int uu=ff(u),vv=ff(v); if(uu==vv)continue; adde(i+n,uu),adde(i+n,vv); ufs[uu]=i+n,ufs[vv]=i+n; ++tot; if(tot==n-1){ for(int j=0;j<=20;++j)fa[i+n][j]=i+n; dfs(i+n); return; } } } bool chk(int u,int v,int mid,int w){ for(int i=20;i>=0;--i){ if(fa[u][i]<=mid)u=fa[u][i]; if(fa[v][i]<=mid)v=fa[v][i]; } int res=0; if(u==v)res=siz[u]; else res=siz[u]+siz[v]; return res>=w; } int main(){ n=read(),m=read(); for(int i=1;i<=m;++i){ int u=read(),v=read(); e[i]=(edge){u,v}; } build(); int q=read(); while(q--){ int u=read(),v=read(),w=read(); int l=0,r=m; while(l<r){ int mid=(l+r)>>1; if(chk(u,v,mid+n,w))r=mid; else l=mid+1; } printf("%d\n",l); } return 0; }
|