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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
| #include <bits/stdc++.h> using namespace std; #define FO(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout) #define fo(i,j,k) for(int i=(j),end_i=(k);i<=end_i;i++) #define ff(i,j,k) for(int i=(j),end_i=(k);i< end_i;i++) #define fd(i,j,k) for(int i=(j),end_i=(k);i>=end_i;i--) #define DEBUG(x) cerr<<#x<<"="<<x<<endl #define all(x) (x).begin(),(x).end() #define cle(x) memset(x,0,sizeof(x)) #define lowbit(x) ((x)&-(x)) #define ll long long #define ull unsigned ll #define db double #define lb long db #define pb push_back #define mp make_pair #define fi first #define se second inline int read() { int x=0; char ch=getchar(); bool f=0; for(;ch<'0'||ch>'9';ch=getchar()) if(ch=='-') f=1; for(;ch>='0'&&ch<='9';ch=getchar()) x=(x<<3)+(x<<1)+(ch^48); return f?-x:x; } #define CASET fo(___,1,read())
const int N=200010; const int M=2000000; namespace Dinic{ const int inf=2e9; int s,t; int head[N],cur[N],ne[M],ver[M],val[M],tot=1; int d[N]; queue<int> q; inline void add(int x,int y,int v) { ver[++tot]=y; val[tot]=v; ne[tot]=head[x]; head[x]=tot; ver[++tot]=x; val[tot]=0; ne[tot]=head[y]; head[y]=tot; } inline bool bfs() { fo(i,0,t) cur[i]=head[i]; for(;!q.empty();q.pop()); fo(i,0,t) d[i]=-1; q.push(s); d[s]=0; for(int u,v;!q.empty();) { u=q.front(); q.pop(); for(int i=head[u];i;i=ne[i]) if(val[i]&&d[v=ver[i]]==-1) { d[v]=d[u]+1,q.push(v); if(v==t) return 1; } } return 0; } int dfs(int u,int flow) { if(!flow||u==t) return flow; int res=flow,r,v; for(int &i=cur[u];i;i=ne[i]) if(val[i]&&d[v=ver[i]]==d[u]+1) { r=dfs(v,min(res,val[i])); if(!r) continue; val[i]-=r; val[i^1]+=r; res-=r; if(!res) break; } return flow-res; } int dinic() { int flow=0; while(bfs()) flow+=dfs(s,inf); return flow; } } using namespace Dinic; int n; int X[N],Y[N],cnt; bool vis[N]; void dfs(int u) { for(int i=head[u],v;i;i=ne[i]) if((v=ver[i])!=s&&!vis[v-n]) { vis[v-n]=1; for(int j=head[v];j;j=ne[j]) if(val[j]) cnt++,X[v-n]=u,Y[v-n]=ver[j],dfs(ver[j]); } } int main() { n=read(); Dinic::t=2*n; s=0; t=2*n; ff(i,n+1,2*n) CASET add(read(),i,1); fo(i,1,n) add(s,i,1); ff(i,n+1,2*n) add(i,t,1); if(dinic()!=n-1) return puts("-1")&0; int root; for(int i=head[0];i;i=ne[i]) if(val[i]) root=ver[i]; dfs(root); if(cnt!=n-1) return puts("-1")&0; ff(i,1,n) printf("%d %d\n",X[i],Y[i]); return 0; }
|