Language: C++

#include<cstdio>
#include<cstring>
#include<vector>
#define inf 1e9
#define N 4000001
using namespace std;
int total1,total2,SS,TT,S,T;
int father[N],get[N];
int nexts[N],head[N],edge[N],v1[N],v[N],nexts2[N],head2[N],edge2[N],down[N],up[N];
int du[N],h[N],d[N],out[N],in[N],ans[N][15];
int n;
vector<int>s[N];
int max(int x,int y)
{
	if (x<y) return y;
	else	return x;
}
int min(int x,int y)
{
	if (x>y) return y;
	else	return x;
}
void insert(int x,int y,int z)
{
//	if(total2%2==1)
//	printf("%d %d %d\n",x,y,z);
	total2++;
	nexts[total2]=head[x];
	head[x]=total2;
	edge[total2]=y;
	v[total2]=z;
//	v1[total2]=z1;
}
void insert2(int x,int y,int l,int r)
{
//	printf("%d %d %d %d\n",x,y,l,r);
	total1++;
	nexts2[total1]=head2[x];
	head2[x]=total1;
	edge2[total1]=y;
	down[total1]=l;
	up[total1]=r;
	out[x]+=l;in[y]+=l;
}
bool bfs()
{
	memset(d,0,sizeof(d));
	int l=1,r=1;
	h[1]=SS;
	d[SS]=1;
	while (l<=r)
	{
		int u=h[l];
		for (int i=head[u];i;i=nexts[i])
		{
			int y=edge[i];
			if (y>TT) continue;
			if (v[i]>0&&!d[y])
			{
				r++;
				h[r]=y;
				d[y]=d[u]+1;
				if (y==TT) return true;
			}
		}
		l++;
	}
	return false;
}
int dinic(int k,int flow)
{ 
	if (k==TT) return flow;
	int rest=flow;
	for (int i=head[k];i && rest;i=nexts[i])
	{
		int y=edge[i];
		if (y>TT) continue;
		if (v[i]&&d[y]==d[k]+1)
		{
			int cost;
			if (rest>v[i]) cost=v[i];
			else	cost=rest;
			int pay=dinic(y,cost);
			if (pay==0) d[y]=0;
			rest-=pay;
			v[i]-=pay;
			v[i^1]+=pay;
		}
	}
	return flow-rest;
}
void restructure()
{
	SS=T+1;
	TT=SS+1;
	for (int i=S;i<=T;i++)
		du[i]=in[i]-out[i];
	total2=1;
	insert(T,S,inf);
	insert(S,T,0);
	for (int i=S;i<=T;i++)
		if (du[i]>0) 
		{
			insert(SS,i,du[i]);insert(i,SS,0);
		}
		else	
		{
			insert(i,TT,-du[i]);insert(TT,i,0);
		}
	for (int i=S;i<=T;i++)
		for (int j=head2[i];j;j=nexts2[j])
		{
			int y=edge2[j];
			if (i==6&&y==11)
			{
				T++;T--;
			}
			insert(i,y,up[j]-down[j]);
			insert(y,i,0);
		}
}
int re_dinic(int start)
{
	int maxflow=0;
	int flow;
	while (bfs())
		while ((flow=dinic(start,inf))) maxflow+=flow;
	return maxflow;
}
void dfs2(int k,int fa)
{
	for (int i=head2[k+n];i;i=nexts2[i])
	{
		int y=edge2[i];
		if (father[y]==0)
		{
			father[y]=k;
			dfs2(get[y],k);
		}
	}
}
int main()
{
	S=0;
	scanf("%d",&n);
	T=n*4+1;
	total1=1;
	memset(out,0,sizeof(out));
	memset(in,0,sizeof(in));
	insert2(S,1,0,1e9);
	for (int i=2;i<=n;i++)
	{
		insert2(i,i+n,1,1e9);
	//	insert2(i+n,i,1,1e9);	
		insert2(i+n,T,0,1e9);
	}
	for (int i=1;i<n;i++)
	{
		insert2(i+2*n,i+3*n,1,1);
	//	insert2(i+3*n,i+2*n,1,1);
	}
	for (int i=1;i<n;i++)
	{
	//	int p;
		s[i].resize(1);
		scanf("%d",&s[i][0]);
		s[i].resize(s[i][0]+1);
		for (int j=1;j<=s[i][0];j++) 	scanf("%d",&s[i][j]);
		for (int j=1;j<=s[i][0];j++)
		{
			insert2(s[i][j]+n,i+2*n,0,1);
			insert2(i+3*n,s[i][j],0,1);
		}
	}
	restructure();
	int sum=0;
	for (int i=S;i<=T;i++)	if (du[i]>=0) sum+=du[i];
	int maxflow=re_dinic(SS);
	if (maxflow==sum)
	{
		total1=1;
		SS=S=0;TT=T=2*n+1;
		memset(head,0,sizeof(head));
		memset(nexts,0,sizeof(nexts));
		memset(edge,0,sizeof(edge));
		memset(v,0,sizeof(v));
		total2=1;
		memset(head2,0,sizeof(head2));
		memset(edge2,0,sizeof(edge2));
		memset(nexts2,0,sizeof(nexts2));
		for (int i=1;i<n;i++)
		{
			insert(SS,i,1);
			insert(i,SS,0);
		//	printf("%d %d %d\n",SS,i,1); 
			for (int j=1;j<=s[i][0];j++)
			{
				insert2(s[i][j]+n,i,0,0);
				if (s[i][j]==1) continue;
			//	printf("%d %d %d\n",i,s[i][j]+n,1);
				insert(i,s[i][j]+n,1);
				insert(s[i][j]+n,i,0);
			}
		}
		for (int i=1;i<=n;i++)
		{
			insert(i+n,TT,1);
			insert(TT,i+n,0);
		//	printf("%d %d %d\n",i+n,TT,1);
		}
		maxflow=re_dinic(SS);
		for (int i=1;i<n;i++)
		{
			for (int j=head[i];j;j=nexts[j])
			{
				int y=edge[j];
			//	printf("%d %d %d\n",i,y,v[j^1]);
				if (j%2==0&&v[j]==0)
				{
					get[i]=y-n;
				}
			} 
		}
		memset(father,0,sizeof(father));
		//stotal=0;
		dfs2(1,0);
		for (int i=1;i<n;i++)
		{
			printf("%d %d\n",get[i],father[i]);
		}
	}
	else
	{
		printf("-1\n");
	}
}

Compiler output:
a.cc: In function 'void dfs2(int, int)':
a.cc:138:30: error: reference to 'get' is ambiguous
  138 |                         dfs2(get[y],k);
      |                              ^~~
In file included from /usr/include/c++/14/bits/memory_resource.h:47,
                 from /usr/include/c++/14/vector:87,
                 from a.cc:3:
/usr/include/c++/14/tuple:2517:5: note: candidates are: 'template<class _Tp, class ... _Types> constexpr const _Tp&& std::get(const tuple<_Elements ...>&&)'
 2517 |     get(const tuple<_Types...>&& __t) noexcept
      |     ^~~
/usr/include/c++/14/tuple:2505:5: note:                 'template<class _Tp, class ... _Types> constexpr const _Tp& std::get(const tuple<_Elements ...>&)'
 2505 |     get(const tuple<_Types...>& __t) noexcept
      |     ^~~
/usr/include/c++/14/tuple:2494:5: note:                 'template<class _Tp, class ... _Types> constexpr _Tp&& std::get(tuple<_Elements ...>&&)'
 2494 |     get(tuple<_Types...>&& __t) noexcept
      |     ^~~
/usr/include/c++/14/tuple:2483:5: note:                 'template<class _Tp, class ... _Types> constexpr _Tp& std::get(tuple<_Elements ...>&)'
 2483 |     get(tuple<_Types...>& __t) noexcept
      |     ^~~
/usr/include/c++/14/tuple:2476:5: note:                 'template<long unsigned int __i, class ... _Elements> constexpr std::__enable_if_t<(__i >= sizeof... (_Types))> std::get(const tuple<_Elements ...>&)'
 2476 |     get(const tuple<_Elements...>&) = delete;
      |     ^~~
In file included from /usr/include/c++/14/bits/stl_algobase.h:64,
                 from /usr/include/c++/14/vector:62:
/usr/include/c++/14/bits/stl_pair.h:1307:5: note:                 'template<class _Tp, class _Up> constexpr const _Tp&& std::get(const pair<_Up, _Tp>&&)'
 1307 |     get(const pair<_Up, _Tp>&& __p) noexcept
      |     ^~~
/usr/include/c++/14/bits/stl_pair.h:1302:5: note:                 'template<class _Tp, class _Up> constexpr _Tp&& std::get(pair<_Up, _Tp>&&)'
 1302 |     get(pair<_Up, _Tp>&& __p) noexcept
      |     ^~~
/usr/include/c++/14/bits/stl_pair.h:1297:5: note:                 'template<class _Tp, class _Up> constexpr const _Tp& std::get(const pair<_Up, _Tp>&)'
 1297 |     get(const pair<_Up, _Tp>& __p) noexcept
      |     ^~~
/usr/include/c++/14/bits/stl_pair.h:1292:5: note:                 'template<class _Tp, class _Up> constexpr _Tp& std::get(pair<_Up, _Tp>&)'
 1292 |     get(pair<_Up, _Tp>& __p) noexcept
      |     ^~~
/usr/include/c++/14/bits/stl_pair.h:1287:5: note:                 'template<class _Tp, class _Up> constexpr const _Tp&& std::get(const pair<_T1, _T2>&&)'
 1287 |     get(const pair<_Tp, _Up>&& __p) noexcept
      |     ^~~
/usr/include/c++/14/bits/stl_pair.h:1282:5: note:                 'template<class _Tp, class _Up> constexpr _Tp&& std::get(pair<_T1, _T2>&&)'
 1282 |     get(pair<_Tp, _Up>&& __p) noexcept
      |     ^~~
/usr/include/c++/14/bits/stl_pair.h:1277:5: note:                 'template<class _Tp, class _Up> constexpr const _Tp& std::get(const pair<_T1, _T2>&)'
 1277 |     get(const pair<_Tp, _Up>& __p) noexcept
      |     ^~~
/usr/include/c++/14/bits/stl_pair.h:1272:5: note:                 'template<class _Tp, class _Up> constexpr _Tp& std::get(pair<_T1, _T2>&)'
 1272 |     get(pair<_Tp, _Up>& __p) noexcept
      |     ^~~
/usr/include/c++/14/bits/stl_pair.h:150:5: note:                 'template<long unsigned int _Int, class _Tp, long unsigned int _Nm> constexpr const _Tp&& std::get(const array<_Tp, _Nm>&&)'
  150 |     get(const array<_Tp, _Nm>&&) noexcept;
      |     ^~~
/usr/include/c++/14/bits/stl_pair.h:146:5: note:                 'template<long unsigned int _Int, class _Tp, long unsigned int _Nm> constexpr const _Tp& std::get(const array<_Tp, _Nm>&)'
  146 |     get(const array<_Tp, _Nm>&) noexcept;
      |     ^~~
/usr/include/c++/14/bits/stl_pair.h:142:5: note:                 'template<long unsigned int _Int, class _Tp, long unsigned int _Nm> constexpr _Tp&& std::get(array<_Tp, _Nm>&&)'
  142 |     get(array<_Tp, _Nm>&&) noexcept;
      |     ^~~
/usr/include/c++/14/bits/stl_pair.h:138:5: note:                 'template<long unsigned int _Int, class _Tp, long unsigned int _Nm> constexpr _Tp& std::get(array<_Tp, _Nm>&)'
  138 |     get(array<_Tp, _Nm>&) noexcept;
      |     ^~~
/usr/include/c++/14/tuple:2466:5: note:                 'template<long unsigned int __i, class ... _Elements> constexpr std::__tuple_element_t<__i, std::tuple<_Elements ...> >&& std::get(const tuple<_Elements ...>&&)'
 2466 |     get(const tuple<_Elements...>&& __t) noexcept
      |     ^~~
/usr/include/c++/14/tuple:2457:5: note:                 'template<long unsigned int __i, class ... _Elements> constexpr std::__tuple_element_t<__i, std::tuple<_Elements ...> >&& std::get(tuple<_Elements ...>&&)'
 2457 |     get(tuple<_Elements...>&& __t) noexcept
      |     ^~~
/usr/include/c++/14/tuple:2451:5: note:                 'template<long unsigned int __i, class ... _Elements> constexpr std::__tuple_element_t<__i, std::tuple<_Elements ...> >& std::get(const tuple<_Elements ...>&)'
 2451 |     get(const tuple<_Elements...>& __t) noexcept
      |     ^~~
/usr/include/c++/14/tuple:2445:5: note:                 'template<long unsigned int __i, class ... _Elements> constexpr std::__tuple_element_t<__i, std::tuple<_Elements ...> >& std::get(tuple<_Elements ...>&)'
 2445 |     get(tuple<_Elements...>& __t) noexcept
      |     ^~~
/usr/include/c++/14/bits/stl_pair.h:1265:5: note:                 'template<long unsigned int _Int, class _Tp1, class _Tp2> constexpr const typename std::tuple_element<_Int, std::pair<_Tp1, _Tp2> >::type&& std::get(const pair<_Tp1, _Tp2>&&)'
 1265 |     get(const pair<_Tp1, _Tp2>&& __in) noexcept
      |     ^~~
/usr/include/c++/14/bits/stl_pair.h:1260:5: note:                 'template<long unsigned int _Int, class _Tp1, class _Tp2> constexpr const typename std::tuple_element<_Int, std::pair<_Tp1, _Tp2> >::type& std::get(const pair<_Tp1, _Tp2>&)'
 1260 |     get(const pair<_Tp1, _Tp2>& __in) noexcept
      |     ^~~
/usr/include/c++/14/bits/stl_pair.h:1255:5: note:                 'template<long unsigned int _Int, class _Tp1, class _Tp2> constexpr typename std::tuple_element<_Int, std::pair<_Tp1, _Tp2> >::type&& std::get(pair<_Tp1, _Tp2>&&)'
 1255 |     get(pair<_Tp1, _Tp2>&& __in) noexcept
      |     ^~~
/usr/include/c++/14/bits/stl_pair.h:1250:5: note:                 'template<long unsigned int _Int, class _Tp1, class _Tp2> constexpr typename std::tuple_element<_Int, std::pair<_Tp1, _Tp2> >::type& std::get(pair<_Tp1, _Tp2>&)'
 1250 |     get(pair<_Tp1, _Tp2>& __in) noexcept
      |     ^~~
a.cc:8:15: note:                 'int get [4000001]'
    8 | int father[N],get[N];
      |               ^~~
a.cc: In function 'int main()':
a.cc:220:41: error: reference to 'get' is ambiguous
  220 |                                         get[i]=y-n;
      |                                         ^~~
/usr/include/c++/14/tuple:2517:5: note: candidates are: 'template<class _Tp, class ... _Types> constexpr const _Tp&& std::get(const tuple<_Elements ...>&&)'
 2517 |     get(const tuple<_Types...>&& __t) noexcept
      |     ^~~
/usr/include/c++/14/tuple:2505:5: note:                 'template<class _Tp, class ... _Types> constexpr const _Tp& std::get(const tuple<_Elements ...>&)'
 2505 |     get(const tuple<_Types...>& __t) noexcept
      |     ^~~
/usr/include/c++/14/tuple:2494:5: note:                 'template<class _Tp, class ... _Types> constexpr _Tp&& std::get(tuple<_Elements ...>&&)'
 2494 |     get(tuple<_Types...>&& __t) noexcept
      |     ^~~
/usr/include/c++/14/tuple:2483:5: note:                 'template<class _Tp, class ... _Types> constexpr _Tp& std::get(tuple<_Elements ...>&)'
 2483 |     get(tuple<_Types...>& __t) noexcept
      |     ^~~
/usr/include/c++/14/tuple:2476:5: note:                 'template<long unsigned int __i, class ... _Elements> constexpr std::__enable_if_t<(__i >= sizeof... (_Types))> std::get(const tuple<_Elements ...>&)'
 2476 |     get(const tuple<_Elements...>&) = delete;
      |     ^~~
/usr/include/c++/14/bits/stl_pair.h:1307:5: note:                 'template<class _Tp, class _Up> constexpr const _Tp&& std::get(const pair<_Up, _Tp>&&)'
 1307 |     get(const pair<_Up, _Tp>&& __p) noexcept
      |     ^~~
/usr/include/c++/14/bits/stl_pair.h:1302:5: note:                 'template<class _Tp, class _Up> constexpr _Tp&& std::get(pair<_Up, _Tp>&&)'
 1302 |     get(pair<_Up, _Tp>&& __p) noexcept
      |     ^~~
/usr/include/c++/14/bits/stl_pair.h:1297:5: note:                 'template<class _Tp, class _Up> constexpr const _Tp& std::get(const pair<_Up, _Tp>&)'
 1297 |     get(const pair<_Up, _Tp>& __p) noexcept
      |     ^~~
/usr/include/c++/14/bits/stl_pair.h:1292:5: note:                 'template<class _Tp, class _Up> constexpr _Tp& std::get(pair<_Up, _Tp>&)'
 1292 |     get(pair<_Up, _Tp>& __p) noexcept
      |     ^~~
/usr/include/c++/14/bits/stl_pair.h:1287:5: note:                 'template<class _Tp, class _Up> constexpr const _Tp&& std::get(const pair<_T1, _T2>&&)'
 1287 |     get(const pair<_Tp, _Up>&& __p) noexcept
      |     ^~~
/usr/include/c++/14/bits/stl_pair.h:1282:5: note:                 'template<class _Tp, class _Up> constexpr _Tp&& std::get(pair<_T1, _T2>&&)'
 1282 |     get(pair<_Tp, _Up>&& __p) noexcept
      |     ^~~
/usr/include/c++/14/bits/stl_pair.h:1277:5: note:                 'template<class _Tp, class _Up> constexpr const _Tp& std::get(const pair<_T1, _T2>&)'
 1277 |     get(const pair<_Tp, _Up>& __p) noexcept
      |     ^~~
/usr/include/c++/14/bits/stl_pair.h:1272:5: note:                 'template<class _Tp, class _Up> constexpr _Tp& std::get(pair<_T1, _T2>&)'
 1272 |     get(pair<_Tp, _Up>& __p) noexcept
      |     ^~~
/usr/include/c++/14/bits/stl_pair.h:150:5: note:                 'template<long unsigned int _Int, class _Tp, long unsigned int _Nm> constexpr const _Tp&& std::get(const array<_Tp, _Nm>&&)'
  150 |     get(const array<_Tp, _Nm>&&) noexcept;
      |     ^~~
/usr/include/c++/14/bits/stl_pair.h:146:5: note:                 'template<long unsigned int _Int, class _Tp, long unsigned int _Nm> constexpr const _Tp& std::get(const array<_Tp, _Nm>&)'
  146