//Franciszek Witt #include<bits/stdc++.h> using namespace std; #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 typedef long long ll; constexpr int base = 15; struct mst { vector<int> st; int si = 1; void init(int x) { while(si <= (2 * (x + 1))) si *= 2; st.resize(si); } void ins(int x, int y){ y <<= base; y |= x; x += si / 2; st[x] = y; x >>= 1; while(x) { int mx = max(st[x << 1], st[(x << 1) | 1]); if(st[x] == mx) break; st[x] = mx; x >>= 1; } } int get() { return st[1] & ((1 << base) - 1); } }; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t; cin >> t; REP(tt, t) { int n, m, k; cin >> n >> m >> k; vector graph(n + 1, vector<int>()); vector vdeg(n + 1, 0); vector<pair<int,int>> edg(m); REP(i, m) { cin >> edg[i].first >> edg[i].second; if(edg[i].first > edg[i].second) swap(edg[i].first, edg[i].second); } sort(edg.begin(), edg.end()); edg.resize(unique(edg.begin(), edg.end()) - edg.begin()); m = ssize(edg); for(auto [a, b] : edg) { graph[a].emplace_back(b); graph[b].emplace_back(a); ++vdeg[a]; ++vdeg[b]; } mst st; st.init(n); FOR(i, 1, n) st.ins(i, vdeg[i]); debug(graph); vector<int> ans(n + 1), cho(n + 1); int roz = 0, best = n + 1, unma = m; function<void()> bck = [&]() { debug(roz, best, unma, cho, vdeg); if(roz > k || roz > best) return; if(unma == 0) { if(roz < best) { FOR(i, 1, n) ans[i] = 0; best = roz; } FOR(i, 1, n) //bitset ?? ans[i] |= cho[i]; return; } int kto = st.get(); debug(kto, vdeg[kto]); if(vdeg[kto] == 1) { //mozna latwo liniowo debug("!!\n"); int cnt = 0; FOR(i, 1, n) cnt += (vdeg[i] == 1); assert(cnt % 2 == 0); if(roz + cnt / 2 > best) return; if(roz + cnt / 2 < best) FOR(i, 1, n) ans[i] = 0; best = roz + cnt / 2; FOR(i, 1, n) ans[i] |= (cho[i]) || (vdeg[i] == 1); return; } auto turnon = [&] (int x) { assert(!cho[x]); for(auto i : graph[x]) { if(!cho[i]) { --unma; --vdeg[i]; --vdeg[x]; st.ins(i, vdeg[i]); } } st.ins(x, vdeg[x]); cho[x] = 1; ++roz; }; auto turnoff = [&] (int x) { assert(cho[x]); for(auto i : graph[x]) { if(!cho[i]) { ++unma; ++vdeg[i]; ++vdeg[x]; st.ins(i, vdeg[i]); } } st.ins(x, vdeg[x]); cho[x] = 0; --roz; }; if(roz + 1 <= k && roz + 1 <= best) { turnon(kto); bck(); turnoff(kto); } if(roz + vdeg[kto] <= k && roz + vdeg[kto] <= best) { debug(roz); vector<int> to; for(auto i : graph[kto]) { if(!cho[i]) { turnon(i); to.emplace_back(i); } } debug(roz); bck(); for(auto i : to) turnoff(i); } }; bck(); int ile = accumulate(ans.begin(), ans.end(), 0); if(best > k) cout << -1 << endl; else { cout << best << " " << ile << endl; FOR(i, 1, n) if(ans[i]) cout << i << " "; cout << endl; } } }
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 | //Franciszek Witt #include<bits/stdc++.h> using namespace std; #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 typedef long long ll; constexpr int base = 15; struct mst { vector<int> st; int si = 1; void init(int x) { while(si <= (2 * (x + 1))) si *= 2; st.resize(si); } void ins(int x, int y){ y <<= base; y |= x; x += si / 2; st[x] = y; x >>= 1; while(x) { int mx = max(st[x << 1], st[(x << 1) | 1]); if(st[x] == mx) break; st[x] = mx; x >>= 1; } } int get() { return st[1] & ((1 << base) - 1); } }; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t; cin >> t; REP(tt, t) { int n, m, k; cin >> n >> m >> k; vector graph(n + 1, vector<int>()); vector vdeg(n + 1, 0); vector<pair<int,int>> edg(m); REP(i, m) { cin >> edg[i].first >> edg[i].second; if(edg[i].first > edg[i].second) swap(edg[i].first, edg[i].second); } sort(edg.begin(), edg.end()); edg.resize(unique(edg.begin(), edg.end()) - edg.begin()); m = ssize(edg); for(auto [a, b] : edg) { graph[a].emplace_back(b); graph[b].emplace_back(a); ++vdeg[a]; ++vdeg[b]; } mst st; st.init(n); FOR(i, 1, n) st.ins(i, vdeg[i]); debug(graph); vector<int> ans(n + 1), cho(n + 1); int roz = 0, best = n + 1, unma = m; function<void()> bck = [&]() { debug(roz, best, unma, cho, vdeg); if(roz > k || roz > best) return; if(unma == 0) { if(roz < best) { FOR(i, 1, n) ans[i] = 0; best = roz; } FOR(i, 1, n) //bitset ?? ans[i] |= cho[i]; return; } int kto = st.get(); debug(kto, vdeg[kto]); if(vdeg[kto] == 1) { //mozna latwo liniowo debug("!!\n"); int cnt = 0; FOR(i, 1, n) cnt += (vdeg[i] == 1); assert(cnt % 2 == 0); if(roz + cnt / 2 > best) return; if(roz + cnt / 2 < best) FOR(i, 1, n) ans[i] = 0; best = roz + cnt / 2; FOR(i, 1, n) ans[i] |= (cho[i]) || (vdeg[i] == 1); return; } auto turnon = [&] (int x) { assert(!cho[x]); for(auto i : graph[x]) { if(!cho[i]) { --unma; --vdeg[i]; --vdeg[x]; st.ins(i, vdeg[i]); } } st.ins(x, vdeg[x]); cho[x] = 1; ++roz; }; auto turnoff = [&] (int x) { assert(cho[x]); for(auto i : graph[x]) { if(!cho[i]) { ++unma; ++vdeg[i]; ++vdeg[x]; st.ins(i, vdeg[i]); } } st.ins(x, vdeg[x]); cho[x] = 0; --roz; }; if(roz + 1 <= k && roz + 1 <= best) { turnon(kto); bck(); turnoff(kto); } if(roz + vdeg[kto] <= k && roz + vdeg[kto] <= best) { debug(roz); vector<int> to; for(auto i : graph[kto]) { if(!cho[i]) { turnon(i); to.emplace_back(i); } } debug(roz); bck(); for(auto i : to) turnoff(i); } }; bck(); int ile = accumulate(ans.begin(), ans.end(), 0); if(best > k) cout << -1 << endl; else { cout << best << " " << ile << endl; FOR(i, 1, n) if(ans[i]) cout << i << " "; cout << endl; } } } |