#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define FOR(i, x, y) for (int i = x, _y = y; i <= _y; i++)
#define FOD(i, x, y) for (int i = x, _y = y; i >= _y; i--)
#define em emplace
#define emb emplace_back
#define emf emplace_front
#define fi first
#define se second
template<typename T1, typename T2> bool Tmax(T1 &a, const T2 &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template<typename T1, typename T2> bool Tmin(T1 &a, const T2 &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
const long long INF = (long long) 1e9;
const long long oo = (long long) 1e18;
const long long MOD = (long long) 1e9 + 7;
const char el = '\n';
const int Nmax = 3500 + 7;
const int Mmax = 500 + 7;
void File () {
ios_base::sync_with_stdio(NULL); cin.tie(NULL); cout.tie(NULL);
#define NAME "TEST"
if (fopen(NAME".inp", "r")) {
freopen(NAME".inp", "r", stdin);
freopen(NAME".out", "w", stdout);
}
#undef NAME
#define NAME "D:\\Onedrive\\Dokumen\\Code\\TEMPLATE_TEST\\Test"
if (fopen(NAME".inp", "r")) {
freopen(NAME".inp", "r", stdin);
freopen(NAME".out", "w", stdout);
}
}
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
#define rand rd
long long Rand (long long l, long long r) {
assert(l <= r);
return l + rd() % (r - l + 1ll);
}
int N, T;
int Pos[Nmax];
vector<int> Adj[Nmax];
bitset<Nmax> E[Nmax];
bool VoNghiem[Nmax];
signed main () {
File();
cin >> N >> T;
FOR(i, 1, N) {
int u, v; cin >> u >> v;
Adj[u].emb(v);
Adj[v].emb(u);
}
FOR(_, 1, T) {
FOR(i, 1, N) {
int x; cin >> x;
if (!x) E[i].flip(_ + N - 1);
}
}
FOR(i, 1, N) {
E[i].flip(i - 1);
for (int j : Adj[i]) E[i].flip(j - 1);
}
FOR(i, 1, N) {
int FirstPos = -1;
for (int j = 0; j <= N - 1; j++) {
if (E[i][j]) {
FirstPos = j;
break;
}
}
if (FirstPos == -1) {
VoNghiem[i] = 1;
}
else {
FOR(j, 1, N) {
if (i == j) continue;
if (E[j][FirstPos]) E[j] ^= E[i];
}
Pos[i] = FirstPos;
}
}
FOR(j, N, N + T - 1) {
bool Flag = 0;
vector<int> Ans;
Ans.clear();
FOR(i, 1, N) {
if (VoNghiem[i] && E[i][j]) {
Flag = 1;
break;
}
if (E[i][j]) Ans.emb(Pos[i] + 1);
}
if (Flag) cout << -1 << el;
else {
cout << Ans.size() << ' ';
for (int k : Ans) cout << k << ' ';
cout << el;
}
}
// vector<int> Ans;
// Ans.clear();
// FOR(i, 1, N) {
// if (E[i][N]) {
// FOR(j, 0, N - 1) if (E[i][j]) {
// Ans.emb(j + 1);
// break;
// }
// }
// }
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCnR5cGVkZWYgbG9uZyBsb25nIGxsOwp0eXBlZGVmIHBhaXI8aW50LCBpbnQ+IHBpaTsKdHlwZWRlZiBwYWlyPGxsLCBsbD4gcGxsOwoKI2RlZmluZSBGT1IoaSwgeCwgeSkgZm9yIChpbnQgaSA9IHgsIF95ID0geTsgaSA8PSBfeTsgaSsrKQojZGVmaW5lIEZPRChpLCB4LCB5KSBmb3IgKGludCBpID0geCwgX3kgPSB5OyBpID49IF95OyBpLS0pCiNkZWZpbmUgZW0gZW1wbGFjZQojZGVmaW5lIGVtYiBlbXBsYWNlX2JhY2sKI2RlZmluZSBlbWYgZW1wbGFjZV9mcm9udAojZGVmaW5lIGZpIGZpcnN0CiNkZWZpbmUgc2Ugc2Vjb25kCgp0ZW1wbGF0ZTx0eXBlbmFtZSBUMSwgdHlwZW5hbWUgVDI+IGJvb2wgVG1heChUMSAmYSwgY29uc3QgVDIgJmIpIHsKICAgIGlmIChhIDwgYikgewogICAgICAgIGEgPSBiOwogICAgICAgIHJldHVybiAxOwogICAgfQogICAgcmV0dXJuIDA7Cn0KCnRlbXBsYXRlPHR5cGVuYW1lIFQxLCB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JhbmQgcmQgCgpsb25nIGxvbmcgUmFuZCAobG9uZyBsb25nIGwsIGxvbmcgbG9uZyByKSB7CiAgICBhc3NlcnQobCA8PSByKTsKICAgIHJldHVybiBsICsgcmQoKSAlIChyIC0gbCArIDFsbCk7Cn0KCmludCBOLCBUOwppbnQgUG9zW05tYXhdOwp2ZWN0b3I8aW50PiBBZGpbTm1heF07CmJpdHNldDxObWF4PiBFW05tYXhdOwpib29sIFZvTmdoaWVtW05tYXhdOwoKc2lnbmVkIG1haW4gKCkgewogICAgRmlsZSgpOwoKICAgIGNpbiA+PiBOID4+IFQ7CiAgICBGT1IoaSwgMSwgTikgewogICAgICAgIGludCB1LCB2OyBjaW4gPj4gdSA+PiB2OwogICAgICAgIEFkalt1XS5lbWIodik7CiAgICAgICAgQWRqW3ZdLmVtYih1KTsKICAgIH0KCiAgICBGT1IoXywgMSwgVCkgewogICAgICAgIEZPUihpLCAxLCBOKSB7CiAgICAgICAgICAgIGludCB4OyBjaW4gPj4geDsKICAgICAgICAgICAgaWYgKCF4KSBFW2ldLmZsaXAoXyArIE4gLSAxKTsKICAgICAgICB9CiAgICB9CgogICAgRk9SKGksIDEsIE4pIHsKICAgICAgICBFW2ldLmZsaXAoaSAtIDEpOwogICAgICAgIGZvciAoaW50IGogOiBBZGpbaV0pIEVbaV0uZmxpcChqIC0gMSk7CiAgICB9CgogICAgRk9SKGksIDEsIE4pIHsKICAgICAgICBpbnQgRmlyc3RQb3MgPSAtMTsKICAgICAgICBmb3IgKGludCBqID0gMDsgaiA8PSBOIC0gMTsgaisrKSB7CiAgICAgICAgICAgIGlmIChFW2ldW2pdKSB7CiAgICAgICAgICAgICAgICBGaXJzdFBvcyA9IGo7CiAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgfQogICAgICAgIH0KCiAgICAgICAgaWYgKEZpcnN0UG9zID09IC0xKSB7CiAgICAgICAgICAgIFZvTmdoaWVtW2ldID0gMTsKICAgICAgICB9IAogICAgICAgIGVsc2UgewogICAgICAgICAgICBGT1IoaiwgMSwgTikgewogICAgICAgICAgICAgICAgaWYgKGkgPT0gaikgY29udGludWU7CiAgICAgICAgICAgICAgICBpZiAoRVtqXVtGaXJzdFBvc10pIEVbal0gXj0gRVtpXTsKICAgICAgICAgICAgfQogICAgICAgICAgICBQb3NbaV0gPSBGaXJzdFBvczsKICAgICAgICB9CiAgICB9CgogICAgRk9SKGosIE4sIE4gKyBUIC0gMSkgewogICAgICAgIGJvb2wgRmxhZyA9IDA7CiAgICAgICAgdmVjdG9yPGludD4gQW5zOwogICAgICAgIEFucy5jbGVhcigpOwogICAgICAgIEZPUihpLCAxLCBOKSB7CiAgICAgICAgICAgIGlmIChWb05naGllbVtpXSAmJiBFW2ldW2pdKSB7CiAgICAgICAgICAgICAgICBGbGFnID0gMTsKICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmIChFW2ldW2pdKSBBbnMuZW1iKFBvc1tpXSArIDEpOwogICAgICAgIH0KICAgICAgICBpZiAoRmxhZykgY291dCA8PCAtMSA8PCBlbDsKICAgICAgICBlbHNlIHsKICAgICAgICAgICAgY291dCA8PCBBbnMuc2l6ZSgpIDw8ICcgJzsKICAgICAgICAgICAgZm9yIChpbnQgayA6IEFucykgY291dCA8PCBrIDw8ICcgJzsKICAgICAgICAgICAgY291dCA8PCBlbDsKICAgICAgICB9CiAgICB9CgoKCiAgICAvLyB2ZWN0b3I8aW50PiBBbnM7CiAgICAvLyBBbnMuY2xlYXIoKTsKCiAgICAvLyBGT1IoaSwgMSwgTikgewogICAgLy8gICAgIGlmIChFW2ldW05dKSB7CiAgICAvLyAgICAgICAgIEZPUihqLCAwLCBOIC0gMSkgaWYgKEVbaV1bal0pIHsKICAgIC8vICAgICAgICAgICAgIEFucy5lbWIoaiArIDEpOwogICAgLy8gICAgICAgICAgICAgYnJlYWs7CiAgICAvLyAgICAgICAgIH0KICAgIC8vICAgICB9IAogICAgLy8gfQoKICAgIHJldHVybiAwOwp9Cg==