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
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <stack>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;
    while (t--) {
        int n, m, k;
        cin >> n >> m >> k;

        // input
        vector<pair<int,int>>Pairs(m);
        for (auto& p : Pairs) {
            cin >> p.first >> p.second;
            p.first--;
            p.second--;
            if (p.first > p.second)
                swap(p.first, p.second);
        }
        sort(Pairs.begin(), Pairs.end());
        Pairs.erase(unique(Pairs.begin(), Pairs.end()), Pairs.end());

        // transform to the graph
        vector<vector<int>>G(n);
        vector<int>Degs(n);
        for (auto& p : Pairs) {
            G[p.first].push_back(p.second);
            Degs[p.first]++;
            G[p.second].push_back(p.first);
            Degs[p.second]++;
        }

        // perform vertex cover on it
        vector<bool>InCover(n);
        vector<bool>CanBeInGoodCover(n);

        auto report_solution = [&](int k_used) {
            if (k_used < k) {
                k = k_used;
                fill(CanBeInGoodCover.begin(), CanBeInGoodCover.end(), false);
            }
        };

        auto vertex_cover_small_deg = [&](int k_used) -> int {
            vector<bool>LocalInCover(InCover);
            vector<int>ToDel;
            int result = 0;

            // paths
            for (int i = 0; i < n; i++)
                if (!LocalInCover[i] && Degs[i] == 1) {
                    vector<int>Path;
                    for (int v = i;;) {
                        LocalInCover[v] = true;
                        Path.push_back(v);
                        int new_v = -1;
                        for (int nei : G[v])
                            if (!LocalInCover[nei]) {
                                new_v = nei;
                                break;
                            }
                        if (new_v == -1)
                            break;
                        else
                            v = new_v;
                    }
                    result += Path.size() / 2;
                    if (result + k_used > k)
                        return numeric_limits<int>::max();
                    if (Path.size() % 2 == 0)
                        for (int x : Path)
                            ToDel.push_back(x);
                    else
                        for (int i = 1; i < (int)Path.size(); i+=2)
                            ToDel.push_back(Path[i]);
                }

            // cycles
            for (int i = 0; i < n; i++)
                if (!LocalInCover[i] && Degs[i] == 2) {
                    int cycle_len = 0;
                    for (int v = i;;) {
                        ToDel.push_back(v);
                        LocalInCover[v] = true;
                        cycle_len++;
                        int new_v = -1;
                        for (int nei : G[v])
                            if (!LocalInCover[nei]) {
                                new_v = nei;
                                break;
                            }
                        if (new_v == -1)
                            break;
                        else
                            v = new_v;
                    }
                    result += (cycle_len + 1) / 2;
                    if (result + k_used > k)
                        return numeric_limits<int>::max();
                }

            report_solution(k_used + result);
            for (int x : ToDel)
                CanBeInGoodCover[x] = true;
            return k_used + result;
        };

        function<int(int)> vertex_cover = [&](int k_used) -> int { // returns min k_used found, INT_MAX on failure
            // helpers
            auto add_to_cover = [&](int v) {
                InCover[v] = true;
                for (int nei : G[v])
                    Degs[nei]--;
            };
            auto remove_from_cover = [&](int v) {
                InCover[v] = false;
                for (int nei : G[v])
                    Degs[nei]++;
            };

            // get stats, find next target
            int max_deg_val = -1, max_deg_i = -1;
            for (int i = 0; i < n; i++)
                if (!InCover[i] && Degs[i] > max_deg_val) {
                    max_deg_val = Degs[i];
                    max_deg_i = i;
                }

            // special cases
            if (max_deg_val == 0) {
                report_solution(k_used);
                return k_used;
            }
            if (max_deg_val <= 2) {
                int used = vertex_cover_small_deg(k_used);
                if (used == k)
                    return used;
                else
                    return numeric_limits<int>::max();
            }

            // general case (maxdeg >= 3)
            int result = numeric_limits<int>::max();
            if (k - k_used >= 1) {
                add_to_cover(max_deg_i);
                int subres = vertex_cover(k_used + 1);
                if (subres == k)
                    CanBeInGoodCover[max_deg_i] = true;
                result = min(result, subres);
                remove_from_cover(max_deg_i);
            }
            if (k - k_used >= max_deg_val) {
                stack<int>S;
                for (int nei : G[max_deg_i])
                    if (!InCover[nei]) {
                        add_to_cover(nei);
                        S.push(nei);
                    }
                int subres = vertex_cover(k_used + max_deg_val);
                result = min(result, subres);
                while (!S.empty()) {
                    remove_from_cover(S.top());
                    if (subres == k)
                        CanBeInGoodCover[S.top()] = true;
                    S.pop();
                }
            }

            return result;
        };

        int res = vertex_cover(0);
        if (res == numeric_limits<int>::max()) {
            cout << "-1\n";
            continue;
        }
        else {
            int CBIGCnumber = 0;
            for (int i = 0; i < n; i++)
                if (CanBeInGoodCover[i])
                    CBIGCnumber++;
            cout << res << " " << CBIGCnumber << "\n";
            for (int i = 0; i < n; i++)
                if (CanBeInGoodCover[i])
                    cout << i + 1 << " ";
            cout << "\n";
        }
    }

    return 0;
}