#include using namespace std; typedef long long ll; typedef long double ld; #define inf 1000000007 #define MP make_pair #define MT make_tuple #define PB push_back #define fi first #define se second #define rep(i,n) for(int i = 0; i < (int)(n); ++i) #define rrep(i,n) for(int i = (int)n-1; i >= 0; --i) #define srep(i,a,b) for(int i = (int)a; i < (int)(b); ++i) #define all(x) (x).begin(),(x).end() #define SUM(v) accumulate(all(v), 0LL) #define MIN(v) *min_element(all(v)) #define MAX(v) *max_element(all(v)) #define lb(c, x) distance((c).begin(), lower_bound(all(c), (x))) #define ub(c, x) distance((c).begin(), upper_bound(all(c), (x))) #define UNIQUE(x) sort(all(x)), x.erase(unique(all(x)), x.end()) #define SZ(c) (int)(c).size() template ostream& operator << (ostream& os, vector& vec) { os << "{"; for (int i = 0; i<(int)vec.size(); i++) { os << vec[i] << (i + 1 == (int)vec.size() ? "" : ", "); } os << "}"; return os; } // pair出力 template ostream& operator << (ostream& os, pair pair_var) { os << "(" << pair_var.first << ", " << pair_var.second << ")"; return os; } // map出力 template ostream& operator << (ostream& os, map& map_var) { os << "{"; for(auto itr = map_var.begin(); itr != map_var.end(); itr++){ os << "(" << itr->first << ", " << itr->second << ")"; itr++; if(itr != map_var.end()) os << ", "; itr--; } os << "}"; return os; } // set 出力 template ostream& operator << (ostream& os, set& set_var) { os << "{"; for(auto itr = set_var.begin(); itr != set_var.end(); itr++){ os << (*itr); ++itr; if(itr != set_var.end()) os << ", "; itr--; } os << "}"; return os; } #define overload2(_1, _2, name, ...) name #define vec(type, name, ...) vector name(__VA_ARGS__) #define VEC(type, name, size) \ vector name(size); \ IN(name) #define vv(type, name, h, ...) vector> name(h, vector(__VA_ARGS__)) #define VV(type, name, h, w) \ vector> name(h, vector(w)); \ IN(name) #define vvv(type, name, h, w, ...) vector>> name(h, vector>(w, vector(__VA_ARGS__))) #define vvvv(type, name, a, b, c, ...) \ vector>>> name(a, vector>>(b, vector>(c, vector(__VA_ARGS__)))) #define INT(...) \ int __VA_ARGS__; \ IN(__VA_ARGS__) #define LL(...) \ ll __VA_ARGS__; \ IN(__VA_ARGS__) #define STR(...) \ string __VA_ARGS__; \ IN(__VA_ARGS__) #define CHR(...) \ char __VA_ARGS__; \ IN(__VA_ARGS__) #define DBL(...) \ double __VA_ARGS__; \ IN(__VA_ARGS__) int scan() { return getchar(); } void scan(int &a) { cin >> a; } void scan(long long &a) { cin >> a; } void scan(char &a) { cin >> a; } void scan(double &a) { cin >> a; } void scan(string &a) { cin >> a; } template void scan(pair &p) { scan(p.first), scan(p.second); } template void scan(vector &); template void scan(vector &a) { for(auto &i : a) scan(i); } template void scan(T &a) { cin >> a; } void IN() {} template void IN(Head &head, Tail &...tail) { scan(head); IN(tail...); } const string YESNO[2] = {"NO", "YES"}; const string YesNo[2] = {"No", "Yes"}; const string yesno[2] = {"no", "yes"}; void YES(bool t = 1) { cout << YESNO[t] << endl; } void NO(bool t = 1) { YES(!t); } void Yes(bool t = 1) { cout << YesNo[t] << endl; } void No(bool t = 1) { Yes(!t); } void yes(bool t = 1) { cout << yesno[t] << endl; } void no(bool t = 1) { yes(!t); } #ifdef LOCAL void debug_out() { cerr << endl; } template void debug_out(Head H, Tail... T) { cerr << " " << H; debug_out(T...); } #define dbg(...) \ cerr << __LINE__ << " [" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl #else #define dbg(...) (void(0)) #define dump(x) (void(0)) #endif template std::enable_if_t::value> fill(A& array, const T& val) { array = val; } template std::enable_if_t::value> fill(A& array, const T& val) { for (auto& a : array) { fill(a, val); } } template T ceil(T x, T y) {assert(y >= 1);return (x > 0 ? (x + y - 1) / y : x / y);} template T floor(T x, T y) {assert(y >= 1);return (x > 0 ? x / y : (x + y - 1) / y);} vector iota(int n) {vector a(n);iota(all(a), 0);return a;} template T POW(T x, int n) {T res = 1;for(; n; n >>= 1, x *= x){if(n & 1) res *= x;}return res;} ll pow2(int i) { return 1LL << i; } int topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); } int topbit(ll t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); } int lowbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); } int lowbit(ll a) { return a == 0 ? 64 : __builtin_ctzll(a); } // int allbit(int n) { return (1 << n) - 1; } ll allbit(ll n) { return (1LL << n) - 1; } int popcount(signed t) { return __builtin_popcount(t); } int popcount(ll t) { return __builtin_popcountll(t); } bool ispow2(int i) { return i && (i & -i) == i; } template void fold_in(vector &v) {} template void fold_in(vector &v, Head &&a, Tail &&...tail) { for(auto e : a) v.emplace_back(e); fold_in(v, tail...); } template void renumber(vector &v) {} template void renumber(vector &v, Head &&a, Tail &&...tail) { for(auto &&e : a) e = lb(v, e); renumber(v, tail...); } template void zip(vector &head, Args &&...args) { vector v; fold_in(v, head, args...); sort(all(v)), v.erase(unique(all(v)), v.end()); renumber(v, head, args...); } template inline bool chmax(T &a, T b){ if(a inline bool chmin(T &a, T b){ if(a>b){ a = b; return true; } return false; } template class UnorderedMapIterator; template, bool DOWNSIZE = false> class UnorderedMap { private: using iterator = UnorderedMapIterator<_Key, _Tp, _Hash, DOWNSIZE>; using value_type = _Tp; using data_type = pair<_Key, _Tp>; using aligned_pointer = typename aligned_storage::type; friend UnorderedMapIterator<_Key, _Tp, _Hash, DOWNSIZE>; struct bucket { _Key _key; short int _dist; bool _last, _end; aligned_pointer _value_ptr; bucket() noexcept : _dist(-1), _last(false), _end(false){} bucket& operator=(const bucket& another) noexcept { _key = another._key, _dist = another._dist, _last = another._last, _end = another._end; if(!another.empty()){ new(&_value_ptr) value_type(*reinterpret_cast(&another._value_ptr)); } return *this; } ~bucket(){ if(!empty()) _delete(); } inline void clear() noexcept { _dist = -1; } inline void _delete(){ _dist = -1, value_ptr()->~value_type(); } inline bool empty() const noexcept { return (_dist == -1); } inline value_type& value() noexcept { return *reinterpret_cast(&_value_ptr); } inline value_type* value_ptr() noexcept { return reinterpret_cast(&_value_ptr); } inline void new_value(value_type&& value){ new(&_value_ptr) value_type(move(value)); } }; inline static unsigned int ceilpow2(unsigned int u) noexcept { if(u == 0u) return 0u; --u, u |= u >> 1, u |= u >> 2, u |= u >> 4, u |= u >> 8; return (u | (u >> 16)) + 1u; } inline static bucket *increment(bucket *cur) noexcept { for(++cur; !cur->_end; ++cur){ if(!cur->empty()) break; } return cur; } inline bucket *next_bucket(bucket *cur) const noexcept { return cur->_last ? _buckets : cur + 1; } inline unsigned int make_hash(const _Key& key) const noexcept { return _Hash()(key); } inline float load_rate() const noexcept { return (float)_data_count / _bucket_count; } bucket *insert(bucket *cur, _Key&& key, short int dist, value_type&& value){ bucket *ret = cur; bool flag = false; while(true){ if(cur->empty()){ cur->_key = move(key), cur->_dist = dist, cur->new_value(move(value)); if(!flag) ret = cur, flag = true; break; }else if(dist > cur->_dist){ swap(key, cur->_key), swap(dist, cur->_dist), swap(value, cur->value()); if(!flag) ret = cur, flag = true; } ++dist; cur = next_bucket(cur); } return ret; } template bucket *_find(Key&& key, bool push = false){ unsigned int hash = make_hash(key); bucket *cur = _buckets + (hash & _mask); short int dist = 0; while(dist <= cur->_dist){ if(key == cur->_key) return cur; ++dist, cur = next_bucket(cur); } if(!push) return _buckets + _bucket_count; ++_data_count; if(rehash_check()){ cur = _buckets + (hash & _mask), dist = 0; } value_type new_value = value_type(); _Key new_key = forward(key); return insert(cur, move(new_key), dist, move(new_value)); } template bucket *find_insert(Data&& data){ const _Key& key = data.first; unsigned int hash = make_hash(key); bucket *cur = _buckets + (hash & _mask); short int dist = 0; while(dist <= cur->_dist){ if(key == cur->_key) return cur; ++dist, cur = next_bucket(cur); } ++_data_count; if(rehash_check()){ cur = _buckets + (hash & _mask), dist = 0; } data_type new_data = forward(data); return insert(cur, move(new_data.first), dist, move(new_data.second)); } template bucket *emplace(Args&&... args){ return find_insert(data_type(forward(args)...)); } bucket *backward_shift(bucket *cur, bool next_ret){ bucket *next = next_bucket(cur), *ret = cur; if(next->_dist < 1) return next_ret ? increment(cur) : cur; do { cur->_key = next->_key, cur->_dist = next->_dist - 1; cur->new_value(move(next->value())); cur = next, next = next_bucket(cur); }while(next->_dist >= 1); cur->clear(); return ret; } bucket *erase_impl(bucket *cur, bool next_ret){ assert(static_cast(cur - _buckets) != _bucket_count); cur->_delete(); --_data_count; return backward_shift(cur, next_ret); } bucket *erase_itr(bucket *cur, bool next_ret = true){ const _Key key = cur->_key; return erase_impl(rehash_check() ? _find(key) : cur, next_ret); } size_t erase_key(const _Key& key){ rehash_check(); bucket *cur = _find(key); if(static_cast(cur - _buckets) == _bucket_count){ return 0; }else{ erase_impl(_find(key), false); return 1; } } bool rehash_check(){ if(_bucket_count == 0){ rehash(1u); return true; }else if(load_rate() >= MAX_LOAD_RATE){ rehash(_bucket_count * 2u); return true; }else if(DOWNSIZE){ if(load_rate() <= MIN_LOAD_RATE && _bucket_count >= DOWNSIZE_THRESHOLD){ rehash(_bucket_count / 2u); return true; } } return false; } void move_data(bucket *cur){ insert(_buckets + (make_hash(cur->_key) & _mask), move(cur->_key), 0, move(cur->value())); } void rehash(unsigned int new_bucket_count){ UnorderedMap new_unordered_map(new_bucket_count); new_unordered_map._data_count = _data_count; for(bucket *cur = _buckets; !cur->_end; ++cur){ if(!cur->empty()){ new_unordered_map.move_data(cur); } } swap(*this, new_unordered_map); } friend void swap(UnorderedMap& ump1, UnorderedMap& ump2){ swap(ump1._bucket_count, ump2._bucket_count); swap(ump1._mask, ump2._mask); swap(ump1._data_count, ump2._data_count); swap(ump1._buckets, ump2._buckets); } private: unsigned int _bucket_count, _mask, _data_count; bucket *_buckets; public: const float MAX_LOAD_RATE = 0.5f; const float MIN_LOAD_RATE = 0.1f; const unsigned int DOWNSIZE_THRESHOLD = 16u; UnorderedMap(unsigned int bucket_size = 0u) : _bucket_count(ceilpow2(bucket_size)), _mask(_bucket_count - 1), _data_count(0u), _buckets(new bucket[_bucket_count + 1]){ if(_bucket_count > 0) _buckets[_bucket_count - 1]._last = true; else _mask = 0; _buckets[_bucket_count]._end = true; } UnorderedMap(const UnorderedMap& another) : _bucket_count(another._bucket_count), _mask(another._mask), _data_count(another._data_count){ _buckets = new bucket[_bucket_count + 1u]; for(unsigned int i = 0u; i <= _bucket_count; ++i){ _buckets[i] = another._buckets[i]; } } UnorderedMap(UnorderedMap&& another) : _bucket_count(move(another._bucket_count)), _mask(move(another._mask)), _data_count(move(another._data_count)), _buckets(another._buckets){ another._buckets = nullptr; } UnorderedMap& operator=(const UnorderedMap& another){ delete[] _buckets; _bucket_count = another._bucket_count; _mask = another._mask; _data_count = another._data_count; _buckets = new bucket[_bucket_count + 1u]; for(unsigned int i = 0u; i <= _bucket_count; ++i){ _buckets[i] = another._buckets[i]; } return *this; } UnorderedMap& operator=(UnorderedMap&& another){ delete[] _buckets; _bucket_count = move(another._bucket_count); _mask = move(another._mask); _data_count = move(another._data_count); _buckets = another._buckets; another._buckets = nullptr; return *this; } void allocate(unsigned int element_size){ rehash(ceilpow2(ceil(element_size / MAX_LOAD_RATE) + 1)); } ~UnorderedMap(){ delete[] _buckets; } friend ostream& operator<< (ostream& os, UnorderedMap& ump) noexcept { for(auto val : ump) os << '{' << val.first << ',' << val.second << "} "; return os; } _Tp& operator[](const _Key& key){ return _find(key, true)->value(); } _Tp& operator[](_Key&& key){ return _find(move(key), true)->value(); } const _Tp& at(const _Key& key){ bucket *res = _find(key); if(res == _buckets + _bucket_count) __throw_out_of_range("Unordered_Map::at"); return res->value(); } void clear(){ UnorderedMap new_unordered_map(0u); swap(*this, new_unordered_map); } size_t size() const noexcept { return _data_count; } size_t bucket_count() const noexcept { return _bucket_count; } bool empty() const noexcept { return (_data_count == 0); } iterator begin() noexcept { return (_buckets->empty() && _bucket_count > 0) ? iterator(increment(_buckets)) : iterator(_buckets); } iterator end() noexcept { return iterator(_buckets + _bucket_count); } iterator find(const _Key& key){ return iterator(_find(key)); } iterator insert(const data_type& data){ return iterator(find_insert(data)); } iterator insert(data_type&& data){ return iterator(find_insert(move(data))); } template iterator emplace(Args&&... args){ return iterator(_emplace(forward(args)...)); } size_t erase(const _Key& key){ return erase_key(key); } iterator erase(const iterator& itr){ return iterator(erase_itr(itr.bucket_ptr)); } void simple_erase(const _Key& key){ erase_key(key); } void simple_erase(const iterator& itr){ erase_itr(itr.bucket_ptr, false); } // DEBUG 用 short int maximum_distance() const noexcept { short int ret = -1; for(bucket *cur = _buckets; !cur->_end; ++cur){ ret = max(ret, cur->_dist); } return ret; } }; template class UnorderedMapIterator { private: friend UnorderedMap<_Key, _Tp, _Hash, DOWNSIZE>; typename UnorderedMap<_Key, _Tp, _Hash, DOWNSIZE>::bucket *bucket_ptr; using iterator_category = forward_iterator_tag; using value_type = pair; using difference_type = ptrdiff_t; using reference = pair; private: UnorderedMapIterator(typename UnorderedMap<_Key, _Tp, _Hash, DOWNSIZE>::bucket *_bucket_ptr) noexcept : bucket_ptr(_bucket_ptr){} public: UnorderedMapIterator() noexcept : bucket_ptr(){} UnorderedMapIterator(const UnorderedMapIterator& itr) noexcept : bucket_ptr(itr.bucket_ptr){} UnorderedMapIterator& operator=(const UnorderedMapIterator& itr) & noexcept { return bucket_ptr = itr.bucket_ptr, *this; } UnorderedMapIterator& operator=(const UnorderedMapIterator&& itr) & noexcept { return bucket_ptr = itr.bucket_ptr, *this; } reference operator*() const noexcept { return {bucket_ptr->_key, bucket_ptr->value()}; } UnorderedMapIterator& operator++() noexcept { return bucket_ptr = UnorderedMap<_Key, _Tp, _Hash, DOWNSIZE>::increment(bucket_ptr), *this; } UnorderedMapIterator operator++(int) const noexcept { return UnorderedMapIterator(UnorderedMap<_Key, _Tp, _Hash, DOWNSIZE>::increment(this->bucket_ptr)); } bool operator==(const UnorderedMapIterator& itr) const noexcept { return !(*this != itr); }; bool operator!=(const UnorderedMapIterator& itr) const noexcept { return bucket_ptr != itr.bucket_ptr; } }; int main(){ INT(n); vector > > a; vector num(n,1); UnorderedMap mx; rep(i,n){ INT(m); vector > q; rep(j,m){ INT(p,e); chmax(mx[p],e); q.push_back({p,e}); rep(zz,e)num[i] *= p; } a.push_back(q); } vector pr; for(auto [x,c]:mx){ pr.push_back(x); } reverse(all(pr)); ll K = 1; ll cc = 0; vector ans; for(auto x:pr){ cc++; K *=x; ans.push_back(x); if(K>=1000000000){ break; } } auto answer = [&]()->vector >{ vector > r; vector flag(n); dbg(ans); rep(z,ans.size()){ vector ttt; rep(i,n){ bool fff = 1; if(flag[i])continue; for(auto [p,e]:a[i]){ if(ans[z]==p){ if(mx[p]==e)fff = 0; } } if(fff){ ttt.push_back(i); flag[i] = 1; } } r.push_back(ttt); } rep(i,n){ if(!flag[i]){ return vector>(); } } return r; }; if(cc==2){ vector > res = answer(); if(res.size()==0){ cout << -1 << endl; return 0; } cout << res.size() << "\n"; for(auto&v:res){ cout << v.size() << " "; for(auto x:v){ cout << num[x] << " "; } cout << "\n"; } return 0; } UnorderedMap mp; vector > s(n); rep(i,n){ vector t; for(auto [p,e]:a[i]){ if(mx[p]==e){ t.push_back(p); } } if(t.size()==mx.size()){ cout << -1 << endl; return 0; } s[i] = t; int N = t.size(); vectorpp(1< tmp; auto dfs = [&](auto&&f,int X,int s,int id)->void{ if(s==c){ if(mp[X]==0){ ok = 1; ans = tmp; } return; } if(id==pr.size())return; int p = pr[id]; tmp.push_back(p); f(f,X*p,s+1,id+1); if(ok)return; tmp.pop_back(); f(f,X,s,id+1); }; dfs(dfs,1,0,0); if(ok){ vector > res = answer(); if(res.size()==0){ cerr<< "error" << endl; cout << -1 << endl; return 0; } cout << res.size() << "\n"; for(auto&v:res){ cout << v.size() << " "; for(auto x:v){ cout << num[x] << " "; } cout << "\n"; } return 0; } } vector > res = answer(); if(res.size()==0){ cout << -1 << endl; return 0; } cout << res.size() << "\n"; for(auto&v:res){ cout << v.size() << " "; for(auto x:v){ cout << num[x] << " "; } cout << "\n"; } return 0; }