Problem:
Score : 2200 points

Problem Statement

You are given N-1 subsets of \{1,2,...,N\}. Let the i-th set be E_i.

Let us choose two distinct elements u_i and v_i from each set E_i, and consider a graph T with N vertices and N-1 edges, whose vertex set is \{1,2,..,N\} and whose edge set is (u_1,v_1),(u_2,v_2),...,(u_{N-1},v_{N-1}).
Determine if T can be a tree by properly deciding u_i and v_i.
If it can, additionally find one instance of (u_1,v_1),(u_2,v_2),...,(u_{N-1},v_{N-1}) such that T is actually a tree.

Constraints

2 \leq N \leq 10^5

E_i is a subset of \{1,2,..,N\}.

|E_i| \geq 2

The sum of |E_i| is at most 2 \times 10^5.

Input

Input is given from Standard Input in the following format:

N
c_1 w_{1,1} w_{1,2} ... w_{1,c_1}
:
c_{N-1} w_{N-1,1} w_{N-1,2} ... w_{N-1,c_{N-1}}

Here, c_i stands for the number of elements in E_i, and w_{i,1},...,w_{i,c_i} are the c_i elements in c_i.
Here, 2 \leq c_i \leq N, 1 \leq w_{i,j} \leq N, and w_{i,j} \neq w_{i,k} (1 \leq j < k \leq c_i) hold.

Output

If T cannot be a tree, print -1; otherwise, print the choices of (u_i,v_i) that satisfy the condition, in the following format:

u_1 v_1
:
u_{N-1} v_{N-1}

Sample Input 1

5
2 1 2
3 1 2 3
3 3 4 5
2 4 5

Sample Output 1

1 2
1 3
3 4
4 5

Sample Input 2

6
3 1 2 3
3 2 3 4
3 1 3 4
3 1 2 4
3 4 5 6

Sample Output 2

-1

Sample Input 3

10
5 1 2 3 4 5
5 2 3 4 5 6
5 3 4 5 6 7
5 4 5 6 7 8
5 5 6 7 8 9
5 6 7 8 9 10
5 7 8 9 10 1
5 8 9 10 1 2
5 9 10 1 2 3

Sample Output 3

1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10

Solution in C++ (GCC 9.2.1) (judge verdict: Wrong Answer)

#include <iostream>
#include <vector>
#include <algorithm>
#include <memory>
#include <cassert>
#include <set>

using namespace std;

struct MaxFlow{
  typedef long long ll;
  const ll INF = 1e18;
  struct Edge{
    int u,v;
    ll c,rc;
    shared_ptr<ll> flow;
    Edge(int _u, int _v, ll _c, ll _rc = 0):u(_u),v(_v),c(_c),rc(_rc){
    }
  };
  struct FlowTracker{
    ll cap, rcap;
    shared_ptr<ll> flow;
    bool dir;
    FlowTracker(ll _cap, ll _rcap, shared_ptr<ll> _flow, int _dir):cap(_cap),rcap(_rcap),flow(_flow),dir(_dir){ }
    ll rem() const {
      if(dir == 0){
        return cap-*flow;
      }
      else{
        return rcap+*flow;
      }
    }
    void add_flow(ll f){
      if(dir == 0)
        *flow += f;
      else
        *flow -= f;
      assert(*flow <= cap);
      assert(-*flow <= rcap);
    }
    operator ll() const { return rem(); }
    void operator-=(ll x){ add_flow(x); }
    void operator+=(ll x){ add_flow(-x); }
  };
  int source,sink;
  vector<vector<int> > adj;
  vector<vector<FlowTracker> > cap;
  vector<Edge> edges;
  MaxFlow(int _source, int _sink):source(_source),sink(_sink){
    assert(source != sink);
  }
  int add_edge(int u, int v, ll c, ll rc = 0){
    edges.push_back(Edge(u,v,c,rc));
    return edges.size()-1;
  }
  vector<int> now,lvl;
  void prep(){
    int max_id = max(source, sink);
    for(auto edge : edges)
      max_id = max(max_id, max(edge.u, edge.v));
    adj.resize(max_id+1);
    cap.resize(max_id+1);
    now.resize(max_id+1);
    lvl.resize(max_id+1);
    for(auto &edge : edges){
      auto flow = make_shared<ll>(0);
      adj[edge.u].push_back(edge.v);
      cap[edge.u].push_back(FlowTracker(edge.c, edge.rc, flow, 0));
      if(edge.u != edge.v){
        adj[edge.v].push_back(edge.u);
        cap[edge.v].push_back(FlowTracker(edge.c, edge.rc, flow, 1));
      }
      assert(cap[edge.u].back() == edge.c);
      edge.flow = flow;
    }
  }
  bool dinic_bfs(){
    fill(now.begin(),now.end(),0);
    fill(lvl.begin(),lvl.end(),0);
    lvl[source] = 1;
    vector<int> bfs(1,source);
    for(int i = 0; i < (int) bfs.size(); ++i){
      int u = bfs[i];
      for(int j = 0; j < (int) adj[u].size(); ++j){
        int v = adj[u][j];
        if(cap[u][j] > 0 && lvl[v] == 0){
          lvl[v] = lvl[u]+1;
          bfs.push_back(v);
        }
      }
    }
    return lvl[sink] > 0;
  }
  ll dinic_dfs(int u, ll flow){
    if(u == sink)
      return flow;
    while(now[u] < (int) adj[u].size()){
      int v = adj[u][now[u]];
      if(lvl[v] == lvl[u] + 1 && cap[u][now[u]] != 0){
        ll res = dinic_dfs(v,min(flow,(ll)cap[u][now[u]]));
        if(res > 0){
          cap[u][now[u]] -= res;
          return res;
        }
      }
      ++now[u];
    }
    return 0;
  }
  ll calc_max_flow(){
    prep();
    ll ans = 0;
    while(dinic_bfs()){
      ll cur = 0;
      do{
        cur = dinic_dfs(source,INF);
        ans += cur;
      }while(cur > 0);
    }
    return ans;
  }
  ll flow_on_edge(int edge_index){
    assert(edge_index < (int) edges.size());
    return *edges[edge_index].flow;
  }
};

// auto mf = MaxFlow(1,n);
// arguments source and sink, memory usage
// O(largest node index + input size), sink
// doesn't need to be last index
// int edge_index;
// for(int i = 0; i < m; ++i){
//    int a,b,c;
//    cin >> a >> b >> c;
//mf.add\_edge(a,b,c); // for directed edges
//      edge_index = mf.add_edge(a,b,c,c); // store edge index if care about flow value
//   }
//  cout << mf.calc_max_flow() << '\n';
//cout << mf.flow\_on\_edge(edge\_index) << endl; // return flow on this edge
//}

void failure () {
  cout << -1 << endl;
  exit(0);
}

const int MAX_N = 1e5 + 5;

vector<int> adj [MAX_N]; // adj[v] is the set of edgesets v is in

const int HYPER = 1e5 + 5;
const int SOURCE = 2e5 + 10;
const int SINK = SOURCE + 1;

int adder [MAX_N];
int match [MAX_N]; // match[e] is the v matching to edgeset e

int main () {
  ios::sync_with_stdio(false);

  int n;
  cin >> n;

  auto mf = MaxFlow(SOURCE, SINK);

  vector<pair<pair<int, int>, int>> edges; // <<vertex, edgeset>, mf idx>
  for (int i = 1; i < n; i++) {
    int cnt;
    cin >> cnt;

    for (int j = 0; j < cnt; j++) {
      int v;
      cin >> v;

      adj[v].push_back(i);
      int idx = mf.add_edge(v, HYPER + i, 1);
      edges.push_back({{v, i}, idx});
    }
  }

  for (int i = 1; i <= n; i++) {
    mf.add_edge(SOURCE, i, 1);
  }

  for (int i = 1; i < n; i++) {
    mf.add_edge(HYPER + i, SINK, 1);
  }

  int flow = mf.calc_max_flow();
  if (flow != n - 1) failure();
  
  set<int> freevs;
  for (int i = 1; i <= n; i++) {
    freevs.insert(i);
  }

  for (auto e : edges) {
    if (mf.flow_on_edge(e.second) == 1) {
      match[e.first.second] = e.first.first;
      freevs.erase(e.first.first);
    }
  }

  int start = *freevs.begin();

  set<int> esets;
  for (int e : adj[start]) {
    esets.insert(e);
    adder[e] = start;
  }

  vector<pair<int, int>> ans;
  for (int i = 0; i < n - 1; i++) {
    if (esets.empty()) failure();

    int E = *esets.begin();
    esets.erase(E);

    int v = match[E];
    for (int e : adj[v]) {
      if (adder[e] == 0) {
        esets.insert(e);
        adder[e] = v;
      }
    }

    ans.push_back({v, adder[E]});
  }

  for (auto pr : ans) {
    cout << pr.first << " " << pr.second << endl;
  }
}