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
| #include<bits/stdc++.h> #define fo(i,a,b) for(int i=a;i<=b;i++) using namespace std;
const int maxn=1e5+5, maxsum=2e5+5, maxe=4e5+5;
int n,m; vector<int> S[maxn];
vector<int> e[maxn]; int dis[2*maxn],pt[2*maxn],maxdis; bool Hopcroft_bfs() { memset(dis,255,sizeof(dis)); maxdis=-1; queue<int> Q; fo(i,1,n) if (!pt[i]) { Q.push(i); dis[i]=0; } while (!Q.empty()) { int cur=Q.front(); Q.pop(); for(int go:e[cur]) if (dis[go]==-1) { dis[go]=dis[cur]+1; if (!pt[go]) { maxdis=dis[go]; continue; } else { dis[pt[go]]=dis[go]+1; Q.push(pt[go]); } } } return maxdis>-1; } bool Hopcroft_dfs(int k) { for(int go:e[k]) if (dis[k]+1==dis[go]) { dis[go]=-1; if (!pt[go] || Hopcroft_dfs(pt[go])) { pt[k]=go, pt[go]=k; return 1; } } return 0; } int Hopcroft() { int re=0; while (Hopcroft_bfs()) fo(i,1,n) if (!pt[i]) re+=Hopcroft_dfs(i); return re; }
bool vis[maxn]; pair<int,int> ans[maxn]; int main() { scanf("%d",&n); fo(i,1,n-1) { int sz,x; scanf("%d",&sz); fo(j,1,sz) { scanf("%d",&x); e[x].push_back(n+i); S[x].push_back(i); } } if (Hopcroft()!=n-1) {puts("-1"); return 0;} int root; fo(i,1,n) if (!pt[i]) {root=i; break;} queue<pair<int,int>> Q; for(int x:S[root]) { vis[x]=1; Q.push(make_pair(root,x)); } while (!Q.empty()) { auto cur=Q.front(); Q.pop(); int son=pt[n+cur.second]; ans[cur.second]=make_pair(cur.first,son); for(int x:S[son]) if (!vis[x]) { vis[x]=1; Q.push(make_pair(son,x)); } } fo(i,1,n-1) if (!vis[i]) {puts("-1"); return 0;} fo(i,1,n-1) printf("%d %d\n",ans[i].first,ans[i].second); }
|