Language: C++

#include <cstdio>
#include <iostream>
#include <algorithm>
#define fi first
#define se second

using namespace std;

typedef pair<int,int> par;

const int N=100010;

int n,m,q,fa[N],ans[N],size[N],rank[N];
par e[N];
struct Qry{
  int x,y,z,g;
}Q[N],a[N],b[N];

inline char nc(){
  static char buf[100000],*p1=buf,*p2=buf;
  return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}

inline void read(int &x){
  char c=nc(); x=0;
  for(;c>'9'||c<'0';c=nc());for(;c>='0'&&c<='9';x=x*10+c-'0',c=nc());
}

int Gfat(int x){
  return fa[x]==x?x:Gfat(fa[x]);
}

struct sta{
  int x,rk;
}S[N];
int t;

inline void Union(int x,int y){
  x=Gfat(x); y=Gfat(y);
  if(x==y) return ;
  if(rank[x]>rank[y]) swap(x,y);
  fa[x]=y; size[y]+=size[x]; S[++t]=(sta){x,rank[y]};
  if(rank[x]==rank[y]) rank[y]++;
}

inline void Back(){
  int x=S[t].x; size[fa[x]]-=size[x]; rank[fa[x]]=S[t].rk;
  fa[x]=x; t--;
}

void solve(int l,int r,int L,int R){
  if(l>r) return ;
  int mid=l+r>>1,t1=0,t2=0,cur=t;
  for(int i=l;i<=mid;i++)
    Union(e[i].fi,e[i].se);
  for(int i=L;i<=R;i++){
    int x=Gfat(Q[i].x),y=Gfat(Q[i].y);
    if(x==y){
      if(size[x]>=Q[i].z)
	a[++t1]=Q[i],ans[Q[i].g]=mid;
      else
	b[++t2]=Q[i];
    }
    else{
      if(size[x]+size[y]>=Q[i].z)
	a[++t1]=Q[i],ans[Q[i].g]=mid;
      else
	b[++t2]=Q[i];
    }
  }
  for(int i=1;i<=t1;i++) Q[L+i-1]=a[i];
  for(int i=1;i<=t2;i++) Q[L+t1+i-1]=b[i];
  solve(mid+1,r,L+t1,R);
  while(t>cur) Back();
  solve(l,mid-1,L,L+t1-1);
}

int main(){
  read(n); read(m);
  for(int i=1;i<=n;i++)
    fa[i]=i,size[i]=rank[i]=1;
  for(int i=1;i<=m;i++)
    read(e[i].fi),read(e[i].se);
  read(q);
  for(int i=1;i<=q;i++)
    read(Q[i].x),read(Q[i].y),read(Q[i].z),Q[i].g=i;
  solve(1,m,1,q);
  for(int i=1;i<=m;i++) printf("%d\n",ans[i]);
  return 0;
}


Compiler output:
a.cc: In function 'void Union(int, int)':
a.cc:41:6: error: reference to 'rank' is ambiguous
   41 |   if(rank[x]>rank[y]) swap(x,y);
      |      ^~~~
In file included from /usr/include/c++/14/bits/move.h:37,
                 from /usr/include/c++/14/bits/exception_ptr.h:41,
                 from /usr/include/c++/14/exception:166,
                 from /usr/include/c++/14/ios:41,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:2:
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:13:32: note:                 'int rank [100010]'
   13 | int n,m,q,fa[N],ans[N],size[N],rank[N];
      |                                ^~~~
a.cc:41:14: error: reference to 'rank' is ambiguous
   41 |   if(rank[x]>rank[y]) swap(x,y);
      |              ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:13:32: note:                 'int rank [100010]'
   13 | int n,m,q,fa[N],ans[N],size[N],rank[N];
      |                                ^~~~
a.cc:42:12: error: reference to 'size' is ambiguous
   42 |   fa[x]=y; size[y]+=size[x]; S[++t]=(sta){x,rank[y]};
      |            ^~~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44:
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:13:24: note:                 'int size [100010]'
   13 | int n,m,q,fa[N],ans[N],size[N],rank[N];
      |                        ^~~~
a.cc:42:21: error: reference to 'size' is ambiguous
   42 |   fa[x]=y; size[y]+=size[x]; S[++t]=(sta){x,rank[y]};
      |                     ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:13:24: note:                 'int size [100010]'
   13 | int n,m,q,fa[N],ans[N],size[N],rank[N];
      |                        ^~~~
a.cc:42:41: error: expected primary-expression before ')' token
   42 |   fa[x]=y; size[y]+=size[x]; S[++t]=(sta){x,rank[y]};
      |                                         ^
a.cc:43:6: error: reference to 'rank' is ambiguous
   43 |   if(rank[x]==rank[y]) rank[y]++;
      |      ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:13:32: note:                 'int rank [100010]'
   13 | int n,m,q,fa[N],ans[N],size[N],rank[N];
      |                                ^~~~
a.cc:43:15: error: reference to 'rank' is ambiguous
   43 |   if(rank[x]==rank[y]) rank[y]++;
      |               ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:13:32: note:                 'int rank [100010]'
   13 | int n,m,q,fa[N],ans[N],size[N],rank[N];
      |                                ^~~~
a.cc:43:24: error: reference to 'rank' is ambiguous
   43 |   if(rank[x]==rank[y]) rank[y]++;
      |                        ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:13:32: note:                 'int rank [100010]'
   13 | int n,m,q,fa[N],ans[N],size[N],rank[N];
      |                                ^~~~
a.cc: In function 'void Back()':
a.cc:47:17: error: reference to 'size' is ambiguous
   47 |   int x=S[t].x; size[fa[x]]-=size[x]; rank[fa[x]]=S[t].rk;
      |                 ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:13:24: note:                 'int size [100010]'
   13 | int n,m,q,fa[N],ans[N],size[N],rank[N];
      |                        ^~~~
a.cc:47:30: error: reference to 'size' is ambiguous
   47 |   int x=S[t].x; size[fa[x]]-=size[x]; rank[fa[x]]=S[t].rk;
      |                              ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:13:24: note:                 'int size [100010]'
   13 | int n,m,q,fa[N],ans[N],size[N],rank[N];
      |                        ^~~~
a.cc:47:39: error: reference to 'rank' is ambiguous
   47 |   int x=S[t].x; size[fa[x]]-=size[x]; rank[fa[x]]=S[t].rk;
      |                                       ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:13:32: note:                 'int rank [100010]'
   13 | int n,m,q,fa[N],ans[N],size[N],rank[N];
      |                                ^~~~
a.cc: In function 'void solve(int, int, int, int)':
a.cc:59:10: error: reference to 'size' is ambiguous
   59 |       if(size[x]>=Q[i].z)
      |          ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:13:24: note:                 'int size [100010]'
   13 | int n,m,q,fa[N],ans[N],size[N],rank[N];
      |                        ^~~~
a.cc:65:10: error: reference to 'size' is ambiguous
   65 |       if(size[x]+size[y]>=Q[i].z)
      |          ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:13:24: note:                 'int size [100010]'
   13 | int n,m,q,fa[N],ans[N],size[N],rank[N];
      |                        ^~~~
a.cc:65:18: error: reference to 'size' is ambiguous
   65 |       if(size[x]+size[y]>=Q[i].z)
      |                  ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:13:24: note:                 'int size [100010]'
   13 | int n,m,q,fa[N],ans[N],size[N],rank[N];
      |                        ^~~~
a.cc: In function 'int main()':
a.cc:81:13: error: reference to 'size' is ambiguous
   81 |     fa[i]=i,size[i]=rank[i]=1;
      |             ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:13:24: note:                 'int size [100010]'
   13 | int n,m,q,fa[N],ans[N],size[N],rank[N];
      |                        ^~~~
a.cc:81:21: error: reference to 'rank' is ambiguous
   81 |     fa[i]=i,size[i]=rank[i]=1;
      |                     ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:13:32: note:                 'int rank [100010]'
   13 | int n,m,q,fa[N],ans[N],size[N],rank[N];
      |                                ^~~~