Language: C++

#include<cstdio>
#include<cstring>
#include<vector>
#define inf 1e9
#define N 300001
using namespace std;
int total1,total2,SS,TT,S,T;
int next[N],head[N],edge[N],v[N],next2[N],head2[N],edge2[N],down[N],up[N];
int du[N],h[N],d[N],out[N],in[N];
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)
{
	total2++;
	next[total2]=head[x];
	head[x]=total2;
	edge[total2]=y;
	v[total2]=z;
}
void insert2(int x,int y,int l,int r)
{
//	printf("%d %d %d %d\n",x,y,l,r);
	total1++;
	next2[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=next[i])
		{
			int y=edge[i];
			if (v[i]&&!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=next[i])
	{
		int y=edge[i];
		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=next2[j])
		{
			int y=edge2[j];
			insert(i,y,up[j]-down[j]);
			insert(y,i,0);
		}
}
int re_dinic()
{
	int maxflow=0;
	int flow;
	while (bfs())
		while ((flow=dinic(SS,inf))) maxflow+=flow;
	return maxflow;
}
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=1;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+2*n,i+3*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]+3*n,i,0,1);
			insert2(i+n,s[i][j]+2*n,0,1);
		}
	}
	restructure();
	int sum=0;
	for (int i=S;i<=T;i++)	if (du[i]>=0) sum+=du[i];
	int maxflow=re_dinic();
	if (maxflow==sum)
	{
		printf("1\n");
	}
	else
		printf("-1\n");
}

Compiler output:
a.cc: In function 'void insert(int, int, int)':
a.cc:25:9: error: reference to 'next' is ambiguous
   25 |         next[total2]=head[x];
      |         ^~~~
In file included from /usr/include/c++/14/bits/stl_algobase.h:66,
                 from /usr/include/c++/14/vector:62,
                 from a.cc:3:
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:8:5: note:                 'int next [300001]'
    8 | int next[N],head[N],edge[N],v[N],next2[N],head2[N],edge2[N],down[N],up[N];
      |     ^~~~
a.cc: In function 'bool bfs()':
a.cc:50:40: error: reference to 'next' is ambiguous
   50 |                 for (int i=head[u];i;i=next[i])
      |                                        ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:8:5: note:                 'int next [300001]'
    8 | int next[N],head[N],edge[N],v[N],next2[N],head2[N],edge2[N],down[N],up[N];
      |     ^~~~
a.cc: In function 'int dinic(int, int)':
a.cc:69:40: error: reference to 'next' is ambiguous
   69 |         for (int i=head[k];i && rest;i=next[i])
      |                                        ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:8:5: note:                 'int next [300001]'
    8 | int next[N],head[N],edge[N],v[N],next2[N],head2[N],edge2[N],down[N],up[N];
      |     ^~~~