#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int> pii; const int MAXN = 1e3 + 5; const int inf = 0x3f3f3f3f; int n,m,d; vector<int> g[MAXN]; int deg[MAXN]; int vis[MAXN], sta[MAXN], tp = 0; int ans = inf; int mnval[MAXN]; void setvis(int u,int k) { vis[u] += k; for(int v: g[u]) if(!vis[v]) deg[v] -= k; } void roll(int k) { while(tp > k) { setvis(sta[tp], -1); --tp; } } void dfs(void) { if(tp > ans || tp > d) return; int mx = 0; for(int i=1; i<=n; ++i) if(!vis[i] && deg[i] > deg[mx]) mx = i; if(deg[mx] == 0) { ans = tp; for(int i=1; i<=tp; ++i) mnval[sta[i]] = tp; return; } if(deg[mx] <= 2) { static int clr[MAXN]; for(int i=1; i<=n; ++i) clr[i] = vis[i]; int cur = tp; vector<int> ps; for(int i=1; i<=n && cur <= ans && cur <= d; ++i) if(!clr[i] && deg[i] == 1) { int u = i; vector<int> vec; while(1) { clr[u] = 1; vec.emplace_back(u); bool flag = 0; for(int v: g[u]) if(!clr[v]) { u = v; flag = 1; break; } if(!flag) break; } if((int)vec.size() % 2 == 1) { cur += (int)vec.size() / 2; for(int j=1; j<(int)vec.size(); j+=2) ps.emplace_back(vec[j]); } else { cur += (int)vec.size() / 2; for(int j=0; j<(int)vec.size(); ++j) ps.emplace_back(vec[j]); } } for(int i=1; i<=n && cur <= ans && cur <= d; ++i) if(!clr[i] && deg[i] == 2) { int u = i; vector<int> vec; while(1) { clr[u] = 1; vec.emplace_back(u); bool flag = 0; for(int v: g[u]) if(!clr[v]) { u = v; flag = 1; break; } if(!flag) break; } cur += ((int)vec.size() + 1) / 2; for(int j=0; j<(int)vec.size(); ++j) ps.emplace_back(vec[j]); } if(cur > d || cur > ans) return; ans = cur; for(int u: ps) mnval[u] = cur; for(int i=1; i<=tp; ++i) mnval[sta[i]] = cur; return; } int save = tp; setvis(mx, 1); sta[++tp] = mx; dfs(); roll(save); for(int u: g[mx]) if(!vis[u]) setvis(u, 1), sta[++tp] = u; dfs(); roll(save); } void solve(void) { scanf("%d%d%d",&n,&m,&d); for(int i=1; i<=n; ++i) g[i].clear(); set<pii> bak; for(int i=1; i<=m; ++i) { int u,v; scanf("%d%d",&u,&v); if(!bak.insert({u,v}).second) continue; g[u].emplace_back(v); g[v].emplace_back(u); } for(int i=1; i<=n; ++i) { deg[i] = (int)g[i].size(); vis[i] = 0; mnval[i] = inf; } tp = 0; ans = inf; dfs(); if(ans == inf) { printf("-1\n"); } else { vector<int> vec; for(int i=1; i<=n; ++i) if(mnval[i] == ans) vec.emplace_back(i); printf("%d %d\n",ans,(int)vec.size()); for(int u: vec) printf("%d ",u); printf("\n"); } } int main(void) { #ifdef He_Ren freopen("a.in","r",stdin); freopen("a.out","w",stdout); #endif int T; scanf("%d",&T); while(T--) solve(); 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 | #include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int> pii; const int MAXN = 1e3 + 5; const int inf = 0x3f3f3f3f; int n,m,d; vector<int> g[MAXN]; int deg[MAXN]; int vis[MAXN], sta[MAXN], tp = 0; int ans = inf; int mnval[MAXN]; void setvis(int u,int k) { vis[u] += k; for(int v: g[u]) if(!vis[v]) deg[v] -= k; } void roll(int k) { while(tp > k) { setvis(sta[tp], -1); --tp; } } void dfs(void) { if(tp > ans || tp > d) return; int mx = 0; for(int i=1; i<=n; ++i) if(!vis[i] && deg[i] > deg[mx]) mx = i; if(deg[mx] == 0) { ans = tp; for(int i=1; i<=tp; ++i) mnval[sta[i]] = tp; return; } if(deg[mx] <= 2) { static int clr[MAXN]; for(int i=1; i<=n; ++i) clr[i] = vis[i]; int cur = tp; vector<int> ps; for(int i=1; i<=n && cur <= ans && cur <= d; ++i) if(!clr[i] && deg[i] == 1) { int u = i; vector<int> vec; while(1) { clr[u] = 1; vec.emplace_back(u); bool flag = 0; for(int v: g[u]) if(!clr[v]) { u = v; flag = 1; break; } if(!flag) break; } if((int)vec.size() % 2 == 1) { cur += (int)vec.size() / 2; for(int j=1; j<(int)vec.size(); j+=2) ps.emplace_back(vec[j]); } else { cur += (int)vec.size() / 2; for(int j=0; j<(int)vec.size(); ++j) ps.emplace_back(vec[j]); } } for(int i=1; i<=n && cur <= ans && cur <= d; ++i) if(!clr[i] && deg[i] == 2) { int u = i; vector<int> vec; while(1) { clr[u] = 1; vec.emplace_back(u); bool flag = 0; for(int v: g[u]) if(!clr[v]) { u = v; flag = 1; break; } if(!flag) break; } cur += ((int)vec.size() + 1) / 2; for(int j=0; j<(int)vec.size(); ++j) ps.emplace_back(vec[j]); } if(cur > d || cur > ans) return; ans = cur; for(int u: ps) mnval[u] = cur; for(int i=1; i<=tp; ++i) mnval[sta[i]] = cur; return; } int save = tp; setvis(mx, 1); sta[++tp] = mx; dfs(); roll(save); for(int u: g[mx]) if(!vis[u]) setvis(u, 1), sta[++tp] = u; dfs(); roll(save); } void solve(void) { scanf("%d%d%d",&n,&m,&d); for(int i=1; i<=n; ++i) g[i].clear(); set<pii> bak; for(int i=1; i<=m; ++i) { int u,v; scanf("%d%d",&u,&v); if(!bak.insert({u,v}).second) continue; g[u].emplace_back(v); g[v].emplace_back(u); } for(int i=1; i<=n; ++i) { deg[i] = (int)g[i].size(); vis[i] = 0; mnval[i] = inf; } tp = 0; ans = inf; dfs(); if(ans == inf) { printf("-1\n"); } else { vector<int> vec; for(int i=1; i<=n; ++i) if(mnval[i] == ans) vec.emplace_back(i); printf("%d %d\n",ans,(int)vec.size()); for(int u: vec) printf("%d ",u); printf("\n"); } } int main(void) { #ifdef He_Ren freopen("a.in","r",stdin); freopen("a.out","w",stdout); #endif int T; scanf("%d",&T); while(T--) solve(); return 0; } |