#include<bits/stdc++.h> using namespace std; const int MAXN = 1000; const int MAX_DEG = 3000; int n,m; vector<pair<int,int>> g[MAXN]; set<pair<int,int>> S; int deg[MAXN]; bool marked[MAXN]; int diff[MAXN]; bool in_result[MAXN]; bool backtrack(int); inline void mark(int v) { marked[v] = true; for(auto [u,k] : g[v]) { deg[u] -= k; diff[u]--; } m -= deg[v]; } inline void unmark(int v) { marked[v] = false; for(auto [u,k] : g[v]) { deg[u] += k; diff[u]++; } m += deg[v]; } inline bool step(int v, int t) { mark(v); bool res = backtrack(t-1); unmark(v); return res; } bool backtrack(int t) { if(m == 0) return true; if(t == 0) return false; int c = (m+t-1)/t; int md = 0, v = -1; for(int i=0;i<n;i++) if(deg[i] >= md && !marked[i]) md = deg[i], v = i; if(md < c) return false; { mark(v); bool res = backtrack(t-1); unmark(v); if(res) { in_result[v] = true; return true; } } if(diff[v] > t) return false; { vector<int> c; for(auto [u,k] : g[v]) if(!marked[u]) c.push_back(u); for(int u : c) mark(u), t--; bool res = backtrack(t); for(int u : c) unmark(u), t--; if(res) { for(int u : c) in_result[u] = true; return true; } } return false; } void query() { int k; cin >> n >> m >> k; for(int i=0;i<n;i++) g[i].clear(); for(int i=0;i<n;i++) marked[i] = false, in_result[i] = false; for(int i=0;i<m;i++) { int v,u; cin >> v >> u, v--, u--; g[v].push_back({u,1}); g[u].push_back({v,1}); } for(auto& x : g) { sort(x.begin(), x.end()); vector<pair<int,int>> y; if(!x.empty()) y.push_back({x[0].first, 0}); for(auto [a,b] : x) if(y.back().first != a) y.push_back({a,b}); else y.back().second++; x = y; } for(int v=0;v<n;v++) { deg[v] = 0; for(auto [u,k] : g[v]) deg[v] += k; diff[v] = g[v].size(); } for(int t=1;t<=k;t++) if(backtrack(t)) { vector<int> cands; for(int v=0;v<n;v++) if(in_result[v]) for(auto [u,k] : g[v]) cands.push_back(u); sort(cands.begin(), cands.end()); cands.resize(unique(cands.begin(), cands.end()) - cands.begin()); vector<int> result; for(int v : cands) if(in_result[v] || step(v,t)) in_result[v] = true; for(int i=0;i<n;i++) if(in_result[i]) result.push_back(i); cout << t << " " << result.size() << "\n"; for(int i : result) cout << i+1 << " "; cout << "\n"; return; } cout << "-1\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int q; cin >> q; while(q--) query(); return 0; }
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 | #include<bits/stdc++.h> using namespace std; const int MAXN = 1000; const int MAX_DEG = 3000; int n,m; vector<pair<int,int>> g[MAXN]; set<pair<int,int>> S; int deg[MAXN]; bool marked[MAXN]; int diff[MAXN]; bool in_result[MAXN]; bool backtrack(int); inline void mark(int v) { marked[v] = true; for(auto [u,k] : g[v]) { deg[u] -= k; diff[u]--; } m -= deg[v]; } inline void unmark(int v) { marked[v] = false; for(auto [u,k] : g[v]) { deg[u] += k; diff[u]++; } m += deg[v]; } inline bool step(int v, int t) { mark(v); bool res = backtrack(t-1); unmark(v); return res; } bool backtrack(int t) { if(m == 0) return true; if(t == 0) return false; int c = (m+t-1)/t; int md = 0, v = -1; for(int i=0;i<n;i++) if(deg[i] >= md && !marked[i]) md = deg[i], v = i; if(md < c) return false; { mark(v); bool res = backtrack(t-1); unmark(v); if(res) { in_result[v] = true; return true; } } if(diff[v] > t) return false; { vector<int> c; for(auto [u,k] : g[v]) if(!marked[u]) c.push_back(u); for(int u : c) mark(u), t--; bool res = backtrack(t); for(int u : c) unmark(u), t--; if(res) { for(int u : c) in_result[u] = true; return true; } } return false; } void query() { int k; cin >> n >> m >> k; for(int i=0;i<n;i++) g[i].clear(); for(int i=0;i<n;i++) marked[i] = false, in_result[i] = false; for(int i=0;i<m;i++) { int v,u; cin >> v >> u, v--, u--; g[v].push_back({u,1}); g[u].push_back({v,1}); } for(auto& x : g) { sort(x.begin(), x.end()); vector<pair<int,int>> y; if(!x.empty()) y.push_back({x[0].first, 0}); for(auto [a,b] : x) if(y.back().first != a) y.push_back({a,b}); else y.back().second++; x = y; } for(int v=0;v<n;v++) { deg[v] = 0; for(auto [u,k] : g[v]) deg[v] += k; diff[v] = g[v].size(); } for(int t=1;t<=k;t++) if(backtrack(t)) { vector<int> cands; for(int v=0;v<n;v++) if(in_result[v]) for(auto [u,k] : g[v]) cands.push_back(u); sort(cands.begin(), cands.end()); cands.resize(unique(cands.begin(), cands.end()) - cands.begin()); vector<int> result; for(int v : cands) if(in_result[v] || step(v,t)) in_result[v] = true; for(int i=0;i<n;i++) if(in_result[i]) result.push_back(i); cout << t << " " << result.size() << "\n"; for(int i : result) cout << i+1 << " "; cout << "\n"; return; } cout << "-1\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int q; cin >> q; while(q--) query(); return 0; } |