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
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#include<bits/stdc++.h>
using namespace std;
using LL=long long;
#define FOR(i,l,r) for(int i=(l);i<=(r);++i)
#define REP(i,n) FOR(i,0,(n)-1)
#define ssize(x) int(x.size())
template<class A,class B>auto&operator<<(ostream&o,pair<A,B>p){return o<<'('<<p.first<<", "<<p.second<<')';}
template<class T>auto operator<<(ostream&o,T x)->decltype(x.end(),o){o<<'{';int i=0;for(auto e:x)o<<(", ")+2*!i++<<e;return o<<'}';}
#ifdef DEBUG
#define debug(x...) cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...);}(x),cerr<<'\n'
#else
#define debug(...) {}
#endif


/*
 * plan rozgrywki:
 * robimy templatowaną funkcję solve, która używa templatowanego typu BS<T>
 * i przez to mamy tylko jeden kod a kompilator nam ustawia rozmiary bitsetów
 * :essa:
 * UWAŻAJ NA ROZMIAR VISITED_CANDIDATES I INNE RZECZY ROZMIARU N, A NIE M
 */

mt19937 rng(3124289);
int rd(int a, int b) {
    return int(rng() % (b - a + 1) + a);
};

template<size_t LIMIT>
void solve(tuple<int, int, int> data) {
    using BS = bitset<LIMIT>;

    const int limit = LIMIT;

    const auto [n, m, k] = data;

    vector<int> permutation(n);
    iota(permutation.begin(), permutation.end(), 0);
    shuffle(permutation.begin(), permutation.end(), rng);
    vector<int> inverse_permutation(n);
    REP(i, n)
        inverse_permutation[permutation[i]] = i;

    vector p(m, vector(2, 0));
    REP(i, m) {
        REP(j, 2) {
            cin >> p[i][j];
            --p[i][j];
            p[i][j] = permutation[p[i][j]];
        }
    }
    debug(p);



    vector<int> final_ans(n);

    vector<BS> sets(n);
    vector<BS> neg_sets(n);
    REP(i, n) {
        REP(j, m) {
            if (i == p[j][0] or i == p[j][1]) {
                sets[i][j] = true;
            }
        }
        neg_sets[i] = ~sets[i];
    }

    vector<pair<int, int>> covers(n);
    REP(i, n)
        covers[i] = {int(sets[i].count()), i};
    sort(covers.rbegin(), covers.rend());
    REP(i, n + k) {
        covers.emplace_back(0, -1);
    }
    const int spref = ssize(covers) + 1;
    vector<int> pref(spref);
    REP(i, spref - 1)
        pref[i + 1] = pref[i] + covers[i].first;
    debug(covers, pref);



    REP(i, n) {
        int ans = k;
        function<void(BS, int)> rec = [&](const BS& v, int used) {
            if (used >= ans) {
                return;
            }
            if (v.none()) {
                ans = min(ans, used);
                return;
            }
            {
                const int vcount = int(v.count());
                const int left = ans - 1 - used;
                int bound = pref[left];
                const int reps = min(left + used, n);
                int next = left;
                REP(j, reps) {
                    if (bound < vcount) {
                        return;
                    }
                    if ((v & sets[covers[j].second]).none()) {
                        bound -= covers[j].first;
                        bound += covers[next++].first;
                    }
                }
                if (bound < vcount) {
                    return;
                }
            }


            auto get_forced_sets = [&](int id, const BS& forced_bits) {
                vector<int> forced_sets;
                forced_sets.reserve(10);
                for (int j = int(forced_bits._Find_next(-1));
                        j < limit;
                        j = int(forced_bits._Find_next(j))) {
                    if (p[j][0] == id)
                        forced_sets.emplace_back(p[j][1]);
                    else
                        forced_sets.emplace_back(p[j][0]);
                }
                sort(forced_sets.begin(), forced_sets.end());
                forced_sets.erase(unique(forced_sets.begin(), forced_sets.end()), forced_sets.end());
                return forced_sets;
            };
            auto evaluate_candidate = [&](int x) -> pair<int, int> { // {score, id}
                pair<int, int> best = {0, 0};
                REP(l, 2) {
                    const int id = p[x][l];
                    const BS forced_bits = v & sets[id];
                    const auto forced_sets = get_forced_sets(id, forced_bits);
                    best = max(best, pair(ssize(forced_sets), id));
                }
                return best;
            };

            int y = -1;
            pair<int, int> best;

            const int first_reps_limit = 1;
            const int first_reps = min(first_reps_limit, n);
            REP(i, first_reps) {
                const BS common = v & sets[covers[i].second];
                if (not common.none()) {
                    y = common._Find_next(-1);
                    best = evaluate_candidate(y);
                    break;
                }
            }
            if (y == -1) {
                y = int(v._Find_next(-1));
                best = evaluate_candidate(y);
            }

            const int reps_limit = 5;
            const int left = ans - 1 - used;
            const int reps = min(left - 1, reps_limit);
            vector<int> considered = {y};
            considered.reserve(reps + 1);
            auto is_considered = [&](int x) {
                for (const auto e : considered)
                    if (e == x)
                        return true;
                return false;
            };
            REP(j, reps) {
                if (best.first != 2)
                    break;
                const int z = int(v._Find_next(rd(0, m / 2)));
                if (z < limit and not is_considered(z)) {
                    considered.emplace_back(z);
                    best = max(best, evaluate_candidate(z),
                        [](pair<int, int> a, pair<int, int> b) {
                            if (a.first == 1)
                                return false;
                            if (b.first == 1)
                                return true;
                            return a.first < b.first;
                        });
                }
            }

            const auto [score, id] = best;
            debug("best", score, id);

            rec(v & neg_sets[id], used + 1);
            if (score > 1) {
                BS next_v = v;
                for (int j : get_forced_sets(id, v & sets[id])) {
                    next_v &= neg_sets[j];
                }
                rec(next_v, used + score);
            }
        };


        BS to_be_reduced;
        REP(j, m) {
            if (i != p[j][0] and i != p[j][1]) {
                to_be_reduced[j] = true;
            }
        }
        rec(to_be_reduced, 0);
        final_ans[i] = ans + 1;
    }
    debug(final_ans);



    vector<pair<int, int>> v;
    REP(i, n) {
        v.emplace_back(final_ans[i], inverse_permutation[i]);
    }
    int ans = k + 1;
    for (auto [a, b] : v) {
        ans = min(ans, a);
    }
    if (ans > k) {
        cout << -1 << '\n';
    }
    else {
        vector<int> vans;
        for (auto [a, b] : v) {
            if (a == ans) {
                vans.emplace_back(b);
            }
        }
        sort(vans.begin(), vans.end());
        cout << ans << ' ' << ssize(vans) << '\n';
        for (auto e : vans) {
            cout << e + 1 << ' ';
        }
        cout << '\n';
    }
}

int main() {
    cin.tie(0)->sync_with_stdio(0);

    int tt;
    cin >> tt;
    debug(tt);

    REP(ttt, tt) {
        int n, m, k;
        cin >> n >> m >> k;
        debug(n, m, k);

        const auto data = tuple(n, m, k);

        constexpr int limit = 3000;

        constexpr int s1 = 64;
        constexpr int s2 = 128;
        constexpr int s3 = 192;
        constexpr int s4 = 256;
        constexpr int s5 = 384;
        constexpr int s6 = 512;
        constexpr int s7 = 768;
        constexpr int s8 = 1024;
        constexpr int s9 = 1344;
        constexpr int s10 = 1664;
        constexpr int s11 = 2048;
        constexpr int s12 = 2368;
        constexpr int s13 = 2688;

        if (m <= s1) {
            solve<s1>(data);
        }
        else if (m <= s2) {
            solve<s2>(data);
        }
        else if (m <= s3) {
            solve<s3>(data);
        }
        else if (m <= s4) {
            solve<s4>(data);
        }
        else if (m <= s5) {
            solve<s5>(data);
        }
        else if (m <= s6) {
            solve<s6>(data);
        }
        else if (m <= s7) {
            solve<s7>(data);
        }
        else if (m <= s8) {
            solve<s8>(data);
        }
        else if (m <= s9) {
            solve<s9>(data);
        }
        else if (m <= s10) {
            solve<s10>(data);
        }
        else if (m <= s11) {
            solve<s11>(data);
        }
        else if (m <= s12) {
            solve<s12>(data);
        }
        else if (m <= s13) {
            solve<s13>(data);
        }
        else {
            solve<limit>(data);
        }
    }
}