mod util { pub trait Join { fn join(self, sep: &str) -> String; } impl Join for I where I: Iterator, T: std::fmt::Display, { fn join(self, sep: &str) -> String { let mut s = String::new(); use std::fmt::*; for (i, v) in self.enumerate() { if i > 0 { write!(&mut s, "{}", sep).ok(); } write!(&mut s, "{}", v).ok(); } s } } } // ---------- begin scannner ---------- #[allow(dead_code)] mod scanner { use std::str::FromStr; pub struct Scanner<'a> { it: std::str::SplitWhitespace<'a>, } impl<'a> Scanner<'a> { pub fn new(s: &'a String) -> Scanner<'a> { Scanner { it: s.split_whitespace(), } } pub fn next(&mut self) -> T { self.it.next().unwrap().parse::().ok().unwrap() } pub fn next_bytes(&mut self) -> Vec { self.it.next().unwrap().bytes().collect() } pub fn next_chars(&mut self) -> Vec { self.it.next().unwrap().chars().collect() } pub fn next_vec(&mut self, len: usize) -> Vec { (0..len).map(|_| self.next()).collect() } } } // ---------- end scannner ---------- use std::collections::*; use std::io::Write; type Map = BTreeMap; type Set = BTreeSet; type Deque = VecDeque; fn main() { use std::io::Read; let mut s = String::new(); std::io::stdin().read_to_string(&mut s).unwrap(); let mut sc = scanner::Scanner::new(&s); let out = std::io::stdout(); let mut out = std::io::BufWriter::new(out.lock()); run(&mut sc, &mut out); } fn run(sc: &mut scanner::Scanner, out: &mut std::io::BufWriter) { let t: u32 = sc.next(); for _ in 0..t { let n: usize = sc.next(); let mut e = vec![vec![]; n]; for e in e.iter_mut() { let m = sc.next::(); e.resize(m, 0usize); for e in e.iter_mut() { *e = sc.next::() - 1; } } let (q, ans) = solve(e); writeln!(out, "{}", q).ok(); for (f, a) in ans { use util::*; writeln!(out, "{} {}", f + 1, a.iter().map(|a| *a + 1).join(" ")).ok(); } } } fn solve(e: Vec>) -> (usize, Vec<(usize, Vec)>) { let n = e.len(); let mut list = vec![vec![]; n - 1]; for (i, e) in e.iter().enumerate() { for e in e.iter() { list[*e].push(i); } } let deg = e.iter().map(|e| e.len()).collect::>(); let mut q = (1..).find(|&p| p * p >= 2 * (n - 1)).unwrap().max(*deg.iter().max().unwrap()); if greedy(°, q).is_none() { q += 1; } assert!(greedy(°, q).is_some()); (0, vec![]) } fn greedy(a: &[usize], q: usize) -> Option> { let n = a.len(); assert!(a.iter().sum::() == 2 * (n - 1)); let mut rem = vec![q; q]; let mut h = std::collections::BinaryHeap::new(); for i in 0..q { h.push((q, i)); } let mut ans = vec![0; n]; let mut ord = (0..n).collect::>(); ord.sort_by_key(|v| a[*v]); for x in ord.into_iter().rev() { let a = a[x]; let ans = &mut ans[x]; let (_, k) = h.pop().unwrap(); if rem[k] < a { return None; } *ans = k; rem[k] -= a; h.push((rem[k], k)); } Some(ans) }