#include <bits/stdc++.h> #define ll long long #define fors(u, n, s) for(ll u=(s); u < (n); u++) #define foru(u, n) fors(u, n, 0) #define ir(a, b, x) (((a) <= (x)) && ((x) <= (b))) #define vec vector #define pb push_back using namespace std; #define N 1010 #define T 35 int n; int m; int t; vec<int> graph[N]; int deg[N]; bool marked[N]; int edges_left; bool covering[N][T]; ///is N in T covering bool exists_covering[T]; void init_covering(){ foru(i, n){ deg[i]=graph[i].size(); marked[i]=false; foru(j, T) covering[i][j]=false; } foru(i, T) exists_covering[i]=false; edges_left=m; } void mark(int x){ marked[x]=true; for(auto i : graph[x]){ deg[i]--; if(marked[i]) continue; edges_left--; } } void unmark(int x){ marked[x]=false; for(auto i : graph[x]){ deg[i]++; if(marked[i]) continue; edges_left++; } } void save_cover(){ int cnt = 0; foru(i, n) cnt += marked[i]; if(cnt>t) return; exists_covering[cnt]=true; foru(i, n) if(marked[i]) covering[i][cnt]=true; } vec<vec<int>> segments; vec<vec<int>> cycles; bool visited[N]; void cycle_segment_decomposition(){ segments.resize(0); cycles.resize(0); foru(i, n) visited[i]=false; foru(i, n){ ///getting segments if(visited[i] || marked[i]) continue; if(deg[i]==2) continue; if(deg[i]==0){ visited[i]=true; continue; } vec<int> segment; int x = i; while(true){ segment.pb(x); visited[x]=true; int next_x=-1; for(auto j : graph[x]) { if(!visited[j] && !marked[j]){ next_x=j; break; } } if(next_x==-1) break; x=next_x; } vec<int> new_vec = segment; segments.pb(new_vec); } foru(i, n){ ///getting cycles if(visited[i] || marked[i]) continue; vector<int> cycle; int x = i; while(true){ cycle.pb(x); visited[x]=true; bool found=false; int next_x=-1; for(auto j : graph[x]) if(!visited[j] && !marked[j]){ next_x=j; break; } if(next_x==-1) break; x=next_x; } cycles.pb(cycle); } } void cover(int k){ if(k<0) return; if(k==0){ if(edges_left) return; save_cover(); return; } if(!edges_left){ save_cover(); return; } int leader=-1; int prev_deg=-1; foru(i, n) if(deg[i]>prev_deg && !marked[i]) {leader=i; prev_deg=deg[leader];} if(leader == -1) cout << "PANICK" << endl; if(deg[leader]<3){ cycle_segment_decomposition(); int ans = 0; foru(i, n) ans+=marked[i]; for(auto i : cycles){ ans += (i.size()+1)/2; } for(auto i : segments){ ans += i.size()/2; } if(ans >= T) return; exists_covering[ans]=true; for(auto i : cycles){ for(int j=0; j<i.size(); j++){ covering[i[j]][ans]=true; } } for(auto i : segments){ if(!(i.size() & 1)) for(int j=0; j<i.size(); j+=2){ covering[i[j]][ans]=true; } for(int j=1; j<i.size(); j+=2){ covering[i[j]][ans]=true; } } foru(i, n) if(marked[i]) covering[i][ans]=true; return; } int cnt=0; mark(leader); cover(k-1); unmark(leader); vec<int> to_unmark; for(auto i : graph[leader]) if(!marked[i]) {mark(i); to_unmark.pb(i); cnt++;} cover(k-cnt); for(auto i : to_unmark) unmark(i); } bool exists_edge[N][N]; void submain(){ cin >> n >> m >> t; foru(i, n) graph[i].resize(0); foru(i, n) foru(j, n) exists_edge[i][j]=false; int fake_cnt=0; foru(_i, m){ int x; int y; cin >> x >> y; x--; y--; if(exists_edge[x][y]) {fake_cnt++; continue;} exists_edge[x][y]=true; exists_edge[y][x]=true; graph[x].pb(y); graph[y].pb(x); } m-=fake_cnt; init_covering(); cover(t); int ans=-1; foru(i, T) if(exists_covering[i]) {ans=i; break;} if(ans==-1 || ans>t) {cout << -1 << endl; return;} cout << ans << " "; int cnt=0; foru(i, n) cnt+=covering[i][ans]; cout << cnt << endl; foru(i, n) if(covering[i][ans]) cout << i+1 << " "; cout << endl; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int H; cin >> H; foru(_i, H) submain(); 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 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 | #include <bits/stdc++.h> #define ll long long #define fors(u, n, s) for(ll u=(s); u < (n); u++) #define foru(u, n) fors(u, n, 0) #define ir(a, b, x) (((a) <= (x)) && ((x) <= (b))) #define vec vector #define pb push_back using namespace std; #define N 1010 #define T 35 int n; int m; int t; vec<int> graph[N]; int deg[N]; bool marked[N]; int edges_left; bool covering[N][T]; ///is N in T covering bool exists_covering[T]; void init_covering(){ foru(i, n){ deg[i]=graph[i].size(); marked[i]=false; foru(j, T) covering[i][j]=false; } foru(i, T) exists_covering[i]=false; edges_left=m; } void mark(int x){ marked[x]=true; for(auto i : graph[x]){ deg[i]--; if(marked[i]) continue; edges_left--; } } void unmark(int x){ marked[x]=false; for(auto i : graph[x]){ deg[i]++; if(marked[i]) continue; edges_left++; } } void save_cover(){ int cnt = 0; foru(i, n) cnt += marked[i]; if(cnt>t) return; exists_covering[cnt]=true; foru(i, n) if(marked[i]) covering[i][cnt]=true; } vec<vec<int>> segments; vec<vec<int>> cycles; bool visited[N]; void cycle_segment_decomposition(){ segments.resize(0); cycles.resize(0); foru(i, n) visited[i]=false; foru(i, n){ ///getting segments if(visited[i] || marked[i]) continue; if(deg[i]==2) continue; if(deg[i]==0){ visited[i]=true; continue; } vec<int> segment; int x = i; while(true){ segment.pb(x); visited[x]=true; int next_x=-1; for(auto j : graph[x]) { if(!visited[j] && !marked[j]){ next_x=j; break; } } if(next_x==-1) break; x=next_x; } vec<int> new_vec = segment; segments.pb(new_vec); } foru(i, n){ ///getting cycles if(visited[i] || marked[i]) continue; vector<int> cycle; int x = i; while(true){ cycle.pb(x); visited[x]=true; bool found=false; int next_x=-1; for(auto j : graph[x]) if(!visited[j] && !marked[j]){ next_x=j; break; } if(next_x==-1) break; x=next_x; } cycles.pb(cycle); } } void cover(int k){ if(k<0) return; if(k==0){ if(edges_left) return; save_cover(); return; } if(!edges_left){ save_cover(); return; } int leader=-1; int prev_deg=-1; foru(i, n) if(deg[i]>prev_deg && !marked[i]) {leader=i; prev_deg=deg[leader];} if(leader == -1) cout << "PANICK" << endl; if(deg[leader]<3){ cycle_segment_decomposition(); int ans = 0; foru(i, n) ans+=marked[i]; for(auto i : cycles){ ans += (i.size()+1)/2; } for(auto i : segments){ ans += i.size()/2; } if(ans >= T) return; exists_covering[ans]=true; for(auto i : cycles){ for(int j=0; j<i.size(); j++){ covering[i[j]][ans]=true; } } for(auto i : segments){ if(!(i.size() & 1)) for(int j=0; j<i.size(); j+=2){ covering[i[j]][ans]=true; } for(int j=1; j<i.size(); j+=2){ covering[i[j]][ans]=true; } } foru(i, n) if(marked[i]) covering[i][ans]=true; return; } int cnt=0; mark(leader); cover(k-1); unmark(leader); vec<int> to_unmark; for(auto i : graph[leader]) if(!marked[i]) {mark(i); to_unmark.pb(i); cnt++;} cover(k-cnt); for(auto i : to_unmark) unmark(i); } bool exists_edge[N][N]; void submain(){ cin >> n >> m >> t; foru(i, n) graph[i].resize(0); foru(i, n) foru(j, n) exists_edge[i][j]=false; int fake_cnt=0; foru(_i, m){ int x; int y; cin >> x >> y; x--; y--; if(exists_edge[x][y]) {fake_cnt++; continue;} exists_edge[x][y]=true; exists_edge[y][x]=true; graph[x].pb(y); graph[y].pb(x); } m-=fake_cnt; init_covering(); cover(t); int ans=-1; foru(i, T) if(exists_covering[i]) {ans=i; break;} if(ans==-1 || ans>t) {cout << -1 << endl; return;} cout << ans << " "; int cnt=0; foru(i, n) cnt+=covering[i][ans]; cout << cnt << endl; foru(i, n) if(covering[i][ans]) cout << i+1 << " "; cout << endl; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int H; cin >> H; foru(_i, H) submain(); return 0; } |